Excel InStr函数的相反方向

我有这个VBA代码,用于比较Excel电子表格中列B到列A的两列。然后“突出显示”列A中但列B中缺less的那些代码。

我想不出的是如何颠倒过程来searchB列,并突出显示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 

我已经尝试重命名字母和切换列值,并接近,但意识到它正在使用原始search的值,只是突出显示列A中的相应单元格。

回答你的问题:

 For j = 2 To LastRow For i = 2 To LastRow If Report.Cells(j, 1).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(i, 2).Value, Report.Cells(j, 1).Value, vbTextCompare) > 0 Then Report.Cells(j, 1).Interior.Color = xlNone 'Transparent background Report.Cells(j, 1).Font.Color = RGB(0, 0, 0) 'Black font color Exit For Else Report.Cells(j, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background Report.Cells(j, 1).Font.Color = RGB(255, 199, 206) 'Light red font color End If End If Next i Next j 

如果你想使用条件格式,使颜色变化生活你可以用两个循环代替:

 With Report.Range("A2:A" & LastRow).FormatConditions .Delete With .Add(Type:=xlExpression, Formula1:="=And(iserror(Vlookup(A2,B:B,1,False)),A2<>"""")") .Font.Color = RGB(255, 199, 206) .Interior.Color = RGB(156, 0, 6) End With End With With Report.Range("B2:B" & LastRow).FormatConditions .Delete With .Add(Type:=xlExpression, Formula1:="=And(iserror(Vlookup(B2,A:A,1,False)),B2<>"""")") .Font.Color = RGB(255, 199, 206) .Interior.Color = RGB(156, 0, 6) End With End With 

编辑问题是,列A中的数据在最后有一个额外的空间,从而使instr返回false。

 For j = 2 To LastRow Report.Cells(j, 1).Value = Trim(Report.Cells(j, 1).Value) For i = 2 To LastRow Report.Cells(i, 2).Value = Trim(Report.Cells(i, 2).Value) If Report.Cells(j, 1).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(i, 2).Value, Report.Cells(j, 1).Value, vbTextCompare) > 0 Then Report.Cells(j, 1).Interior.Color = xlNone 'Transparent background Report.Cells(j, 1).Font.Color = RGB(0, 0, 0) 'Black font color Exit For Else Report.Cells(j, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background Report.Cells(j, 1).Font.Color = RGB(255, 199, 206) 'Light red font color End If End If Next i Next j 

通过修改值,instr返回true。

有很多方法可以做到这一点。 你可以使用公式,你可以创build字典。

快速解决scheme将是:

  Dim stringCount As Integer Dim myString As String Dim col1Range As Range Dim col2Range As Range Set col1Range = Report.Range("A1") Set col2Range = Report.Range("B1") For i = 1 To LastRow myString = col1Range.Offset(i).Value If myString <> "" Then stringCount = WorksheetFunction.CountIf(Range("B:B"), myString) If (stringCount = 0) Then col1Range.Offset(i).Interior.Color = RGB(156, 0, 6) 'Dark red background col1Range.Offset(i).Font.Color = RGB(255, 199, 206) 'Light red font color Else col1Range.Offset(i).Interior.Color = xlNone 'Transparent background col1Range.Offset(i).Font.Color = RGB(0, 0, 0) 'Black font color End If End If Next i For j = 1 To LastRow myString = col2Range.Offset(j).Value If myString <> "" Then stringCount = WorksheetFunction.CountIf(Range("A:A"), myString) If (stringCount = 0) Then col2Range.Offset(j).Interior.Color = RGB(156, 0, 6) 'Dark red background col2Range.Offset(j).Font.Color = RGB(255, 199, 206) 'Light red font color Else col2Range.Offset(j).Interior.Color = xlNone 'Transparent background col2Range.Offset(j).Font.Color = RGB(0, 0, 0) 'Black font color End If End If Next j