1

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

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

非常感谢你。

4

4 回答 4

2
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 中

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

希望这可以帮助!

于 2014-06-26T13:10:58.313 回答
1

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

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

问候,

帕提

下面的代码:

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

结束子

于 2015-03-10T12:21:09.780 回答
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

这使整行变黄。

于 2014-06-26T12:41:26.210 回答
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
于 2014-06-27T13:46:13.970 回答