既に適切な回答がなされていると思いますが、
質問の冒頭にVBA初心者とありましたので、
リファレンスでなく具体的なコードの例が必要であれば、
次を参考にしてみてください。
WeekDay関数の戻り値が日曜なら1、土曜なら7なので、
戻り値を6で割った余りが1なら土日、それ以外なら平日と判定しています。
vba
1'A列の日付が土日なら着色する
2Sub setInteriorColorByDate()
3 Const firstRow As Long = 2 '日付の開始行
4 Const colorColored As String = "&HF7EBDD"
5 Const colorWhite As String = "&HFFFFFF"
6 Dim sheet As Worksheet
7 Set sheet = ThisWorkbook.Worksheets(1)
8
9 With sheet
10 Dim lastRow As Long
11 lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
12 Dim values As Variant
13 values = .Range("A" & firstRow & ":A" & lastRow).Value
14 Dim i As Long
15 Dim dateValue As Date
16 Dim interiorColor As String
17 For i = 1 To UBound(values, 1)
18 'Debug.Print Hex(.Range("A" & (i - 1 + firstRow)).Interior.color)
19 dateValue = values(i, 1)
20 '日付が土曜、日曜なら背景色を着色
21 If Weekday(dateValue) Mod 6 = 1 Then
22 interiorColor = colorColored
23 Else
24 interiorColor = colorWhite
25 End If
26 .Range("A" & (i - 1 + firstRow)).Interior.color = interiorColor
27 Next i
28 End With
29End Sub
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/01/27 17:49 編集