VBA 比较单行值并突出显示整行(如果不同)

发布于 2025-01-09 06:44:23 字数 1252 浏览 1 评论 0原文

我的代码使用条件格式来查看 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$2:A2<>A$1: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 解决方案。

My code uses conditional formatting to look at the row values in Column A "Order ID", compares them, and then formats the cell if the row values are different. Instead of formatting the cell, how do I format the entire row based off of consecutive row values in Column A "Order ID" being different?

Said differently - if the value in Column A "Order ID" is different from the previous value in Column A "Order ID", I want to format the entire row that is different. My data is variable everyday so I need to use VBA!

Here is the output of my current code:
enter image description here

This is the desired outcome:
enter image description here

Here is the code

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$2:A2<>A$1: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

Thank you! I do not necessarily need a conditional formatting solution, just a VBA solution that works dynamically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一绘本一梦想 2025-01-16 06:44:23

不同风味的带状行

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

A Different Flavor of Banded Rows

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文