\n
This is the desired outcome:\n
Here is the code
\nSub Fulfillment()\n'\n' Fulfillment Macro\n' Format the order number in column A as plum\n\n\nRange("A2").Select\nRange(Selection, Selection.End(xlDown)).Select\nSelection.FormatConditions.Add Type:=xlExpression, Formula1:= _\n "=MOD(SUM((A$2:A2<>A$1:A1)*1),2)=0"\nSelection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority\nWith Selection.FormatConditions(1).Font.Color = RGB(0, 0, 0)\n \nEnd With\nWith Selection.FormatConditions(1).Interior\n .PatternColorIndex = xlAutomatic\n .Color = RGB(221, 160, 221)\n .TintAndShade = 0\nEnd With\nSelection.FormatConditions(1).StopIfTrue = False\n\nApplication.DisplayAlerts = True \nApplication.ScreenUpdating = True\nEnd Sub\n
\nThank you! I do not necessarily need a conditional formatting solution, just a VBA solution that works dynamically.
\n","author":{"@type":"Person","name":"user2880249"},"upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Option Explicit\n\nSub Fulfillment()\n'\n' Fulfillment Macro\n' Format the order number in column A as plum\n\n Const CriteriaColumn As Long = 1\n\n Dim wb As Workbook: Set wb = ThisWorkbook\n Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1") ' adjust\n \n Dim rg As Range: Set rg = ws.Range("A1").CurrentRegion\n \n Set rg = rg.Resize(rg.Rows.Count - 2).Offset(2) ' exclude first two rows\n \n Application.ScreenUpdating = False\n \n rg.Interior.Color = xlNone\n \n Dim Col As Long: Col = 1\n \n Dim cell As Range\n Dim r As Long\n \n For Each cell In rg.Columns(CriteriaColumn).Cells\n r = r + 1\n If cell.Value <> cell.Offset(-1).Value Then Col = Col Mod 2 + 1\n If Col = 2 Then rg.Rows(r).Interior.Color = RGB(221, 160, 221)\n Next cell\n \n Application.ScreenUpdating = True\n \n MsgBox "Fulfillment accomplished.", vbInformation\n\nEnd Sub\n
\n","author":{"@type":"Person","name":"VBasic2008"},"upvoteCount":1}}}Reputation: 35
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:
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.
Upvotes: 1
Views: 785
Reputation: 54973
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
Upvotes: 1