Conditional Formatting Macro Help

ponderance

New Member
Joined
May 17, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
I used to have a macro that did the following formulas automatically - I feel like a part of my brain was overwritten with something else because VBA looks like a foreign language to me these days

=$G1>”H*" (the backgrounds in rows with this would be red.)

=$N1>"PS*" (the backgrounds in rows with this would be green.)

=$N1>"CS*" (the backgrounds in rows with this would be green.)

=$AC2<1 (the backgrounds in rows with this would be light grey.)

seems so simple, but i can't figure out how to do it with all the google and youtube trials i've done. right now i have a macro to sort by fields, a macro to color backgrounds, manually select, and get through it that way. I might have had individual macros for each of the 4 color-by-value portions, but it's going on 3 years now.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
One example.

VBA Code:
Sub SetFormatConditionsExample()
    Dim FCRange As Range
    Dim FormulaStr As String
    Dim WS As Worksheet
    
    Set WS = ActiveSheet                    'define worksheet containing format conditions.
    
    'a) Define the range for conditional formatting
    With WS
        Set FCRange = .Range("A1:A10").EntireRow  'this is an example. Modify to use your own range.
    End With
    
    With FCRange.FormatConditions
        'b1) Delete the existing format conditions
        .Delete

        'c1) Define formula1
        FormulaStr = "=$G1 > " & """" & "H*" & """"         'Any formula that resolves to True or False"
        Debug.Print FormulaStr
        
        'd1) Add the new FormatCondition and define the formatting
        With .Add(Type:=xlExpression, Formula1:=FormulaStr)
            .StopIfTrue = True
            .Interior.Color = vbRed
        End With
        
        'c2) Define formula2
        FormulaStr = "=$N1 > " & """" & "PS*" & """"         'Any formula that resolves to True or False"
        Debug.Print FormulaStr
        
        'd2) Add the new FormatCondition and define the formatting
        With .Add(Type:=xlExpression, Formula1:=FormulaStr)
            .StopIfTrue = True
            .Interior.Color = vbGreen
        End With
        
        'c3) Define formula3
        FormulaStr = "=$N1 > " & """" & "CS*" & """"         'Any formula that resolves to True or False"
        Debug.Print FormulaStr
        
        'd3) Add the new FormatCondition and define the formatting
        With .Add(Type:=xlExpression, Formula1:=FormulaStr)
            .StopIfTrue = True
            .Interior.Color = vbGreen
        End With
        
        'c4) Define formula4
        FormulaStr = "=$AC1 > 1"         'Any formula that resolves to True or False"
        Debug.Print FormulaStr
        
        'd4) Add the new FormatCondition and define the formatting
        With .Add(Type:=xlExpression, Formula1:=FormulaStr)
            .StopIfTrue = True
            .Interior.Color = RGB(211, 211, 211) 'light gray
        End With
        
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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