我正在寻找VBA代码在Excel中运行以查找和替换大量单词。在Excel中使用VBA查找和替换多个值
基本上,它将是一个简单的Excel文件,其中Sheet1包含1列中包含要替换名称的短语(不是整个短语,而是可能由少量词组成的一个名称)。第二个工作表2包含我需要在工作表Sheet1中找到的1个列值(可能有不止一次当值出现在第1列中时)以及包含翻译的列。我不需要Google API,因为名称非常自定义。
我遇到了下面的脚本,但它基本上什么也没做。
Sub ReplaceValues()
Dim dataSht As Worksheet
Dim editSht As Worksheet
Dim dataRange As Range
Dim dataColumn As Long
Dim editColumn As Long
Dim dataEndRow As Long
Dim editEndRow As Long
'sheet that holds all the values we want to find
Set dataSht = Sheet2
'sheet we want to edit
Set editSht = Sheet1
Dim replaceValue As String
'replace value is empty string
replaceValue = ""
'set the column of the data sheet to A
dataColumn = 1
'set the colmun of the sheet to edit to A
editColumn = 5
dataEndRow = dataSht.Cells(dataSht.Rows.count, dataColumn).End(xlUp).Row
editEndRow = editSht.Cells(editSht.Rows.count, editColumn).End(xlUp).Row
'this is the range of the data that we're looking for
Set dataRange = dataSht.Range(dataSht.Cells(1, dataColumn),
dataSht.Cells(dataEndRow, dataColumn))
Dim count As Long
Dim val As String
For i = 1 To editEndRow
val = editSht.Cells(i, editColumn).Value
count = Application.WorksheetFunction.CountIf(dataRange, val)
If count > 0 And Trim(val) <> "" Then
editSht.Cells(i, editColumn).Value = replaceValue
End If
Next i
End Sub
到目前为止您尝试过什么?发布已经遇到特定问题或问题的编码尝试。 – Zerk
请参考[游览](https://stackoverflow.com/tour),阅读[如何问](https://stackoverflow.com/help/how-to-ask)和[最小,完整和可验证示例](https://stackoverflow.com/help/mcve)。然后编辑你的问题。 – danieltakeshi
更新了帖子 –