VBA Font Conditional Formatting based on cell value change - but it doesn't trigger

hcgood

New Member
Joined
Jun 30, 2006
Messages
22
Below is my poor attempt at VBA code based on changing cell value in P14.
When P14 changes to 1 it should trigger, but it doesn't trigger.
If I step through it, it correctly changes the font size and color in cell P15.

How do I need to change this code so it will trigger when P14 changes to 1?
Many thanks in advance!

Sub Formatting()

ThisWorkbook.Worksheets("Progress").Range("$P$14").Select
With ThisWorkbook.Worksheets("Progress").Range("$P$15")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=$P$14=1"
.FormatConditions(1).Interior.ColorIndex = 3 'red
With .Font
.ColorIndex = 27 'color Yellow
.Size = 12
.Bold = True
End With
End With
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi hcgood,

You can add the below slightly amended from your original code in to the worksheet module. It will in effect watch cell P14 for a change and when it is 1 it will trigger the code...

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("P14")) Is Nothing Then

    With ThisWorkbook.Worksheets("Progress").Range("$P$15")
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$P$14=1"
    .FormatConditions(1).Interior.ColorIndex = 3 'red
        With .Font
        .ColorIndex = 27 'color Yellow
        .Size = 12
        .Bold = True
        End With
    End With

End If

End Sub

Where to add the code, double click your sheet 'Progress'...
1732785250545.png
 
Upvote 0
Solution
If I step through it, it correctly changes the font size and color in cell P15
Those aspects are not part of the conditional format, so it would imply that P14 does not actually equal 1 if the background isn't going red.
 
Upvote 0
Hi hcgood,

You can add the below slightly amended from your original code in to the worksheet module. It will in effect watch cell P14 for a change and when it is 1 it will trigger the code...

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("P14")) Is Nothing Then

    With ThisWorkbook.Worksheets("Progress").Range("$P$15")
    .FormatConditions.Delete
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$P$14=1"
    .FormatConditions(1).Interior.ColorIndex = 3 'red
        With .Font
        .ColorIndex = 27 'color Yellow
        .Size = 12
        .Bold = True
        End With
    End With

End If

End Sub

Where to add the code, double click your sheet 'Progress'...
View attachment 119774
Thanks sxhall. That solved that part.
But I'd assumed that if P14 no longer equals 1 that it would go back to it's original format, but it doesn't.
Seems I need a second step so that if P14 not equal to 1 that it goes back to Color = Black, Size = 16.
I don't know if an Else statement is needed or how to make it change back to original font.

Many thanks
 
Upvote 0

Forum statistics

Threads
1,225,619
Messages
6,186,045
Members
453,335
Latest member
sfd039

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