Color cells if workday and holiday (VBA)

Retroshift

Board Regular
Joined
Sep 20, 2016
Messages
119
Office Version
  1. 2019
Platform
  1. Windows
Hello, I got a macro button which adds month sheets for a specific year that is entered in an inputbox.
The following code unsuccessfully attempts to fill all working days with a pink colour if they match a range of cells that represent the holidays (and include blank cells).
Is there a way to make the code work?

VBA Code:
            For Each rng In Range("B1").Resize(2, Day(DateSerial(yr, i + 1, 1) - 1))
                datearray = Application.Transpose(Worksheets("Holidays").Range("C8:C42")) 'includes blank cells
                If Weekday(rng) > 1 And Weekday(rng) < 7 And Filter(datearray, CLng(rng), , vbTextCompare) Then
                rng.Interior.Color = RGB(255, 204, 255)
                End If
            Next
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
VBA is NOT the Holy Grail! All you need is Conditional Formatting. The easiest way to set it up is to create a formula in the worksheet that results in True/False with respect to the top left corner of the conditional area. It's not important here since you're only looking at one column. Here's how to do it:
Excel VBA User Defined Function To Generate A Dynamic Array - 2516.xlsm
ABC
1DateCFHolidays
201/01/2022FALSE01/01/2021
301/02/2022FALSE01/18/2021
401/03/2022FALSE02/15/2021
501/04/2022FALSE04/02/2021
601/05/2022FALSE05/31/2021
701/06/2022FALSE07/05/2021
801/07/2022FALSE09/06/2021
901/08/2022FALSE11/25/2021
1001/09/2022FALSE12/24/2021
1101/10/2022FALSE12/31/2021
1201/11/2022FALSE01/17/2022
1301/12/2022FALSE02/21/2022
1401/13/2022FALSE04/15/2022
1501/14/2022FALSE05/30/2022
1601/15/2022FALSE07/04/2022
1701/16/2022FALSE09/05/2022
1801/17/2022TRUE11/24/2022
1901/18/2022FALSE12/26/2022
2001/19/2022FALSE
2101/20/2022FALSE
2201/21/2022FALSE
2301/22/2022FALSE
2401/23/2022FALSE
2501/24/2022FALSE
2601/25/2022FALSE
2701/26/2022FALSE
2801/27/2022FALSE
2901/28/2022FALSE
3001/29/2022FALSE
3101/30/2022FALSE
3201/31/2022FALSE
Sheet4
Cell Formulas
RangeFormula
B2:B32B2=ISNUMBER(MATCH($A2,$C$2:$C$19,0))
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:A32Expression=ISNUMBER(MATCH($A2,$C$2:$C$19,0))textNO
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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