VBA code to set font color to match background color

kelly mort

Well-known Member
Hello

I need a script that will set font color to match that of the cell background color.

My aim is to hide the texts in the cells. I have groups of rows with different background colors.

The data range is A20:O68


Thanks in advance
 

Fluff

MrExcel MVP, Moderator
How about
Code:
Sub kellymort()
   Dim Cl As Range
   For Each Cl In Range("A20:O68")
      Cl.Font.Color = Cl.Interior.Color
   Next Cl
End Sub
 

Jaafar Tribak

Well-known Member
Alternatively, if you just want to hide the data in the cells, you could change the number format as follows:
Code:
Range("A20:O68").NumberFormat = ";;;"
 

Jaafar Tribak

Well-known Member
You can wrap that in a handy Property as follows :
Code:
Property Let HideData(ByVal Rng As Range, ByVal Hide As Boolean)
    Rng.NumberFormat = IIf(Hide, ";;;", "General")
End Property
Then you just hide\show the data as foolows :
Code:
Sub HideTheData()
   HideData(Range("A20:O68")) = True
End Sub


Sub ShowTheData()
   HideData(Range("A20:O68")) = False
End Sub
 
Last edited:

Jaafar Tribak

Well-known Member
Cool @Jaafar Tribak

How do I access the property from other modules as well?
I just saw this:

The Property is Public so it should be accessible throughout the whole project as it is.

You should also fully qualify the range whose data you want to hide from view.

example :

HideData(Sheet1.Range("A20:O68")) = True
 
Last edited:

Some videos you may like

This Week's Hot Topics

Top