首页 文章

查找多个关键字并突出显示该行

提问于
浏览
-3

在选项卡1中,我有多个行和列(比如列A到列K,至少有1000行) . 在选项卡2中,我列出了关键字(列A和关键字A1-A50) .

我正在尝试找到一个宏,它使用选项卡2中的任何关键字搜索选项卡1,并突出显示找到它的选项卡1中的单元格 .

我该如何为此编写宏?我是宏观世界的新手并且出类拔萃 . 任何帮助都将受到赞赏 .

1 回答

  • 0

    如果您不提供代码或屏幕截图,很难回答 .

    Option Explicit
    
    Sub ColourMatchingCells()
    
    With Thism Tab2range as range
    'Replace Sheet2 below with whatever Tab 2 is named -- and A1:A50 with actual range address.'
    Set Tab2range = .worksheets("Sheet2).range("A1:A50")
    
    Dim cell as range
    
    ' Again, replace Sheet1 name and range address on next line with actual values'
    With .worksheets("Sheet1").range("A1:K1000")
    
    Dim AddressOfFirstMatch as string
    Dim CellFound as range
    Dim MatchAddress as string
    
    For each cell in Tab2range
    
    ' Case insensitive search and which tries to find the whole value. You did not mention what type of data you're searching for e.g. strings, numbers, dates, etc. -- or what type of search you want. '
    
    Set CellFound = .cells.find(what:=cell.value2,lookin:=xlvalues,lookat:=xlwhole,searchdirection:=xlnext,Matchcase:=False)
    
    If not (CellFound is nothing) then
    AddressOfFirstMatch = CellFound.address
    CellFound.interior.color = vbblue 'just an example'
    
    Do
    Set CellFound = .findnext(CellFound)
    CellFound.interior.color = vbblue
    MatchAddress = CellFound.address
    Doevents ' If you experience infinite loop or bad logic, spam/hold Escape or Break key on keyboard'
    Loop until strcomp(AddressOfFirstMatch,MatchAddress, vbbinarycompare) = 0
    
    End if
    
    Next cell
    
    End with
    
    End with
    
    End Sub
    

    它有用吗?它能做你想要的吗?

相关问题