VBA Conditional formatting, if loop

frustrated_macro

New Member
Joined
Sep 4, 2019
Messages
39
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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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

Forum statistics

Threads
1,215,139
Messages
6,123,259
Members
449,093
Latest member
Vincent Khandagale

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