Conditional formatting via VBA

handysmurf

Board Regular
Joined
Jan 17, 2015
Messages
114
Office Version
  1. 365
Platform
  1. Windows
I need to convert these conditional formatting rules to VBA.

1721941414263.png
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You can record a macro from the steps of defining conditional formatting. Since you already have the definitions, follow the following:
1. Select cell C8
2. Enable macro recording (in tab Developer)
3. On the Home tab, select Conditional Formatting/Rule Management, from the drop-down list select This sheet.
4. In the bottom list you have definitions, select the last one and choose the Edit Rule button (or double-click in the row), then do not change anything in the new window and immediately select the OK button.
5 Follow the same procedure as in point 4 with the next rules moving up the list.
6 Finally, close the Rule Manager window by pressing the OK button.
7. Finish recording the macro.
8. Go to the VBA editor , find the recorded macro and remove all occurrences ( if present) in the code:
Code:
Range("C8").Activate

Artik
 
Upvote 0
Had not even thought of that.... I could also clear the formatting and then reset it ... all in the same one


Thanks!!!
 
Upvote 0
uggghhhh .... it doesn't record what happens in the conditional formatting GUI
 
Upvote 0
It looks like you need to buy a new recorder. ;)

Below is an excerpt from the recorded conditional formatting procedure.
VBA Code:
Sub Makro4()
'
' Makro4 Makro
'
    Cells.FormatConditions.Delete
    
    Range("C7:D25,F7:F25").Select
    Range("C8").Activate
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=AND($C8<=90,$D8<=60)"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 10092543
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Range("C8:D25,F8:F25").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=OR($C8=""Meds"",$C8=""P.M.Meds"",$C8=""A.M.Meds"",$C8=""Wake"",$C8=""Sleep"")"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).StopIfTrue = True
    Range("C8:D25,F8:F25").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=ISBLANK($C8)"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).StopIfTrue = True
    Range("A8:F25").Select
    Range("C8").Activate
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$A8<>$A9"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Borders(xlBottom)
        .LineStyle = xlContinuous
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub
And this is the revised code to optimize it a bit.
VBA Code:
Sub Makro4_1()
    Cells.FormatConditions.Delete

    With Range("C7:D25001,F7:F25001")
        .FormatConditions.Add Type:=xlExpression, Formula1:= _
                              "=AND($C8<=90,$D8<=60)"
        .FormatConditions(.FormatConditions.Count).SetFirstPriority

        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 10092543
            .TintAndShade = 0
        End With
        .FormatConditions(1).StopIfTrue = False
    End With

    With Range("C8:D25001,F8:F25001")
        .FormatConditions.Add Type:=xlExpression, Formula1:= _
                              "=OR($C8=""Meds"",$C8=""P.M.Meds"",$C8=""A.M.Meds"",$C8=""Wake"",$C8=""Sleep"")"
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        .FormatConditions(1).StopIfTrue = True
    End With

    With Range("C8:D25001,F8:F25001")
        .FormatConditions.Add Type:=xlExpression, Formula1:= _
                              "=ISBLANK($C8)"
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        .FormatConditions(1).StopIfTrue = True
    End With

    With Range("A8:F25001")
        .FormatConditions.Add Type:=xlExpression, Formula1:="=$A8<>$A9"
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        With .FormatConditions(1).Borders(xlBottom)
            .LineStyle = xlContinuous
            .TintAndShade = 0
            .Weight = xlThin
        End With
        .FormatConditions(1).StopIfTrue = False
    End With
End Sub

It may not matter much to you, but for users of non-English language versions, I remind you that when defining conditional formatting formulas with code, you should use the local format notation, not English.

Artik
 
Upvote 0
Solution
Ok so I'm working with what you gave me ... trying to get it to work. One step at a time. I have the one that lines the days done and working. So ... I'm stuck at the colors. How do I put #F2CEEF into the VBA?
 
Upvote 0
Ok ... baby steps ... I have the color using rgb but I'm working on making it lighter if anyone wants to share any hints.
 
Upvote 0
Ok ... I got it ... had to build each step manually one by one ... but your code help a lot

THANKS!!
 
Upvote 0
Also got it to copy and paste formulas automatically and do it all on save.... shrug ... anyway
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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