VBA to Hide Worksheets Based on Coloured Tabs

chingching831

New Member
Joined
Jun 2, 2022
Messages
35
Office Version
  1. 2019
Platform
  1. Windows
Hi there,

I have a code to hide worksheets based on specific RGB colours. But I am not sure why it isn't working ...

VBA Code:
Sub HideColouredTabs()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

    If ws.Tab.Color = RGB(191, 191, 191) Then
        ws.Visible = xlSheetHidden
    End If

Next ws

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Maybe with the sheet you want to hide active, run the Sub below and check that the RGB that prints to the Immediate window matches your expected RGB.

VBA Code:
Sub GetTabColour()

    Dim strRGB As String
    strRGB = getRGB2(ActiveSheet.Tab)
    Debug.Print strRGB

End Sub

Function getRGB2(rcell_tab) As String
    Dim C As Long
    Dim R As Long
    Dim G As Long
    Dim B As Long
    
    Select Case TypeName(rcell_tab)
    Case "Tab"
        C = rcell_tab.Color
    Case "Range"
        C = rcell_tab.Interior.Color
    Case Else
        Exit Function
    End Select
    R = C Mod 256
    G = C \ 256 Mod 256
    B = C \ 65536 Mod 256
    getRGB2 = "R=" & R & ", G=" & G & ", B=" & B
End Function
 
Upvote 0
What does the 256 and 65536 stand for?

Maybe with the sheet you want to hide active, run the Sub below and check that the RGB that prints to the Immediate window matches your expected RGB.

VBA Code:
Sub GetTabColour()

    Dim strRGB As String
    strRGB = getRGB2(ActiveSheet.Tab)
    Debug.Print strRGB

End Sub

Function getRGB2(rcell_tab) As String
    Dim C As Long
    Dim R As Long
    Dim G As Long
    Dim B As Long
   
    Select Case TypeName(rcell_tab)
    Case "Tab"
        C = rcell_tab.Color
    Case "Range"
        C = rcell_tab.Interior.Color
    Case Else
        Exit Function
    End Select
    R = C Mod 256
    G = C \ 256 Mod 256
    B = C \ 65536 Mod 256
    getRGB2 = "R=" & R & ", G=" & G & ", B=" & B
End Function
 
Upvote 0
The function converts the long integer color number in VBA to the RGB color coding.
Ref: Determining the RGB Value of a Color
If you really want to get more into the mechanics you can try this:

For our purposes you just need to run it to see if your tab color really is RGB(191, 191, 191), so that it meets your if statement criteria.
PS: RGB(191, 191, 191) = 12566463
 
Last edited:
Upvote 0
I found that the code can be run successfully if it's added in the module of my current workbook (green arrow), but not my usual workbook (red arrow) that stores all the VBA Codes

1718720823159.png
 
Upvote 0
ThisWorkbook refers to the workbook that the code is in. If you put the code in your personal workbook change it to ActiveWorbook.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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