Private Sub CommandButton2_Click()
'3個月
If TextBox2.Value = 1 Then A = Array(3, 5, 7, 15, 17, 19)
If TextBox2.Value = 2 Then A = Array(5, 7, 9, 17, 19, 21)
If TextBox2.Value = 3 Then A = Array(7, 9, 11, 19, 21, 23)
If TextBox2.Value = 4 Then A = Array(9, 11, 13, 21, 23, 25)
If TextBox2.Value = 5 Then A = Array(11, 13, 15, 23, 25)
If TextBox2.Value = 6 Then A = Array(13, 15, 17, 25)
If TextBox2.Value = 7 Then A = Array(15, 17, 19)
If TextBox2.Value = 8 Then A = Array(17, 19, 21)
If TextBox2.Value = 9 Then A = Array(19, 21, 23)
If TextBox2.Value = 10 Then A = Array(21, 23, 25)
If TextBox2.Value = 11 Then A = Array(23, 25)
If TextBox2.Value = 12 Then A = Array(25)
If TextBox2.Value = "" Then A = Array(3, 5, 7, 15, 17, 19)
For I = LBound(A) To UBound(A)
For J = 4 To Cells(3, 4).End(xlToRight).Column
If Cells(A(I), J) = "日班" Then Cells(A(I), J) = "夜班"
If Cells(A(I), J) <> "" And Cells(A(I), J) = "夜班" Then
Cells(A(I), J).Font.Color = RGB(Cells(1, 26), Cells(1, 27), Cells(1, 28))
Cells(A(I), J).Interior.Color = RGB(Cells(1, 32), Cells(1, 33), Cells(1, 34))
End If
Next J
Next I
Erase A
If TextBox2.Value = 1 Then A = Array(3, 5, 7, 15, 17, 19)
If TextBox2.Value = 2 Then A = Array(5, 7, 9, 17, 19, 21)
If TextBox2.Value = 3 Then A = Array(7, 9, 11, 19, 21, 23)
If TextBox2.Value = 4 Then A = Array(9, 11, 13, 21, 23, 25)
If TextBox2.Value = 5 Then A = Array(11, 13, 15, 23, 25)
If TextBox2.Value = 6 Then A = Array(13, 15, 17, 25)
If TextBox2.Value = 7 Then A = Array(15, 17, 19)
If TextBox2.Value = 8 Then A = Array(17, 19, 21)
If TextBox2.Value = 9 Then A = Array(19, 21, 23)
If TextBox2.Value = 10 Then A = Array(21, 23, 25)
If TextBox2.Value = 11 Then A = Array(23, 25)
If TextBox2.Value = 12 Then A = Array(25)
If TextBox2.Value = "" Then A = Array(3, 5, 7, 15, 17, 19)
[attach]32173[/attach]作者: 軒云熊 時間: 2020-6-15 14:05
If TextBox2.Value = 1 Then A = Array(3, 5, 7, 15, 17, 19)
If TextBox2.Value = 2 Then A = Array(5, 7, 9, 17, 19, 21)
If TextBox2.Value = 3 Then A = Array(7, 9, 11, 19, 21, 23)
If TextBox2.Value = 4 Then A = Array(9, 11, 13, 21, 23, 25)
If TextBox2.Value = 5 Then A = Array(11, 13, 15, 23, 25)
If TextBox2.Value = 6 Then A = Array(13, 15, 17, 25)
If TextBox2.Value = 7 Then A = Array(15, 17, 19)
If TextBox2.Value = 8 Then A = Array(17, 19, 21)
If TextBox2.Value = 9 Then A = Array(19, 21, 23)
If TextBox2.Value = 10 Then A = Array(21, 23, 25)
If TextBox2.Value = 11 Then A = Array(23, 25)
If TextBox2.Value = 12 Then A = Array(25)
If TextBox2.Value = "" Then A = Array(3, 5, 7, 15, 17, 19)
k = TextBox2.Value: If k = "" Then k = 1
For i = k To 12: Idx = Idx + 1
If Idx < 4 Or Idx > 6 Then
itm = itm + 1: If itm = 7 Then Exit For
s = s & "," & i * 2 + 1
End If
Next i
A = Split(Mid(s, 2), ",")作者: n7822123 時間: 2020-6-17 02:16
寫法二:
k = TextBox2.Value: If k = "" Then k = 1
For i = k To 12
s = s & "," & i * 2 + 1
itm = itm + 1: If itm = 6 Then Exit For
If i = k + 2 Then i = i + 5
Next i
A = Split(Mid(s, 2), ",")
k = TextBox2.Value: If k = "" Then k = 1
For i = k To 12
s = s & "," & i * 2 + 1
itm = itm + 1: If itm = 6 Then Exit For
If i = k + 2 Then i = i + 3 '跳過後3個迴圈
Next i
A = Split(Mid(s, 2), ",")作者: 軒云熊 時間: 2020-6-17 12:38
Sub 玩日期()
Dim Date1 As Date, Date2 As Date
CheckOK = False
Do Until CheckOK
On Error Resume Next
Date1 = InputBox("請輸入第一天上班日期," & vbCrLf & " 需要日期格式 Ex:2020/1/1")
If Err <> 0 Then MsgBox "你輸入的不是日期格式,請修改" Else CheckOK = True
On Error GoTo 0
Loop
CheckOK = False
Do Until CheckOK
On Error Resume Next
Date2 = InputBox("請輸入要查詢的日期," & vbCrLf & " 需要日期格式 Ex:2020/7/1")
If Err <> 0 Then MsgBox "你輸入的不是日期格式,請修改" Else CheckOK = True
On Error GoTo 0
Loop
Date1 = Date1 - 1
If Date2 - Date1 < 0 Then MsgBox "你輸入的查詢日期比第一天上班日還前面": Exit Sub
'以做4休2為例(6天一循環)
Select Case (Date2 - Date1) Mod 6
Case 1, 2, 3, 4: MsgBox "這天要上班"
Case 5, 6: MsgBox "恭喜!這天是休假^.^"
End Select
End Sub作者: 軒云熊 時間: 2020-6-23 17:37
Sub 玩日期()
Dim Date1 As Date, Date2 As Date
CheckOK = False
Do Until CheckOK
On Error Resume Next
Date1 = InputBox("請輸入第一天上班日期," & vbCrLf & " 需要日期格式 Ex:2020/1/1")
If Err <> 0 Then MsgBox "你輸入的不是日期格式,請修改" Else CheckOK = True
On Error GoTo 0
Loop
CheckOK = False
Do Until CheckOK
On Error Resume Next
Date2 = InputBox("請輸入要查詢的日期," & vbCrLf & " 需要日期格式 Ex:2020/7/1")
If Err <> 0 Then MsgBox "你輸入的不是日期格式,請修改" Else CheckOK = True
On Error GoTo 0
Loop
Date1 = Date1 - 1
If Date2 - Date1 < 0 Then MsgBox "你輸入的查詢日期比第一天上班日還前面": Exit Sub
'以做4休2為例(6天一循環)
Select Case Int(Date2 - Date1) Mod 6
Case 1, 2, 3, 4: MsgBox "這天要上班"
Case 5, 0: MsgBox "恭喜!這天是休假^.^"
End Select
End Sub 作者: 軒云熊 時間: 2020-6-24 06:05
S = 3
E = 1
For F = 1 To 12 '建立範圍
For P = 1 To Day(DateSerial(Year(Now), F + 1, 0))
Cells(S, E) = DateSerial(Year(Now), F, P)
Cells(S - 1, E) = F & "月" & P & "日" & WeekdayName(Weekday(P))
E = E + 1
If P = Day(DateSerial(Year(Now), F + 1, 0)) Then
If F = 12 Then Exit For
S = S + 2
E = 1
End If
Next P
Next F
For E = ActiveWorkbook.Names.Count To 1 Step -1 '清除定義名稱
If ActiveWorkbook.Names(E).Name <> "指定範圍" Then
ActiveWorkbook.Names(E).Delete
End If
Next E
Y = 65
For i = 3 To Cells(3, 1).End(xlDown).Row Step 2 '定義名稱
範圍名稱 = Chr(Y)
Names.Add Name:="第" & 範圍名稱 & "項", RefersTo:=Range(Cells(i, 1), Cells(i, 1).End(xlToRight))
Y = Y + 1
Next i
'Application.Wait Now + TimeValue("00:00:01")
Set AWN = ActiveWorkbook.Names '合併
For R = 1 To AWN.Count
If R <> 1 Then
K = Mid(AWN(R).RefersToR1C1Local, 2, Len(AWN(R))) & ","
Else
K = AWN(R).RefersToR1C1Local & ","
End If
u = u + K
Next R
For E = ActiveWorkbook.Names.Count To 1 Step -1 '清除定義名稱
If ActiveWorkbook.Names(E).Name <> "指定範圍" Then
ActiveWorkbook.Names(E).Delete
End If
Next E
For Each G In Range("指定範圍")
D = G.Offset
Select Case DateAdd("d", -1, D) Mod 6 + 1
Case 1 To 4
G.Offset = "上班"
G.Offset.Font.Color = RGB(0, 0, 89)
G.Interior.Color = RGB(150, 201, 123)
Case 5 To 6
G.Offset = "休假"
G.Offset.Font.Color = RGB(114, 0, 55)
G.Offset.Interior.Color = RGB(255, 255, 92)
End Select
Next G
'Application.ScreenUpdating = True
End Sub作者: 軒云熊 時間: 2020-6-24 06:52
如果把 G.Offset = "上班" 和 G.Offset = "休假" 關掉 就不會出錯了 但是 這樣就達不到目的了
不知道有沒有別的方式
Select Case DateAdd("d", -1, D) Mod 6 + 1 這段有問題 程式已經跑完了 但是還是會停在這裡
說型態不符 但我程式已經跑完了 不知道為甚麼...
For Each G In Range("指定範圍")
D = G.Offset
Select Case DateAdd("d", -1, D) Mod 6 + 1
Case 1 To 4
' G.Offset = "上班"
G.Offset.Font.Color = RGB(0, 0, 89)
G.Interior.Color = RGB(150, 201, 123)
Case 5 To 6
' G.Offset = "休假"
G.Offset.Font.Color = RGB(114, 0, 55)
G.Offset.Interior.Color = RGB(255, 255, 92)
End Select
Next G作者: 軒云熊 時間: 2020-6-24 07:20
剛才在最後 多加一個迴圈 把文字跟顏色分開 就可以了 又是格式問題
感謝 準大大 和 n7822123 大大的指導
For Each H In Range("指定範圍")
If H.Interior.Color = RGB(150, 201, 123) Then H.Offset = "上班"
If H.Offset.Interior.Color = RGB(255, 255, 92) Then H.Offset = "休假"
Next H作者: 軒云熊 時間: 2020-6-24 15:21
子細看了 n7822123 大大的 Sub 玩日期 () 發現
把外面再包一個 Int D = Int(G.Offset) 就正常了不用多加迴圈 >"<作者: 准提部林 時間: 2020-6-28 11:24
Sub 日曆_排班()
Dim D1&, D2&, xDay&, xMon&, xTP$, xVM&, xVD1%, xVD2%
Dim Arr, i&, R&, C&, C2&, N&, TN&, T$, xR As Range
If [A_Head] = [A_End] Then MsgBox "**〔月曆〕尚未建立! ": Exit Sub
D1 = [A_Head]: D2 = [A_End] + 30
'---------------------------------
xDay = [aRng1]: If xDay = 0 Then xDay = D1
If xDay < D1 Or xDay > D2 Then MsgBox "〔排班首日〕超出日曆範圍! ": Exit Sub
xMon = Format(xDay, "yyyymm")
'---------------------------------
xTP = [aRng2]: If xTP = "" Then MsgBox "〔排班班別〕未輸入! ": Exit Sub
If xTP = "日夜輪流" Then Turn = True
xVM = [aRng3]: If xVM = 0 Then MsgBox "〔排班月數〕未輸入! ": Exit Sub
xVD1 = [aRng4]: If xVD1 = 0 Then MsgBox "〔排班天數〕未輸入! ": Exit Sub
xVD2 = [aRng5]: If xVD2 = 0 Then MsgBox "〔休息天數〕未輸入! ": Exit Sub
Dturn = [aRng6]: NTurn = [aRng7]
If Turn And (Dturn = "" Or NTurn = "") Then MsgBox "〔日、夜班輪數〕未輸入完全" : Exit Sub
'---------------------------------
Call 日曆_排班_重置
ReDim Arr(1 To D2 - xDay + 1, 1 To 2)
For i = xDay To D2
xMon = Format(i, "yyyymm")
If xMon <> YM Then YM = xMon: N = N + 1
TN = Int((i - D1 + 1) / (xVD1 + xVD2))
If N > xVM Then Exit For
R = R + 1
C = C Mod (xVD1 + xVD2) + 1
C2 = TN Mod (Dturn + NTurn) + 1
T = IIf(Turn, IIf(C2 > Dturn, "夜班", "日班"), xTP)
If C > xVD1 Then T = "休息"
Arr(R, 1) = i
Arr(R, 2) = T
Next i
[AD1] = "<日期>": [AE1] = "<班別>"
[AD2:AE2].Resize(R) = Arr
...
...
...