Option Explicit
Sub 上班日_假日_補班日()
Dim Brr, Sh1, V, xA, xR, Z, P, W, i&, n&, Ch$, y%, ymd As Date
Dim X&(4)
Set W = CreateObject("Scripting.Dictionary")
Set Z = CreateObject("Scripting.Dictionary")
Set P = CreateObject("Scripting.Dictionary")
Set Sh1 = Sheets("Sheet1")
Set Brr = Range(Sh1.[A1], Sh1.UsedRange)
Sh1.[AA:AH].ClearContents
V = Split(",一,二,三,四,五,六,七,八,九,十,十一,十二", ",")
For i = 1 To 12
Z(V(i) & "月") = i
Next
For Each xR In Brr
If xR Like "*西元####年*" Then y = Mid(xR, InStr(xR, "西元") + 2, 4)
Ch = xR & xR.Item(, 2) & xR.Item(, 3)
If Z.Exists(Ch) And xR.Item(, 3) <> "" Then
Set W(Ch) = xR.Item(1, -1).Resize(14, 7)
End If
Next
Z.Add "非周休二日假日", 1
Z.Add "周休二日", 3
Z.Add "上班日", 5
Z.Add "補班日", 7
For Each xR In W.KEYS
For Each xA In W(xR)
If IsNumeric(xA) And xA <> "" Then
ymd = y & "/" & Z(xR) & "/" & xA
If xA.Interior.ColorIndex <> -4142 Then
If Format(ymd, "w") > 1 And Format(ymd, "w") < 7 Then
P(ymd) = "非周休二日假日"
X(1) = X(1) + 1
W(ymd) = X(1)
Else
P(ymd) = "周休二日"
X(2) = X(2) + 1
W(ymd) = X(2)
End If
ElseIf Format(ymd, "w") > 1 And Format(ymd, "w") < 7 Then
P(ymd) = "上班日"
X(3) = X(3) + 1
W(ymd) = X(3)
n = n + 1
Else
P(ymd) = "補班日"
X(4) = X(4) + 1
W(ymd) = X(4)
End If
End If
Next
Next
ReDim Brr(1 To n, 1 To 8)
For Each xR In P.KEYS
ymd = xR
Brr(W(ymd), Z(P(ymd))) = ymd
Brr(W(ymd), Z(P(ymd)) + 1) = Format(ymd, "aaaa")
Next
[AA1].Resize(1, 8) = [{"非周休二日假日","","周休二日","","上班日","","補班日",""}]
[AA2].Resize(n, 8) = Brr
Sh1.[AA:AH].Columns.AutoFit
[AA1].CurrentRegion.Borders.LineStyle = 1
Set W = Nothing: Set Z = Nothing: Set P = Nothing: Set Brr = Nothing
Erase V, X
End Sub作者: Andy2483 時間: 2022-12-7 12:47
添加簡單防錯:
[attach]35570[/attach]
Option Explicit
Sub 上班日_假日_補班日()
Dim Brr, Sh1, V, xA, xR, Z, P, W, i&, n&, Ch$, y%, ymd As Date
Dim X&(4), T&
Set W = CreateObject("Scripting.Dictionary")
Set Z = CreateObject("Scripting.Dictionary")
Set P = CreateObject("Scripting.Dictionary")
Set Sh1 = Sheets("Sheet1")
Set Brr = Range(Sh1.[A1], Sh1.UsedRange)
Sh1.[AA:AH].ClearContents
V = Split(",一,二,三,四,五,六,七,八,九,十,十一,十二", ",")
For i = 1 To 12
Z(V(i) & "月") = i
Next
For Each xR In Brr
If xR Like "*西元####年*" Then y = Mid(xR, InStr(xR, "西元") + 2, 4)
Ch = xR & xR.Item(, 2) & xR.Item(, 3)
If Z.Exists(Ch) And xR.Item(, 3) <> "" Then
Set W(Ch) = xR.Item(1, -1).Resize(14, 7)
End If
Next
Z.Add "非周休二日假日", 1
Z.Add "周休二日", 3
Z.Add "上班日", 5
Z.Add "補班日", 7
For Each xR In W.KEYS
For Each xA In W(xR)
If IsNumeric(xA) And xA <> "" Then
ymd = y & "/" & Z(xR) & "/" & xA
If xA.Interior.ColorIndex <> -4142 Then
If Format(ymd, "w") > 1 And Format(ymd, "w") < 7 Then
P(ymd) = "非周休二日假日"
X(1) = X(1) + 1
W(ymd) = X(1)
Else
P(ymd) = "周休二日"
X(2) = X(2) + 1
W(ymd) = X(2)
End If
ElseIf Format(ymd, "w") > 1 And Format(ymd, "w") < 7 Then
P(ymd) = "上班日"
X(3) = X(3) + 1
W(ymd) = X(3)
n = n + 1
Else
P(ymd) = "補班日"
X(4) = X(4) + 1
W(ymd) = X(4)
End If
End If
Next
Next
For ymd = y & "/1/1" To y & "/12/31"
If P.Exists(ymd) = Empty Then
MsgBox "缺少: " & ymd
GoTo 111
End If
T = T + 1
Next
ReDim Brr(1 To n, 1 To 8)
For Each xR In P.KEYS
ymd = xR
Brr(W(ymd), Z(P(ymd))) = ymd
Brr(W(ymd), Z(P(ymd)) + 1) = Format(ymd, "aaaa")
Next
[AA1].Resize(1, 8) = [{"非周休二日假日","","周休二日","","上班日","","補班日",""}]
[AA2].Resize(n, 8) = Brr
Sh1.[AA:AH].Columns.AutoFit
[AA1].CurrentRegion.Borders.LineStyle = 1
MsgBox y & "年 共有 " & T & " 天" & vbLf & _
"非周休二日假日 共: " & X(1) & " 天" & vbLf & _
"周休二日 共: " & X(2) & " 天" & vbLf & _
"上班日 共: " & X(3) & " 天" & vbLf & _
"補班日 共: " & X(4) & " 天"
111
Set W = Nothing: Set Z = Nothing: Set P = Nothing: Set Brr = Nothing
Erase V, X
End Sub作者: 星空乂羽翼 時間: 2022-12-7 13:43