VBA - Change Font Color to Match Background Color Based on Another Cell's Value

singincpa

New Member
Joined
Nov 7, 2014
Messages
10
Hi,

I'm trying to set up a macro (attached to a button) that will do two things when clicked:

- change the print area
- change the font color in certain cells to match their background color.

I have the code for changing the print area, but don't know how to get the other part. Basically, if cell AC1 is not blank, I want the font in multiple cell ranges (D8:D12, D14:D18, I8:I12, I14:I18, etc.) to change to match the background color so that they aren't visible when printed. Right now I have those cells conditionally formatted so that the font turns white if AC1 is not blank, but of course that only works if the cell background is white. The users of the spreadsheet have started changing the background color on those cells on occasion.

Any assistance would be greatly appreciated!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try:
Code:
Sub Test()
    Application.ScreenUpdating = False
    Dim rng As Range
    If Range("AC1") <> "" Then
        For Each rng In Range("D8:D12, D14:D18, I8:I12, I14:I18")
            rng.Font.ColorIndex = rng.Interior.ColorIndex
        Next rng
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Yes I removed the conditional formatting. When I place an X in cell AC1 and then run the macro, the font colors are not changing.
 
Upvote 0
Do you mean the font colour doesn't change at all, or it changes, but not to the same colour as the fill?
If the latter, try this mod to mumps code
Code:
Sub Test()
    Application.ScreenUpdating = False
    Dim rng As Range
    If Range("AC1") <> "" Then
        For Each rng In Range("D8:D12, D14:D18, I8:I12, I14:I18")
            [COLOR=#ff0000]rng.Font.Color = rng.Interior.Color[/COLOR]
        Next rng
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,442
Members
449,083
Latest member
Ava19

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