VBA Conditional formatting, if loop

frustrated_macro

New Member
Joined
Sep 4, 2019
Messages
41
Office Version
  1. 365
Platform
  1. Windows
i need to create a macro that basically does this kind of conditional formatting

1711118227441.png




the range will be G2:G & LAstRow, which has already been defined, the range in the screenshot was just me testing that all the colors worked
but i have no idea how to write that into the macro. i had a very sad attempt at an IF loop, but im not sure how to properly write the formula AND the formatting into the loop.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
And if you only increase the range of cells to apply, for example, assuming that you will have a growth range up to 10000 data

Excel Formula:
=$G$2:$G$10000
 
Upvote 0
And if you only increase the range of cells to apply, for example, assuming that you will have a growth range up to 10000 data

Excel Formula:
=$G$2:$G$10000
Right, im not too worried about the range. what i need help with is how to convert that formatting (the formula and the color) into VBA code so i can put it in my macro
 
Upvote 0
Try this:


VBA Code:
Sub Macro1()
  Dim lr As Long
 
  lr = Range("G" & Rows.Count).End(3).Row
 
  Cells.FormatConditions.Delete
  '
  With Range("G2:G" & lr)
 
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND($K2>$G2,G2>0,G2>0)"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5263615
        .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False

    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND($G2>$K2)"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13421823
        .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False
   
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND($K2=$G2,G2>0)"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13434879
        .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False
 
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND($K2=0,G2>0)"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 11854022
        .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False
  End With
End Sub


😇
 
Upvote 0
Solution
Try this:


VBA Code:
Sub Macro1()
  Dim lr As Long
 
  lr = Range("G" & Rows.Count).End(3).Row
 
  Cells.FormatConditions.Delete
  '
  With Range("G2:G" & lr)
 
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND($K2>$G2,G2>0,G2>0)"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5263615
        .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False

    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND($G2>$K2)"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13421823
        .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False
  
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND($K2=$G2,G2>0)"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13434879
        .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False
 
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND($K2=0,G2>0)"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 11854022
        .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False
  End With
End Sub


😇
THIS WORKEDDDDDDDDDDD THANK YOUUUUUUUUUUUUUUUUUUUUUUU
 
Upvote 0
One minor comment. The statement deleting any existing format conditions:
VBA Code:
  Cells.FormatConditions.Delete

will delete all format conditions in the worksheet, and if that's what you want , then all is good. However if you have other format conditions on the worksheet that you do not want to delete then it is better to restrict the delete statement to just the range of cells that you are formatting.

VBA Code:
  With Range("G2:G" & lr)
    .FormatConditions.Delete

    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND($K2>$G2,G2>0,G2>0)"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
 
Upvote 0

Forum statistics

Threads
1,215,819
Messages
6,127,045
Members
449,356
Latest member
tstapleton67

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top