使用Excel VBA比较列并突出显示匹配数据 [英] Comparing Columns and Highlight the Matching Data Using Excel VBA
本文介绍了使用Excel VBA比较列并突出显示匹配数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要比较Sheet 2
中的Column B
和Sheet 3
中的Column C
,并突出显示Column B (Sheet 2)
中具有匹配数据的单元格.
I Need to compare Column B
in Sheet 2
with Column C
in Sheet 3
and highlight the cells with matching data in Column B (Sheet 2)
.
在条件格式
在=NOT(ISNA(VLOOKUP(Sheet3!C,Sheet2!B,1,FALSE)))
格式类型为
特殊颜色说黄色Interior.colorindex = 6
如何在VBA中使用代码实现相同的功能?
How to implement the same using code in VBA?
推荐答案
您可以这样做
Sub CompareAndHighlight()
Dim rng1 As Range, rng2 As Range, i As Long, j As Long
For i = 1 To Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp).Row
Set rng1 = Sheets("Sheet2").Range("B" & i)
For j = 1 To Sheets("Sheet3").Range("C" & Rows.Count).End(xlUp).Row
Set rng2 = Sheets("Sheet3").Range("C" & j)
If StrComp(Trim(rng1.Text), Trim(rng2.Text), vbTextCompare) = 0 Then
rng1.Interior.Color = RGB(255, 255, 0)
End If
Set rng2 = Nothing
Next j
Set rng1 = Nothing
Next i
End Sub
代码对照Sheet3列C中的每个单元格检查Sheet2列B中的所有单元格,如果它们匹配,则以黄色突出显示B列中Sheet2上的单元格
the code checks all cells in Sheet2 column B against each cell from Sheet3 column C and if they match it highlights cells on Sheet2 in Column B in yellow
这篇关于使用Excel VBA比较列并突出显示匹配数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文