Convert conditional formatting into a Macro?

donnabee

New Member
Joined
Jan 30, 2023
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hi all.
ConditionalFormattingCORRECT.jpg


Running latest version of Excel through Microsoft 365 using Windows 10 Pro.

I am running conditional formatting in my workbook with varying degrees of success. Sometimes it's great and has zero problems, then suddenly the conditions have changed themselves randomly and I don't know why - Google tells me this is a common problem? By changed I mean it has added more rules in the conditional formatting - rules I have not asked for not put in there myself - or added some more numbers and ranges onto my current conditions.

It's brilliant when it works, but when it changes I only get half the line coloured in or something else "breaks" in the conditions.

As you can see from the photo attached, the formula is relatively simple.
Color the line from A2 to AC20550 with either green, yellow or red - dependant on if number 1, 2 or 3 is typed into column AB
1 = green (hex #C6EFCE)
2 = yellow (hex #FFEB9C)
3 - red (hex #FFC7CE)
(This colouring also then has it's own macro assigned to perform another set of steps - but that is irrelevant to what I am trying to achieve here.)

I Googled if assigning a Macro was a possibility, but it appears it is, though I quickly became lost.
I edited an example Macro I found with a tutorial, and it just won't work:

Sub formatting()

'Defining the variables:
Dim rng As Range
Dim condition1 As FormatCondition, condition2 As FormatCondition, condition3 As FormatCondition

'Fixing/Setting the range on which conditional formatting is to be desired
Set rng = Range("A2", "AC20550")

'Defining and setting the criteria for each conditional format
Set condition1 = rng.FormatConditions.Add(xlCellValue, xlEqual, "=1")
Set condition2 = rng.FormatConditions.Add(xlCellValue, xlEqual, "=2")
Set condition3 = rng.FormatConditions.Add(xlCellValue, xlEqual, "=3")

'Defining and setting the format to be applied for each condition
With condition1
.Font.Color = #C6EFCE
End With

With condition2
.Font.Color = #FFEB9C
End With

With condition3
.Font.Color = #FFC7CE
End With

End Sub

Any assistance is gratefully accepted, thank you :)
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try this:
VBA Code:
Sub formatting()

'Defining the variables:
    Dim rng As Range
    Dim condition1 As FormatCondition, condition2 As FormatCondition, condition3 As FormatCondition

    'Fixing/Setting the range on which conditional formatting is to be desired
    Set rng = Range("A2", "AC20550")

    rng.FormatConditions.Delete                       ' clear existing format conditions

    'Defining and setting the criteria for each conditional format
    Set condition1 = rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=$AB2=1")
    Set condition2 = rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=$AB2=2")
    Set condition3 = rng.FormatConditions.Add(Type:=xlExpression, Formula1:="=$AB2=3")

    'Defining and setting the format to be applied for each condition
    With condition1
        .Font.Color = RGB(86, 153, 97)
        .Interior.Color = RGB(196, 239, 207)
    End With

    With condition2
        .Font.Color = RGB(207, 163, 89)
        .Interior.Color = RGB(255, 234, 164)
    End With

    With condition3
        .Font.Color = RGB(170, 50, 77)
        .Interior.Color = RGB(255, 199, 206)
    End With
End Sub


(Tip: when posting code, please try to use 'code tags' to format the code as I have done above

How to Post Your VBA Code

as it makes the code easier to read.)
 
Upvote 0
Solution

Forum statistics

Threads
1,214,833
Messages
6,121,865
Members
449,052
Latest member
Fuddy_Duddy

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