Change font color for certain range of cell value using VBA

dave tan

New Member
Joined
May 9, 2019
Messages
4
Hi there.
I am trying to make a certain range of cell (A1:C5) turn to red/white according to the value of A in each row using VBA
by clicking on a button to check and change it.

[TABLE="width: 192"]
<colgroup><col width="64" style="width:48pt"> <col width="64" span="2" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl65, width: 64"]A[/TD]
[TD="class: xl65, width: 64"]B[/TD]
[TD="class: xl65, width: 64"]C[/TD]
[/TR]
[TR]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"]Dog[/TD]
[TD="class: xl65"]200[/TD]
[/TR]
[TR]
[TD="class: xl65"]5[/TD]
[TD="class: xl65"]Pig[/TD]
[TD="class: xl65"]50[/TD]
[/TR]
[TR]
[TD="class: xl65"]R1[/TD]
[TD="class: xl65"]Dog[/TD]
[TD="class: xl65"]10[/TD]
[/TR]
[TR]
[TD="class: xl65"]F1[/TD]
[TD="class: xl65"]Dog[/TD]
[TD="class: xl65"]5[/TD]
[/TR]
[TR]
[TD="class: xl65"]8[/TD]
[TD="class: xl65"]Pig[/TD]
[TD="class: xl65"]2
[/TD]
[/TR]
</tbody>[/TABLE]

Rule: If the Value of A is not a number then turn the color of the row to Red/white.
In this case:
Range A3:C3 and range A4:C4 in red/white color.

Thanks,
Dave
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hey try this code:

Code:
Sub ChangeFontColour()
    Dim rng As Range
    Dim cell As Range
    Set rng = Range("A1:A5")
    For Each cell In rng
        If IsNumeric(cell) = False Then
            Range(Cells(cell.Row, Columns("A").Column), Cells(cell.Row, Columns("C").Column)).Font.Color = vbRed
        Else
            Range(Cells(cell.Row, 1), Cells(cell.Row, 3)).Font.Color = vbWhite
        End If
    Next cell
    
End Sub

Then make a commandbutton and use the code ChangeFontColour to link the macro.
 
Last edited:
Upvote 0
On second thought, when you say red/white do you mean red font and a white background?

Use this code instead:

Code:
Sub ChangeFontColour()
    Dim rng As Range
    Dim cell As Range
    Set rng = Range("A1:A5")
    For Each cell In rng
        If IsNumeric(cell) = False Then
            With Range(Cells(cell.Row, Columns("A").Column), Cells(cell.Row, Columns("C").Column))
                .Font.Color = vbRed
                .Interior.Color = vbWhite
            End With
        Else
            With Range(Cells(cell.Row, Columns("A").Column), Cells(cell.Row, Columns("C").Column))
                .Font.Color = vbBlack
                .Interior.ColorIndex = 0
            End With
        End If
    Next cell
End Sub
 
Last edited:
Upvote 0
Hi tyija1995,
Thank you for the code. It works like a charm.
There is another issue. After i click the button, all the command in excel turn grey until i click anywhere on the sheet.
How to make the command automatically finish without clicking again?

Thanks,
Dave
 
Upvote 0
Hey,

I'm not sure how to fix that with a commandbutton actually(?)

However you can insert a shape of your choice and assign the ChangeFontColour macro to the shape - then you can change the text / format of the shape too, it's more customizable than a commandbutton!

Insert Ribbon tab -> Shapes -> Choose a shape -> Right click shape -> Assign Macro -> ChangeFontColour -> OK
 
Last edited:
Upvote 0
Assuming A1:A5 are constants (not formulas), here is another macro to consider...
Code:
Sub ChangeFontColour()
  [A:C].ClearFormats
  With Intersect([A:C], [A1:A5].SpecialCells(xlConstants, xlTextValues).EntireRow)
    .Font.Color = vbRed
    .Interior.Color = vbWhite
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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