VBA 比较单行值,如果不同则突出显示整行

VBA Compare single row values and highlight the entire row if different

我的代码使用条件格式来查看 A 列“订单 ID”中的行值,比较它们,然后在行值不同时格式化单元格。我如何根据 A 列“订单 ID”中的连续行值不同来格式化整行,而不是格式化单元格?

换句话说 - 如果 A 列“订单 ID”中的值与 A 列“订单 ID”中的先前值不同,我想格式化不同的整行。我的数据每天都在变化,所以我需要使用 VBA!

这是我当前代码的输出:

这是期望的结果:

这是代码

Sub Fulfillment()
'
' Fulfillment Macro
' Format the order number in column A as plum


Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=MOD(SUM((A:A2<>A:A1)*1),2)=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font.Color = RGB(0, 0, 0)
    
End With
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = RGB(221, 160, 221)
    .TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

Application.DisplayAlerts = True 
Application.ScreenUpdating = True
End Sub

谢谢!我不一定需要条件格式解决方案,只需要一个动态工作的 VBA 解决方案。

不同风格的带状行

Option Explicit

Sub Fulfillment()
'
' Fulfillment Macro
' Format the order number in column A as plum

    Const CriteriaColumn As Long = 1

    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1") ' adjust
    
    Dim rg As Range: Set rg = ws.Range("A1").CurrentRegion
    
    Set rg = rg.Resize(rg.Rows.Count - 2).Offset(2) ' exclude first two rows
    
    Application.ScreenUpdating = False
    
    rg.Interior.Color = xlNone
    
    Dim Col As Long: Col = 1
    
    Dim cell As Range
    Dim r As Long
    
    For Each cell In rg.Columns(CriteriaColumn).Cells
        r = r + 1
        If cell.Value <> cell.Offset(-1).Value Then Col = Col Mod 2 + 1
        If Col = 2 Then rg.Rows(r).Interior.Color = RGB(221, 160, 221)
    Next cell
    
    Application.ScreenUpdating = True
    
    MsgBox "Fulfillment accomplished.", vbInformation

End Sub