创建宏以搜索工作表以查找字符串列表并突出显示该行 [英] Create macro to search Worksheet for list of strings and highlight the row

查看:293
本文介绍了创建宏以搜索工作表以查找字符串列表并突出显示该行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




  • 我正在使用Office 2010。

  • 我不是Excel或VBA,不知道从哪里开始。

  • 我发现的搜索只允许我搜索一个字符串。



非常感谢你。

解决方案

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

'创建一个连接字符串列表的字符串,由| s
'分隔物品1 | ITEM2 |项目3 | ITEM4 |
对于每个单元格In Sheets(List)。Range(A1:A30)
strConcatList = strConcatList& cell.Value& |
下一个单元格

'对于sheet1的列A中的每个使用的单元格,检查该单元格中的值
'是否包含在连接的字符串中
对于每个单元格Intersect(Sheets(Sheet1)。Range(A:A),Sheets(Sheet1)。UsedRange)
如果InStr(strConcatList,cell.Value)> 0 Then'如果没有找到字符串,InStr返回0
cell.EntireRow.Interior.Color = RGB(255,0,0)'如果找到
,则突出显示红色行如果
下一个单元格
End Sub

如果以下内容将突出显示红色的相关行是真的:




  • 与您要搜索的值相匹配的数据位于名为Sheet1的工作表的A列中

  • 您的字符串列表包含在名为List的工作表的单元格A1:A30中



修改工作表的名称和需要的范围例如如果要在名为数据的工作表的列A至C中搜索,您将修改 Sheets(Sheet1)。Range(A:A) code> Sheets(Data)。Range(A:C)



希望这有帮助!

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.

解决方案

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!

这篇关于创建宏以搜索工作表以查找字符串列表并突出显示该行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆