VBA to Colour Spreadsheet tabs

Excelorate3305

New Member
Joined
May 21, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Is it possible to automatically colour spreadsheet tabs in a workbook using VBA? I would like to either:

a) set all tabs to white then have the worksheet tab I have selected automatically change to any colour, or
b) have coloured tabs in the workbook then have the worksheet tab I selected appear white or more boldly then how the other tabs appear.

Any assistance 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.
I don't know how you want to use it but here is the simple line of code...

VBA Code:
Worksheets("Sheet1").Tab.Color = vbWhite
 
Upvote 0
Perhaps like
VBA Code:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
     ws.Tab.Color = RGB(255, 125, 50)
Next

End Sub
You'd probably want to have similar in newsheet event.
 
Upvote 0
The code that @Micron wrote would have to be placed in the "ThisWorkbook" module for it to trigger automatically when the Workbook is opened...
 
Upvote 0
Another option
VBA Code:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
   Sh.Tab.Color = False
   ActiveSheet.Tab.Color = vbRed
End Sub
This will need to go in the ThisWorkbook module.
 
Upvote 0
The code that @Micron wrote would have to be placed in the "ThisWorkbook" module for it to trigger automatically when the Workbook is opened...
I guess I took that for granted as it's the only module that has that event in the drop down list AFAIK.
Using vb constants is OK but you're limited to 8 colours, which is why I went with RGB. I also skipped a) and went with option b), BTW. I suspect that the selected color highlight is theme based, so lighter for dark colours, darker for light colours. Hard to make a dark colour look darker sometimes, so I say go with the theme - but not for me to decide.
 
Upvote 0
@Micron, yes, you are correct, I just quickly put down something easy, for the OP to get the line of code he needed. For a full range of choice he would be better off with RGB... Then all things considered, I have seen other new posters not realize that certain codes need to go into certain modules to work correctly...
 
Upvote 0
I have seen other new posters not realize that certain codes need to go into certain modules to work correctly..
Me too - just yesterday, I think. Sheet event was put into a standard module & then reported as not working so you were right to point that out.
 
Upvote 0
Suggestions do not appear to be working. I have not described my requirement properly.

Assume that I have a workbook with 10 worksheets in it with tabs named "Worksheet 1", "Worksheet 2" etc. through to "Worksheet 10". Also assume that all tab colours are white.

If I select "Worksheet 5" then I want that tab to change colour (other than white) while all the other tabs remain white. If I then select "Worksheet 9" then I want the "Worksheet 9" tab to change colour to what the "Worksheet 5 " tab was and I want the "Worksheet 5" tab to revert back to white..........and so on.

Sorry for any confusion.
 
Upvote 0
Copy this to your Thisworkbook module

VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Sh.Tab.Color = RGB(255, 0, 0)
End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Sh.Tab.Color = RGB(255, 255, 255)
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,653
Messages
6,120,752
Members
448,989
Latest member
mariah3

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