创建宏以在工作表中搜索字符串列表并突出显示该行

时间:2014-06-26 12:07:58

标签: excel-vba excel-2010 string-search vba excel

有人可以帮我创建一个宏来搜索Excel工作表中的30个字符串列表(例如SV-32488r1SV-33485r1)并在找到时突出显示Row吗?

  • 我正在使用Office 2010。
  • 我不是Excel或VBA,所以我不知道从哪里开始。
  • 我发现的搜索只允许我搜索一个字符串。

提前非常感谢你。

4 个答案:

答案 0 :(得分:1)

Public Sub HighlightListedValues()
    Dim strConcatList As String
    Dim cell As Range

    'Creates a string concatenating your list of strings, separated by |s
    'e.g. "item1|item2|item3|item4|"
    For Each cell In Sheets("List").Range("A1:A30")
        strConcatList = strConcatList & cell.Value & "|"
    Next cell

    'For each used cell in Column A of sheet1, check whether the value in that cell
    'is contained within the concatenated string
    For Each cell In Intersect(Sheets("Sheet1").Range("A:A"), Sheets("Sheet1").UsedRange)
        If InStr(strConcatList, cell.Value) > 0 Then       'InStr returns 0 if the string isn't found
            cell.EntireRow.Interior.Color = RGB(255, 0, 0) 'Highlights the row in red if value found
        End If
    Next cell
End Sub

如果满足以下条件,则会以红色突出显示相关行:

  • 与您要搜索的值匹配的数据位于名为" Sheet1"
  • 的工作表的A列中
  • 您的字符串列表包含在名为" List"的工作表的单元格A1:A30中。

根据需要修改工作表和范围的名称,例如如果要在名为" Data"的工作表的列A到C中搜索。您可以将Sheets("Sheet1").Range("A:A")修改为Sheets("Data").Range("A:C")

希望这有帮助!

答案 1 :(得分:1)

我创建了宏来查找单词列表并突出显示单元格。 当我使用代码时,它还突出显示值之间的空单元格。我也要求代码找到包含单词之间单词的单元格。

实施例: 我创建了查找并突出显示单元格中的“Shev”一词。它应该找到并突出显示它是否位于单元格的中间,如“is shev in”。

此致

Parthi

以下代码:

Public Sub HighlightListedValues()

Dim strConcatList As String
Dim cell As Range


For Each cell In Sheets("Bad Words").Range("A1:A30")
    strConcatList = strConcatList & cell.Value & "|"
Next cell

    For Each cell In Intersect(Sheets("Data").Range("A:Z"), Sheets("data").UsedRange)
    If InStr(strConcatList, cell.Value) > 0 Then
        cell.Interior.Color = 63125789
    End If
Next cell

End Sub

答案 2 :(得分:0)

未经测试对其进行着色的部分,但这应该会给你一个良好的开端

'====================================================================================
iWarnColor = xlThemeColorAccent2
' This Group is what I use to delte a row.
' If you want to delete another row just change the term your searching for
    Do
       Set c = SrchRng.find("CHANGE THIS TO WHAT YOU WANT TO FIND", LookIn:=xlValues)
       If Not c Is Nothing Then c.EntireRow.ColorIndex = iWarnColor
   Loop While Not c Is Nothing
'=====================================================================================

以下是我过去用来删除包含特定文本的行

的内容
Do
       Set c = AsrchRng.find("Date", LookIn:=xlValues)
       If Not c Is Nothing Then c.EntireRow.Delete
   Loop While Not c Is Nothing
    Do
       Set c = AsrchRng.find("Note", LookIn:=xlValues)
       If Not c Is Nothing Then c.EntireRow.Delete
   Loop While Not c Is Nothing
    Do
       Set c = AsrchRng.find("Time", LookIn:=xlValues)
       If Not c Is Nothing Then c.EntireRow.Delete
   Loop While Not c Is Nothing

所以代替c.entirerow.delete只需将其更改为为行着色的行

过去我用这两行来着色一行

iWarnColor = xlThemeColorAccent2
ws.rows(k).Interior.ColorIndex = iWarnColor

使整行变黄。

答案 3 :(得分:0)

这是我目前在宏中所拥有的。

Public Sub HighlightListedValues()
Dim strConcatList As String
Dim cell As Range

'Creates a string concatenating your list of strings, separated by |s
'e.g. "item1|item2|item3|item4|"
For Each cell In Sheets("32281r3|32342r1").Range("E1:E178")
    strConcatList = strConcatList & cell.Value & "|"
Next cell

'For each used cell in Column A of sheet1, check whether the value in that cell
'is contained within the concatenated string
For Each cell In Intersect(Sheets("Gap Analysis").Range("E:E"), Sheets("Gap Analysis").UsedRange)
    If InStr(strConcatList, cell.Value) > 0 Then       'InStr returns 0 if the string isn't found
        cell.EntireRow.Interior.Color = RGB(255, 0, 0) 'Highlights the row in red if value found
    End If
Next cell
End Sub