anarchyflag
New Member
- Joined
- Nov 2, 2018
- Messages
- 15
I have a spreadsheet where I need the formatting to change depending on the day of the week. I also want the formatting to alternate colour by line, to help break it up. It should look like this:
I have some code which formats specific ranges of columns to format in the alternating colours:
However, this isn't particularly agile, since I want to be able to run this report over any number of weeks (up to a year), and hard coding specific cell ranges would mean repeating the same code 52 times.
I also have the following code which I've used for cell widths:
Which I'm using for coding the column widths (this isn't great either - I can't work out how to make it count all the weekdays in the same section of code, but it works so I'm happy to leave it).
Is there any way to combine this code so that it reads: If the cell in Row 1 contains Mon, Tue, Wed, Thu, Fri, Sat, change column width and format in alternating colours? And the same for Sundays etc.
Thanks in advance.
I have some code which formats specific ranges of columns to format in the alternating colours:
VBA Code:
'Colour Sundays alternate light grey / dark grey
For Each cell In Range("AX1:AX" & lastRow)
If cell.Row Mod 2 = 1 Then
cell.Interior.Color = RGB(217, 217, 217)
Else
cell.Interior.Color = RGB(242, 242, 242)
End If
Next cell
However, this isn't particularly agile, since I want to be able to run this report over any number of weeks (up to a year), and hard coding specific cell ranges would mean repeating the same code 52 times.
I also have the following code which I've used for cell widths:
VBA Code:
Dim Range1 As Range
Dim Cell1 As Range
Set Range1 = Range("O1:QA1")
For Each Cell1 In Range1
If Cell1 Like "*Mon*" Or Cell1 Like "*Tue*" Then
Cell1.ColumnWidth = 7.86
End If
Next Cell1
For Each Cell1 In Range1
If Cell1 Like "*Wed*" Or Cell1 Like "*Thu*" Then
Cell1.ColumnWidth = 7.86
End If
Next Cell1
For Each Cell1 In Range1
If Cell1 Like "*Fri*" Or Cell1 Like "*Sat*" Then
Cell1.ColumnWidth = 7.86
End If
Next Cell1
Which I'm using for coding the column widths (this isn't great either - I can't work out how to make it count all the weekdays in the same section of code, but it works so I'm happy to leave it).
Is there any way to combine this code so that it reads: If the cell in Row 1 contains Mon, Tue, Wed, Thu, Fri, Sat, change column width and format in alternating colours? And the same for Sundays etc.
Thanks in advance.