首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel InStr函数相反方向

Excel InStr函数相反方向
EN

Stack Overflow用户
提问于 2015-11-30 18:48:28
回答 2查看 323关注 0票数 1

我使用这个VBA代码来比较我的Excel电子表格中的两列,B列和A列,然后它“突出”了A列中缺少但在B列中缺少的那些列。

我想不出的是,如何反向搜索B列,并突出显示A列中不同的部分。

原始守则:

代码语言:javascript
复制
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中的相应单元格。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-30 20:21:48

回答你的问题:

代码语言:javascript
复制
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

如果要使用条件格式(使颜色更改处于活动状态),则可以用以下两个循环替换这两个循环:

代码语言:javascript
复制
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。

代码语言:javascript
复制
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。

票数 1
EN

Stack Overflow用户

发布于 2015-11-30 20:34:31

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

一个快速解决办法是:

代码语言:javascript
复制
    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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34005743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档