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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24430130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:23:53  来源:igfitidea点击:

Create macro to search Worksheet for list of strings and highlight the row

excel-vbaexcel-2010string-searchvbaexcel

提问by user2985424

Can someone help me create a macro that will search an Excel worksheet for a list of 30 strings (e.g., SV-32488r1, SV-33485r1) and highlight the Rowwhen found?

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

  • I am using Office 2010.
  • I am not a Excel or VBA wiz so I have no idea where to start.
  • The searches I have found only allow me to search for one string.
  • 我正在使用 Office 2010。
  • 我不是 Excel 或 VBA 专家,所以我不知道从哪里开始。
  • 我找到的搜索只允许我搜索一个字符串。

Thank you so much in advance.

非常感谢你。

采纳答案by Aiken

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

This will highlight the relevant rows in red if the following is true:

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

  • The data matching the values you are searching for are is in column A of a worksheet named "Sheet1"
  • Your list of strings is contained in cells A1:A30 of a worksheet named "List"
  • 与您要搜索的值匹配的数据位于名为“Sheet1”的工作表的 A 列中
  • 您的字符串列表包含在名为“列表”的工作表的单元格 A1:A30 中

Amend the names of worksheets and ranges as needed e.g. If you want to search in Columns A to C of a worksheet named "Data" you would amend Sheets("Sheet1").Range("A:A")to Sheets("Data").Range("A:C")

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

Hope this helps!

希望这可以帮助!

回答by Parthiban C

I have created macro to find the list of words and highlight the cell. when i use the code it is also highlighting empty cells inbetween the values. also i required the code to find incase the cell contain the word inbetween the words.

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

Example: I created to find and highlight the word "Shev" in the cells. it should find and highlight if it is in middle of cell like " was shev in ".

示例:我创建以在单元格中查找并突出显示单词“Shev”。它应该找到并突出显示它是否在像“ was shev in ”这样的单元格中间。

Regards,

问候,

Parthi

帕蒂

Code below:

代码如下:

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

结束子

回答by user2985424

This is what I currently have in the macro.

这是我目前在宏中的内容。

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

回答by user3271518

Untested the part that colors it in but this should give you a good start

未测试为其着色的部分,但这应该会给您一个良好的开端

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

Here is what I used in the past to delete rows that contain specific texts

这是我过去用来删除包含特定文本的行的方法

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

so instead of c.entirerow.delete just change it to the line that colors the row

因此,而不是 c.entirerow.delete 只需将其更改为为行着色的行

in the past I have used these 2 lines to color a row

过去我用这两条线给一行上色

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

that makes the entire row yellow.

这使整行变黄。