VBA Excelsearch包含不同单词之一的单元格

我需要创build一个脚本来着色或删除至less包含一个我需要search的单词的行。 例如,我有一个数组或像“alpha”“beta”“gamma”这样的单词列表,我需要在colum A和B中search那里有不同的单词和string,如果这些单词存在,至less有一个他们,然后颜色或删除行,但这不是问题。 这是我所做的,find了互联网上的某个地方,但它不起作用。 我有大约30个词来search,甚至更多。 你可以帮我吗?

Sub Macro2() ' ' Macro2 Macro ' For i = 2 To 1000 If Evaluate("COUNT(SEARCH({""alpha"",""beta"",""gamma""}," & Cells(i, 1).Address & "))") > 0 Then Rows(i).Interior.Color = RGB(127, 187, 199) GoTo Prossimo End If If Evaluate("COUNT(SEARCH({""alpha"",""beta"",""gamma""}," & Cells(i, 2).Address & "))") > 0 Then Rows(i).Interior.Color = RGB(127, 187, 199) GoTo Prossimo End If Prossimo: Next i End Sub 

感谢你们!

我会先创build一个你的特殊字的数组

 words = Array("alpha", "beta", ...) 

然后,您可以遍历电子表格中的行和数组中的项目,查找匹配项。

 words = Array("alpha", "beta", ...) for i = 2 to 1000 for j = lbound(words) to ubound(words) if cells(i,1)=words(j) or cells(i,2)=words(j) then rows(i).interior.color=rgb(127,187,199) end if next j next i 

尝试这个

 Option Explicit Sub Macro2() Dim cell as Range Dim myWords as String myWords = "|alpha|beta|gamma|" With Worksheets("MySheet") '<--| change "MySheet" to your actual sheet name For Each cell in .Range("A2:A" & .Cells(.Rows.Count,"A").End(xlUp).Row) If InStr(myWords, "|" & cell.Value & "|") > 0 or InStr(myWords, "|" & cell.Offset(,1).Value & "|") > 0 Then cell.EntireRow.Interior.Color = RGB(127, 187, 199) Next cell End With End Sub