VBA Toggle off to show/hide text based on font colour

Melimob

Active Member
Joined
Oct 16, 2011
Messages
396
Office Version
  1. 365
Hi

I have a table and in column c I have cells which have black font and cells with a blue font.

When the toggle button is pressed I want the cells which are black to remain black font and the cells which are blue to match their interior colour, in other words, they seem to disappear.

The reason for this is they repeat the header row of that section but it looks too much for them to always show however if I'm filtering another column, I need their values to show as the header row for that section won't.

Any advice gratefully received as I've spent hours trying to work this one out :(

thank you!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi - I feel like I'm so close with this...but no cigar!

Any help would be great..

so this works just to change the whole range which I'm looking at to green:

Code:
Private Sub ToggleButton1_Click()
Dim rng As Range
Set rng = Range("Table1[Country]")
With rng.Font
    .ColorIndex = IIf(.ColorIndex = 50, 1, 50)
End With
End Sub


and this is how I've tried to adapt it but nothing happens?:
Code:
Private Sub ToggleButton1_Click()
Dim rng As Range
Set rng = Range("Table1[Country]")
With rng.Font
    If .ColorIndex <> 1 Then
    .Interior.ColorIndex
    End If
End With
End Sub
 
Upvote 0
Hi - I feel like I'm so close with this...but no cigar!

Any help would be great..

so this works just to change the whole range which I'm looking at to green:

Code:
Private Sub ToggleButton1_Click()
Dim rng As Range
Set rng = Range("Table1[Country]")
With rng.Font
    .ColorIndex = IIf(.ColorIndex = 50, 1, 50)
End With
End Sub


and this is how I've tried to adapt it but nothing happens?:
Code:
Private Sub ToggleButton1_Click()
Dim rng As Range
Set rng = Range("Table1[Country]")
With rng.Font
    If .ColorIndex <> 1 Then
    .Interior.ColorIndex
    End If
End With
End Sub

OK so tried another approach:

Code:
Private Sub ToggleButton2_Click()
If ToggleButton2.Value = True Then
 'This area contains the things you want to happen
'when the toggle button is not depressed
UnhideFont
Else
'This area contains the things you want to happen
'when the toggle button is depressed
 HideFont
  End If


End Sub

and by Subs for Hide/unhide are
Code:
Private Sub HideFont()
    Dim cell As Variant
    Dim rng As Range
    Set rng = Range("Table1[Country]")
    For Each cell In rng
        cell.Font.Color = cell.Interior.Color
    Next cell
End Sub


Sub UnhideFont()
    Dim cell As Variant
    Dim rng As Range
    Set rng = Range("Table1[Country]")
    For Each cell In rng
        cell.Font.Color = 1
    Next cell
End Sub

Hide: changes the whole column to interior color but I only want to do for the cells where font is NOT black
Unhide: changes the whole column to black - again, if I can get 'hide to work' I only want to change the ones which were previously 'interior color' to say color 25 (so that hide won't see them as black)?

tearing my hair out here but any advice would be wonderful please!
 
Upvote 0
I thought this would work but no :(

Code:
Private Sub HideFont()
    Dim cell As Variant
    Dim rng As Range
    Set rng = Range("Table1[Country]")
    For Each cell In rng
       If cell.Font.Color <> 1 Then
       cell.Font.Color = cell.Interior.Color
       End If
    Next cell
End Sub

Just hides all :(
 
Upvote 0
SOLVED

I played around with it and managed to figure it out so posting the final code here in case it helps anyone else:

Code:
Private Sub HideFont()
    Dim cell As Variant
    Dim rng As Range
    Set rng = Range("Table1[Country]")
    For Each cell In rng
       If cell.Font.Color <> vbBlack Then
       cell.Font.Color = cell.Interior.Color
       End If
    Next cell
End Sub


Sub UnhideFont()
    Dim cell As Variant
    Dim rng As Range
    Set rng = Range("Table1[Country]")
    For Each cell In rng
        If cell.Font.Color <> vbBlack Then
        cell.Font.Color = 50
       End If
    Next cell
End Sub

then my toggle is:

Code:
Private Sub ToggleButton2_Click()
If ToggleButton2.Value = True Then
 'This area contains the things you want to happen
'when the toggle button is not depressed
UnhideFont
Else
'This area contains the things you want to happen
'when the toggle button is depressed
 HideFont
  End If


End Sub

thank you all!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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