VBA - If cell = 0, make font white?

lichldo

Board Regular
Joined
Apr 19, 2022
Messages
65
Office Version
  1. 365
Platform
  1. MacOS
Hello, I am trying to use conditional formatting but in VBA and the record macro doesn't record the code from conditional formatting

I just need any cell that = 0 to have white font

Is this possible in VBA? It needs to apply to the entire sheet

Thank you!!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi,

In your conditional formatting you can use a rule whereby if value = 0 then custom format is ;;;
 
Upvote 0
My macro recorder creates the appropriate code as expected, not sure why yours doesn't. Example (with minor tweaks):
VBA Code:
Sub Macro1()
With Sheets("Sheet1").UsedRange
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=0"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
End With
With Sheets("Sheet1").UsedRange.FormatConditions(1).Font
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = 0
End With
Sheets("Sheet1").UsedRange.FormatConditions(1).StopIfTrue = False
End Sub
 
Upvote 0
Solution
My macro recorder creates the appropriate code as expected, not sure why yours doesn't. Example (with minor tweaks):
VBA Code:
Sub Macro1()
With Sheets("Sheet1").UsedRange
    .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=0"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
End With
With Sheets("Sheet1").UsedRange.FormatConditions(1).Font
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = 0
End With
Sheets("Sheet1").UsedRange.FormatConditions(1).StopIfTrue = False
End Sub
the name of the sheet will not always be consistent. can it be applied to just be on the active sheet?
 
Upvote 0
Just change all references from Sheets("Sheet1") to ActiveSheet
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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