【发布时间】:2015-12-02 23:57:08
【问题描述】:
我有这个 VBA 代码,用于比较我的 Excel 电子表格中的两列,B 列与 A 列。然后它“突出显示”A 列中缺少但 B 列中的那些。
我想不通的是如何反转搜索列 B 并突出显示 A 列中不同的过程。
原代码:
For i = 2 To LastRow
For j = 2 To LastRow
If Report.Cells(i, 2).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal.
If InStr(1, Report.Cells(j, 1).Value, Report.Cells(i, 2).Value, vbTextCompare) > 0 Then
Report.Cells(i, 2).Interior.Color = xlNone 'Transparent background
Report.Cells(i, 2).Font.Color = RGB(0, 0, 0) 'Black font color
Exit For
Else
Report.Cells(i, 2).Interior.Color = RGB(156, 0, 6) 'Dark red background
Report.Cells(i, 2).Font.Color = RGB(255, 199, 206) 'Light red font color
End If
End If
Next j
Next i
我尝试重命名字母并切换列值并接近但意识到它正在使用原始搜索中的值并且只是突出显示 A 列中的相应单元格。
【问题讨论】:
-
您是否有理由不想使用条件格式来执行此操作?
-
什么是
Report?Worksheet? -
要搭载@ScottCraner,请参阅this question 以获得条件格式解决方案。
-
=And(ISERROR(VLOOKUP(A1,B:B,1,False)) = FALSE, A1 <> "")是我将用于条件格式的公式。 -
我本来打算这样做,但这只是一个更大的 VBA 结构的一部分,它创建了一个精心制作的工作簿解决方案并输出最终用户只需按下按钮即可看到的数据。跨度>