【问题标题】:VBA macro to compare two columns and color highlight cell differencesVBA宏比较两列并颜色突出显示单元格差异
【发布时间】:2020-09-29 01:26:49
【问题描述】:

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

编辑 我试图让这个功能做的是: 1. 突出 ColA 中与 ColB 不同或不同的单元格 2. 高亮 ColB 中与 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

【问题讨论】:

  • 完全摆脱 VBA 并使用 XL 强大的 Conditional Formatting 功能怎么样?另外,也许这更适合Code Review
  • @ScottHoltzman 该功能是否适用于所有版本?
  • @njk -> 好问题。确实如此,但 07/10 中的功能比 03 更强大。不过,我不确定 07/10 中的差异。
  • @ScottHoltzman 我不知道代码审查网站。以后我会在那里发帖,见谅。另外,我刚刚注意到我的代码中有一个错误,它跳过了单元格。我怀疑它击中了 Exit For 并绕过了两个 For 循环,而不仅仅是内部循环。
  • 是的,您的Exit For 将退出原来的For。但是,目前还不清楚您要突出显示的不同之处,因为您循环遍历 B 列中的每个单元格以获取 A 列中的每个单元格,然后对相反方向执行相同操作,因此您的颜色可能会在过程,取决于你的价值观。你能用更多的非代码描述来编辑你的帖子吗?

标签: excel vba


【解决方案1】:

啊,是的,我整天都在做蛋糕。实际上,您的代码看起来很像我这样做的方式。虽然,我选择使用整数循环而不是使用“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

我做了不同的事情:

  1. 我使用了上述整数方法(与“for each”方法相反)。
  2. 我将工作表定义为对象变量。
  3. 我在 InStr 函数中使用了 vbTextCompare 而不是它的数值。
  4. 我添加了一个 if 语句来省略空白单元格。提示:即使只有一个 工作表中的列超长(例如,单元格 D5000 不小心被 格式化),那么所有列的 usedrange 被认为是 5000。
  5. 我使用 rgb 代码作为颜色(这对我来说更容易,因为我 在这个隔间里我旁边的墙上有一张备忘单 哈哈)。

嗯,总结一下。祝你的项目好运!

【讨论】:

    【解决方案2】:

    '比较两列并突出差异

        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
    

    '希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-25
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 2013-11-29
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多