Change The Tab Color

wrangler

New Member
Joined
Mar 17, 2011
Messages
6
How to Change The Tab Color Based On A Cell Date ( Thursday & Friday ) Green Color, Rest of the week blue.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi and welcome to the forum.

For this to happen automatically you will need to use a Worksheet_Change event.

For example, consider the date is placed in cell A1. First we set the Change event to trigger only if A1 changes.

Code:
  [COLOR=green]'only trigger if cell A1 changes[/COLOR]
  [COLOR=darkblue]If[/COLOR] Target.Address <> "$A$1" [COLOR=darkblue]Then[/COLOR] [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
We can the use the Weekday() function to extract the day from the date.
This returns an integer, 1-7. See the Excel Help file for how to change the week start day.
Code:
  [COLOR=green]'weekday() 1=Sunday, 2=Monday, etc[/COLOR]
  myDay = Weekday(CDate(Range("A1").Value))
And use a Select Case statement to control the tab colour:
Code:
  [COLOR=darkblue]Select[/COLOR] [COLOR=darkblue]Case[/COLOR] myDay
    [COLOR=darkblue]Case[/COLOR] vbThursday, vbFriday
      Me.Tab.ColorIndex = 4 [COLOR=green]'green[/COLOR]
    [COLOR=darkblue]Case[/COLOR] [COLOR=darkblue]Else[/COLOR]
      Me.Tab.ColorIndex = 5 [COLOR=green]'blue[/COLOR]
  [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Select[/COLOR]
To test the code below:
Open a new Excel workbook.
Press Alt+F11 to open the VBA editor.
Double click the Sheet1 module in the Project Window on the left hand side.
Copy and paste the code below.

Go to Sheet1 and place a date in cell A1.
Click on the sheet2 tab to see the full effect.
Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Worksheet_Change([COLOR=darkblue]ByVal[/COLOR] Target [COLOR=darkblue]As[/COLOR] Range)
  [COLOR=darkblue]Dim[/COLOR] myDay [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]

  [COLOR=green]'only trigger if cell A1 changes[/COLOR]
  [COLOR=darkblue]If[/COLOR] Target.Address <> "$A$1" [COLOR=darkblue]Then[/COLOR] [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
  
  [COLOR=green]'weekday() 1=Sunday, 2=Monday, etc[/COLOR]
  myDay = Weekday(CDate(Range("A1").Value))

  [COLOR=darkblue]Select[/COLOR] [COLOR=darkblue]Case[/COLOR] myDay
    [COLOR=darkblue]Case[/COLOR] vbThursday, vbFriday
      Me.Tab.ColorIndex = 4 [COLOR=green]'green[/COLOR]
    [COLOR=darkblue]Case[/COLOR] [COLOR=darkblue]Else[/COLOR]
      Me.Tab.ColorIndex = 5 [COLOR=green]'blue[/COLOR]
  [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Select[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

I hope this helps,
Bertie
 
Last edited:
Upvote 0
Thank you so much and I really appreciate your help Sir.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,357
Members
452,907
Latest member
Roland Deschain

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