繁体   English   中英

Excel VBA突出显示行和列与2张纸匹配的单元格

[英]Excel VBA highlight cell where Rows and Column Match with 2 sheets

我有的床单

大家好,我在 sheet1 中有两列有多行,在 sheet2 中有相同的列,但在 sheet2 中,第二列的值成为列标题,我想突出显示 sheet2 中与 sheet1 匹配的单元格。 在此先感谢,我希望它说清楚。 还附上了截图。 我写了以下代码,但这不能正常工作。 谢谢你的帮助

Public Sub test3()

Dim rng As Range
Dim aNumber As Range
Dim bNumber As Range
Dim rng2 As Range
Dim LastColumn As Long
Dim iRow As Long
Dim iCol As Long
Set rng = Sheets("Sheet2").Range("B2:B" & Range("B" & Rows.Count).End(xlUp).row)
LastColumn = Sheets("Sheet2").Range("D1").CurrentRegion.Columns.Count
Set rng2 = Sheets("Sheet2").Range(Cells(1, 1), Cells(1, LastColumn))
'MsgBox rng2.Address
For iRow = 2 To 6 'this i need last row count
For iCol = 2 To 6 ''this i need last row count
    Set aNumber = Sheets("Sheet1").Cells(iRow, 1) 'Row, Column Searching A
    Set bNumber = Sheets("Sheet1").Cells(iCol, 2) 'Row, Column Searching B

    If Application.WorksheetFunction.CountIf(rng, aNumber) > 0 Then
    If Application.WorksheetFunction.CountIf(rng2, bNumber) > 0 Then

        ColNum = Application.WorksheetFunction.Match(bNumber, rng2, 0)
        RowNum = Application.WorksheetFunction.Match(aNumber, rng, 0)
        'MsgBox (RowNum + 1)
        'MsgBox (ColNum)
        Sheets("Sheet2").Cells(RowNum + 1, ColNum).Interior.Color = vbGreen
    Else
        'MsgBox aNumber & " does not exist in range " & rng.Address
    End If
    End If
Next iCol
Next iRow
End Sub

使用辅助列 C ( =CONCAT($A2,$B2) ),您可以使用条件格式来执行此操作。

设置您的帮助栏:

在此处输入图片说明

然后,设置新的条件格式规则。

规则:

=IF(ISERROR(INDEX($C$3:$C$8,MATCH(CONCAT($E3,F$2),$C$3:$C$8,0))),FALSE,TRUE)

适用范围:

=$F$3:$K$8

您可能需要调整这些范围,但这对我有用:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM