Setting the background color for weekends

DChambers

Active Member
Joined
May 19, 2006
Messages
257
I am using the following code to create a spreadsheet that has all the days of the week for that month and would like to set the background color for both Saturday and Sunday to gray, any Ideas?
Code:
' Put in the first day of the current month
    ws.Range("A8").Value = DateSerial(Year(Date), Month(Date), 1)
 
' AutoFill Column "A" with all the dates of the month
    ws.Range("A8").AutoFill Destination:=ws.Range("A8").Resize(DateSerial(Year(Date), Month(Date) + 1, 1) - DateSerial(Year(Date), Month(Date), 1))
 
' AutoFill Column "B" with the days of the week
    ws.Range("B8").Resize(DateSerial(Year(Date), Month(Date) + 1, 1) - DateSerial(Year(Date), Month(Date), 1)).Formula = "=TEXT(A8,""DDDD"")"
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
You can get the number of days in the month like this:

Day(DateSerial(Year(Date), Month(Date) + 1, 0))

and here's another way to get the first day of the month:
Date - Day(Date) + 1

Why not use Conditional Formatting?

Select cell A8:B38 and use the following Conditional Formatting formula:
=AND($A8<>"",WEEKDAY($A8,2)>5)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,381
Messages
5,595,843
Members
414,025
Latest member
haraop

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
Top