如何在Excel中查找和替换多个值?

时间:2013-11-05 15:47:55

标签: excel vba excel-vba

我有一个包含18000个域的列和另一个包含210.000域的列表,可以找到这些18000个域。我需要做像CTRL + H一样的smth,用于查找和替换,在查找字段中我需要添加整个18000域名:smth如* domain1.com *,* domain2.com *,* domain3.com *并替换他们有空白。尝试使用excel中的查找和替换,但在“查找”字段中添加多于1个值无效。我怎样才能为多个值做到这一点?

1 个答案:

答案 0 :(得分:1)

VBA解决方案

您需要更改两个工作表引用(数据和编辑工作表)data = source,edit = destination。我还将替换字符串设置为变量,以便您可以根据需要从空字符串中更改它。

如果您需要任何其他逻辑(即在比较之前修剪字符串或更改字符串大小写比较),代码应该相当容易调整。

希望这有帮助。

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 = 1


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

您可以将通配符与find / replace一起使用。所以在你的情况下,你应该可以使用像

这样的东西 domain*字段中的

find whatReplace字段中没有任何内容