Change font color after cell color, 5 lines of code only

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I think this is a pretty simple request. I have many different modules that look for errors in my worksheets and colors any cell red with an error. Note that this is NOT through conditional formatting for many reasons.

The problem that I run into with all my code is that every time a cell is made red, the black font is hard to see against the red cell. I would like to resolve this by changing the font to white anytime a cell is made red.

The code I have that makes cells red doesn't always look the same.

It appears that I have 5 different ways to highlight a cell red
1. - Interior.ColorIndex = 3
2. - Interior.color = RGB(255, 0, 0)
3. - .ColorIndex = 3
4. - Interior.color = vbRed
5. - Interior.color = 255


Here are 5 examples of code that turns my cells red, but keeps the fonts black.

Example 1

VBA Code:
Select Case LCase(Cells(i, Scol).Value)
        Case "need customer number", "need order number": Cells(i, lcol).Interior.ColorIndex = 3 '<<<Cell is Red, but also want to make the font white
    End Select


Example 2
VBA Code:
With Cells(i, iCol)
        If .Value = "Need customer number" Then .Interior.color = RGB(255, 0, 0) '<<<Cell is Red, but also want to make the font white


Example 3
VBA Code:
With Cells(L4Row, rngGrnd.Column).Interior
         .Pattern = xlSolid
         If Cells(L4Row, rngGrnd.Column).Value = "" Then
          .ColorIndex = 3 '<<<Cell is Red, but also want to make the font white
          End if
          End with


Example 4
VBA Code:
With ActiveSheet
    nCol = Application.Match("Customer Number", .Rows(1), 0)
Dim fnd As Range
Set fnd = Range("1:1").Find("Office", , , xlWhole, , , False, , False)
If Not fnd Is Nothing Then fnd.EntireColumn.SpecialCells(xlBlanks).Interior.color = vbRed'<<<Cell is Red, but also want to make the font white
End With


Example 5
VBA Code:
Do While ActiveCell.Row > 1
    If Cells(ActiveCell.Row, totalCol).Value >= 100 _
    And Cells(ActiveCell.Row, bigorderCol).Value = "Big Order" Then
        Cells(ActiveCell.Row, totalCol).Interior.color = 255'<<<Cell is Red, but also want to make the font white
    End If


Note that I only want to have white fonts whose cells were made red through that particular sub/code and can't use conditional formatting due to the way that I manipulate the data.
It needs to be a "solid red cell" with a "solid white font" without being rule-dependent like Conditional Formatting is.

Being only a novice, I won't know what to do if I'm just given a piece of code. If you would please just combine the code with my 5 examples above, that would be great!


Thanks so much!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
How about
VBA Code:
Select Case LCase(Cells(i, Scol).Value)
        Case "need customer number", "need order number"
            Cells(i, lcol).Interior.ColorIndex = 3
            Cells(i, lcol).Font.ColorIndex = 2
End Select
VBA Code:
With Cells(i, iCol)
        If .Value = "Need customer number" Then
            .Interior.ColorIndex = 3
            .Font.ColorIndex = 2
        End If
VBA Code:
If Not Fnd Is Nothing Then
    With Fnd.EntireColumn.SpecialCells(xlBlanks)
        .Interior.ColorIndex = 3
        .Font.ColorIndex = 2
    End With
End If
VBA Code:
    If Cells(ActiveCell.Row, totalCol).Value >= 100 _
        And Cells(ActiveCell.Row, bigorderCol).Value = "Big Order" Then
            Cells(ActiveCell.Row, totalCol).Interior.ColorIndex = 3
            Cells(ActiveCell.Row, totalCol).Font.ColorIndex = 2
    End If
 
Upvote 0
Hello Fluff, thank you for the code!

I initially had some difficulties, (my fault) but figured it out and all is great!

Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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