【问题标题】:Excel InStr Function opposite directionExcel InStr 函数反方向
【发布时间】: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 列中的相应单元格。

【问题讨论】:

  • 您是否有理由不想使用条件格式来执行此操作?
  • 什么是ReportWorksheet?
  • 要搭载@ScottCraner,请参阅this question 以获得条件格式解决方案。
  • =And(ISERROR(VLOOKUP(A1,B:B,1,False)) = FALSE, A1 &lt;&gt; "") 是我将用于条件格式的公式。
  • 我本来打算这样做,但这只是一个更大的 VBA 结构的一部分,它创建了一个精心制作的工作簿解决方案并输出最终用户只需按下按钮即可看到的数据。跨度>

标签: vba function excel


【解决方案1】:

回答你的问题:

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。

【讨论】:

  • 我已经复制并粘贴了,它为每列中的所有单元格着色。
  • @JustinTriadTechx 因此,由于我的方法和 jspek 两种方法,我会假设您的数据并不相同。您能否在原始帖子中发布正在测试的数据示例?按照你所拥有的,他们应该工作。
  • 这与我从外部来源提取数据有什么关系吗?我不能分享它,因为它是机密信息。我将尝试创建一个虚拟工作表并分享它。
  • 这里是一个与其他数据完全相同的虚拟数据库设置的链接:drive.google.com/…
  • 我需要与您正在测试的数据非常匹配的数据。一个空的工作表模板对我没有好处。问题在于您尝试测试的数据。
【解决方案2】:

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

一个快速的解决方案是:

    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

【讨论】:

  • 我已经复制并粘贴了,它为每列中的所有单元格着色。
猜你喜欢
  • 1970-01-01
  • 2016-01-16
  • 2017-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-18
  • 1970-01-01
  • 2013-07-09
相关资源
最近更新 更多