VBA code to set font color to match background color

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
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 = ";;;"
 
Upvote 0
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:
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
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