Toggle Font Color Between Red and Black in Excel 2010 VBA
Question details
Correct an Excel 2010 VBA macro that toggles the font color of selected cells or text boxes between red and black.

- Product
- Microsoft Excel 2010
- Device & OS
- not provided
- Scenario
- The user wants to run one macro on a selected cell range or selected text boxes.
- Observed behavior
- The macro needs the appropriate color values and properties for ranges and text boxes.
Open the workbook in Excel 2010 and save it as an Excel Macro-Enabled Workbook (.xlsm). Enable macros only if you trust the workbook, and select either a cell range or the border of a text box before running the macro.
Use the correct VBA color properties for cells and text boxes
Use this macro to toggle a selected range or selected shapes containing text between red and black.
Cell text uses Font.Color, while text in a shape uses TextFrame2.TextRange.Font.Fill.ForeColor.RGB. RGB(255, 0, 0) is red and has the value 255; RGB(0, 0, 0) is black.
The macro treats a cell range as one selection and toggles each selected shape separately. It works best when the text in each selection has a uniform font color.
In Excel, press Alt+F11, then choose Insert > Module.
Paste this code into the module: Sub ToggleFontColor() Dim current As Variant Dim shapes As ShapeRange Dim shp As Shape If TypeOf Selection Is Range Then current = Selection.Font.Color If IsNull(current) Then Selection.Font.Color = RGB(255, 0, 0) ElseIf current = RGB(255, 0, 0) Then Selection.Font.Color = RGB(0, 0, 0) Else Selection.Font.Color = RGB(255, 0, 0) End If Else On Error Resume Next Set shapes = Selection.ShapeRange On Error GoTo 0 If shapes Is Nothing Then MsgBox "Select a cell range or text box first." Exit Sub End If For Each shp In shapes On Error Resume Next Err.Clear current = shp.TextFrame2.TextRange.Font.Fill.ForeColor.RGB If Err.Number = 0 Then If current = RGB(255, 0, 0) Then shp.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 0) Else shp.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0) End If End If On Error GoTo 0 Next shp End If End Sub
Return to the worksheet and select a cell range or click a text box border. Press Alt+F8, select ToggleFontColor, and click Run.
Press Alt+F8 and run ToggleFontColor again to switch the selected text back. Repeat with the other selection type to check both cells and text boxes.

Edit spreadsheet font colors with WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and use the font color controls to change cell text as you move between office suites. It does not fix Excel's VBA macro behavior.

Frequently Asked Questions
Why does the cell color comparison use 255 for red?
Excel VBA represents RGB(255, 0, 0) as the numeric color value 255. Compare Selection.Font.Color with RGB(255, 0, 0) or 255 to identify red cell text.
Why doesn't Font.Color work the same way for a text box?
Text box text is part of a shape. Use the shape's TextFrame2.TextRange.Font.Fill.ForeColor.RGB property instead of the cell range's Font.Color property.
How do I select a text box before running the macro?
Click the text box border so the shape is selected rather than editing its text, then press Alt+F8 and run ToggleFontColor.
Can I use automatic font color instead of black?
For a cell range, replace the explicit black assignment with Selection.Font.ColorIndex = xlAutomatic. Automatic color depends on Excel's formatting context and is not the same as explicitly setting RGB(0, 0, 0).
What happens if selected cells have different font colors?
The macro applies one color to the entire selected range. A range with mixed colors may not toggle each cell individually, so select uniformly colored cells if you need a predictable result.




