Excel VBA宏用于比较两列和颜色高亮显示单元格差异

Excel VBA宏用于比较两列和颜色高亮显示单元格差异,excel,vba,Excel,Vba,我想用颜色突出显示彼此不同的单元格;在这种情况下,可乐和可乐。这个函数可以满足我的需要,但看起来重复、丑陋且效率低下。我不太精通VBA编码;有没有更优雅的方法来编写这个函数 编辑 我想让这个函数做的是: 1.突出显示ColA中与ColB不同或不相同的细胞 2.突出显示ColB中与ColA不同或不在ColA中的细胞 Sub compare_cols() Dim myRng As Range Dim lastCell As Long 'Get the last r

我想用颜色突出显示彼此不同的单元格;在这种情况下,可乐和可乐。这个函数可以满足我的需要,但看起来重复、丑陋且效率低下。我不太精通VBA编码;有没有更优雅的方法来编写这个函数

编辑 我想让这个函数做的是: 1.突出显示ColA中与ColB不同或不相同的细胞 2.突出显示ColB中与ColA不同或不在ColA中的细胞

    Sub compare_cols()

    Dim myRng As Range
    Dim lastCell As Long

    'Get the last row
    Dim lastRow As Integer
    lastRow = ActiveSheet.UsedRange.Rows.Count

    'Debug.Print "Last Row is " & lastRow

    Dim c As Range
    Dim d As Range

    Application.ScreenUpdating = False

    For Each c In Worksheets("Sheet1").Range("A2:A" & lastRow).Cells
        For Each d In Worksheets("Sheet1").Range("B2:B" & lastRow).Cells
            c.Interior.Color = vbRed
            If (InStr(1, d, c, 1) > 0) Then
                c.Interior.Color = vbWhite
                Exit For
            End If
        Next
    Next

    For Each c In Worksheets("Sheet1").Range("B2:B" & lastRow).Cells
        For Each d In Worksheets("Sheet1").Range("A2:A" & lastRow).Cells
            c.Interior.Color = vbRed
            If (InStr(1, d, c, 1) > 0) Then
                c.Interior.Color = vbWhite
                Exit For
            End If
        Next
    Next

Application.ScreenUpdating = True

End Sub

啊,是的,那是蛋糕,我整天都在做。事实上,你的代码看起来和我做的很像。尽管如此,我还是选择使用整数循环,而不是使用“For Each”方法。我在代码中看到的唯一潜在问题是,ActiveSheet可能并不总是“Sheet1”,而且InStr已知会给出一些有关vbTextCompare参数的问题。使用给定的代码,我将其更改为以下内容:

Sub compare_cols()

    'Get the last row
    Dim Report As Worksheet
    Dim i As Integer, j As Integer
    Dim lastRow As Integer

    Set Report = Excel.Worksheets("Sheet1") 'You could also use Excel.ActiveSheet _
                                            if you always want this to run on the current sheet.

    lastRow = Report.UsedRange.Rows.Count

    Application.ScreenUpdating = False

    For i = 2 To lastRow
        For j = 2 To lastRow
            If Report.Cells(i, 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(j, 2).Value, Report.Cells(i, 1).Value, vbTextCompare) > 0 Then
                    'You may notice in the above instr statement, I have used vbTextCompare instead of its numerical value, _
                    I find this much more reliable.
                    Report.Cells(i, 1).Interior.Color = RGB(255, 255, 255) 'White background
                    Report.Cells(i, 1).Font.Color = RGB(0, 0, 0) 'Black font color
                    Exit For
                Else
                    Report.Cells(i, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background
                    Report.Cells(i, 1).Font.Color = RGB(255, 199, 206) 'Light red font color
                End If
            End If
        Next j
    Next i

    'Now I use the same code for the second column, and just switch the column numbers.
    For i = 2 To lastRow
        For j = 2 To lastRow
            If Report.Cells(i, 2).Value <> "" Then
                If InStr(1, Report.Cells(j, 1).Value, Report.Cells(i, 2).Value, vbTextCompare) > 0 Then
                    Report.Cells(i, 2).Interior.Color = RGB(255, 255, 255) 'White 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

Application.ScreenUpdating = True

End Sub
Sub compare_cols()
“坐到最后一排
作为工作表的Dim报告
尺寸i为整数,j为整数
将最后一行设置为整数
设置Report=Excel.Worksheets(“Sheet1”)”也可以使用Excel.ActiveSheet_
如果您始终希望在当前工作表上运行此操作。
lastRow=Report.UsedRange.Rows.Count
Application.ScreenUpdating=False
对于i=2到最后一行
对于j=2到最后一行
如果Report.Cells(i,1).Value为“”,则这将忽略末尾的空白单元格(如果列长度不相等)。
如果InStr(1,Report.Cells(j,2).Value,Report.Cells(i,1).Value,vbTextCompare)大于0,则
'您可能会注意到,在上面的instr语句中,我使用了vbTextCompare而不是其数值_
我觉得这更可靠。
Report.Cells(i,1).Interior.Color=RGB(255,255,255)“白色背景
Report.Cells(i,1).Font.Color=RGB(0,0,0)'黑色字体颜色
退出
其他的
Report.Cells(i,1).Interior.Color=RGB(156,0,6)“深红色背景
Report.Cells(i,1).Font.Color=RGB(255,199,206)浅红色字体颜色
如果结束
如果结束
下一个j
接下来我
'现在我对第二列使用相同的代码,只需切换列号即可。
对于i=2到最后一行
对于j=2到最后一行
如果报告.Cells(i,2).Value“”,则
如果InStr(1,Report.Cells(j,1).Value,Report.Cells(i,2).Value,vbTextCompare)>0,那么
Report.Cells(i,2).Interior.Color=RGB(255,255,255)“白色背景
Report.Cells(i,2).Font.Color=RGB(0,0,0)'黑色字体颜色
退出
其他的
Report.Cells(i,2).Interior.Color=RGB(156,0,6)“深红色背景
Report.Cells(i,2).Font.Color=RGB(255,199,206)浅红色字体颜色
如果结束
如果结束
下一个j
接下来我
Application.ScreenUpdating=True
端接头
我做了一些不同的事情:

  • 我使用了上面描述的整数方法(与“for each”方法相反)
  • 我将工作表定义为一个对象变量
  • 我在InStr函数中使用了vbTextCompare,而不是它的数值
  • 我添加了一个if语句来省略空白单元格。提示:即使只有一个 表中的列过长(例如,单元格D5000意外损坏 格式),则所有列的usedrange被视为5000
  • 我使用了rgb代码的颜色(这只是对我来说更容易,因为我 把一张备忘单别在我隔壁的墙上 哈哈)

  • 好吧,这就是总结。祝你的项目好运!

    '

        Sub CompareandHighlight()
    
    
    
            Dim n As Integer
            Dim valE As Double
            Dim valI As Double
            Dim i As Integer
    
            n = Worksheets("Indices").Range("E:E").Cells.SpecialCells(xlCellTypeConstants).Count
            Application.ScreenUpdating = False
    
            For i = 2 To n
            valE = Worksheets("Indices").Range("E" & i).Value
            valI = Worksheets("Indices").Range("I" & i).Value
    
                If valE = valI Then
    
                Else:
    
                   Worksheets("Indices").Range("E" & i).Font.Color = RGB(255, 0, 0)
    
                End If
            Next i
    
    
        End Sub
    

    “我希望这能帮助你

    完全摆脱VBA,只使用XL强大的
    条件格式
    功能如何?另外,也许这更适合@ScottHoltzman,这项功能在所有版本中都可用吗?@njk->好问题。是的,但是07/10中的功能比03更强大。我不确定区别然而,在07/10,我突然想到了。@ScottHoltzman我不知道代码审查网站。我以后会在那里发布,抱歉。另外,我刚刚注意到我的代码中有一个错误,它跳过了单元格。我怀疑它击中了For的出口,并绕过了For循环,而不仅仅是内部循环。是的,Exit For将退出原点al
    用于
    。但是,不太清楚您想要突出显示哪些内容是不同的,因为您在B列中的每个单元格中为A列中的每个单元格循环,然后对相反方向执行相同操作,因此您的颜色可能会根据您的值在过程中多次更改。您是否可以使用一些非代码描述来编辑您的帖子你想要做的事情的描述。