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

瘦欲@ 提交于 2019-12-24 11:46:56

问题


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 Row when found?

  • 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.

Thank you so much in advance.


回答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

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"

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")

Hope this helps!




回答2:


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 ".

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




回答3:


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

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.




回答4:


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


来源:https://stackoverflow.com/questions/24430130/create-macro-to-search-worksheet-for-list-of-strings-and-highlight-the-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!