Macro to colour code tab based on date on that tab

hotspot1972

New Member
Joined
Jul 9, 2020
Messages
16
Office Version
  1. 2013
Platform
  1. Windows
Hi. I need to highlight the weekends on my tabs. Change the colour of the tab
I run a macro on the first day of the month and it populates the dates onto the tab names.

Private sub commnadbutton2_click
Dim shtcount as long
Dim Sarea as date, Earea as date
Sarea = InputBox("What is the start date?")
Earea = InoutBox("What is the end date?")
Shtcount = EArea - Sarea + 1
For i = 1 to shtcount
Sheets(i).Activate
If i = 1 then
Activesheet.Name = Format(SArea, "dd-mm-yy")
Else
ActiveSheet.Name = Format(SArea, "dd-mm-yy")
End if
SArea =SArea + 1
Next i
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
Hi there,

Though I don't think have a single separate tab for each day of the month is the most efficient way to go, the following will do what you're after:

VBA Code:
Option Explicit
Private Sub commnadbutton2_click()

    Dim shtcount As Long, i As Long
    Dim Sarea As Date, Earea As Date
    
    Application.ScreenUpdating = False
    
    Sarea = InputBox("What is the start date?")
    Earea = InputBox("What is the end date?")
    
    shtcount = Earea - Sarea + 1
    
    For i = 1 To shtcount
        Sheets(i).Activate
        If i = 1 Then
            ActiveSheet.Name = Format(Sarea, "dd-mm-yy")
        Else
            ActiveSheet.Name = Format(Sarea, "dd-mm-yy")
        End If
        Select Case Weekday(ActiveSheet.Name)
            Case vbSaturday, vbSunday
                ActiveSheet.Tab.Color = RGB(0, 255, 0) 'Colour weekend tabs green. Change to suit.
        End Select
        Sarea = Sarea + 1
    Next i
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Hi there,

Though I don't think have a single separate tab for each day of the month is the most efficient way to go, the following will do what you're after:

VBA Code:
Option Explicit
Private Sub commnadbutton2_click()

    Dim shtcount As Long, i As Long
    Dim Sarea As Date, Earea As Date
   
    Application.ScreenUpdating = False
   
    Sarea = InputBox("What is the start date?")
    Earea = InputBox("What is the end date?")
   
    shtcount = Earea - Sarea + 1
   
    For i = 1 To shtcount
        Sheets(i).Activate
        If i = 1 Then
            ActiveSheet.Name = Format(Sarea, "dd-mm-yy")
        Else
            ActiveSheet.Name = Format(Sarea, "dd-mm-yy")
        End If
        Select Case Weekday(ActiveSheet.Name)
            Case vbSaturday, vbSunday
                ActiveSheet.Tab.Color = RGB(0, 255, 0) 'Colour weekend tabs green. Change to suit.
        End Select
        Sarea = Sarea + 1
    Next i
   
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
Worked a treat. Thankyou so much
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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