返回列表 上一主題 發帖

[發問] 請問 ARRAY範圍 改成 動態 指定 範圍問題 請前輩們指教

請問准提大大 如果要設定幾輪上 夜班 日班 假設一輪是12天
該如何寫呢?
準提大大的版本 排版更是清楚 但我應該在哪一段修改 要怎麼寫? 

以下是之前修改過的 但對我來說已經是極限了..實在想不出來 >�苤�
問題出在這裡:
如果用 �苺@w�苤@週數計算上班的天數就會有問題..不知如何修改 
�苠噿苳捊こ|搭不到  
        Select Case DateAdd("d", -1, K) Mod Cells(1, 3) + 1   '常日班
        Case 1 To Cells(1, 4)
            G.Offset = "上班"
            G.Offset.Font.Color = RGB(0, 0, 89)
            G.Interior.Color = RGB(150, 201, 123)
        Case Cells(1, 4) + 1 To Cells(1, 4) + Cells(1, 5)
            If G.Offset >= Cells(1, 1) Then
            G.Offset = "休假"
            G.Offset.Font.Color = RGB(114, 0, 55)
            G.Offset.Interior.Color = RGB(255, 255, 92)
            End If
        End Select
        
        Select Case DateAdd("d", -1, K) Mod Cells(1, 7) + 1 '周輪班
        Case 1 To Cells(1, 4)
            G.Offset = "夜班"
            G.Offset.Font.Color = RGB(114, 0, 55)
            G.Offset.Interior.Color = RGB(255, 208, 0)
        Case Cells(1, 4) + 1 To Cells(1, 4) + Cells(1, 5)
        End Select
 ----------------------
  1. Public Sub 周輪班練習()
  2. Range(Cells(2, 1).End(xlToRight), Cells(2, 1).End(xlDown)).Clear
  3. Cells(1, 3) = Cells(1, 4) + Cells(1, 5)
  4. Cells(1, 7) = Cells(1, 3) * 2
  5.    If Cells(1, 2) = "" Then
  6.       Cells(1, 2) = Year(Date)
  7.    Else
  8.       Cells(1, 2) = Cells(1, 2)
  9.    End If

  10.     S = 3
  11.     E = 1
  12.     For F = 1 To 12 '建立範圍
  13.         For P = 1 To Day(DateSerial(Cells(1, 2), F + 1, 0))
  14.             Cells(S, E) = DateSerial(Cells(1, 2), F, P)
  15.             Cells(S - 1, E) = F & "月" & P & "日" & WeekdayName(Weekday(P))
  16.             E = E + 1
  17.             If P = Day(DateSerial(Cells(1, 2), F + 1, 0)) Then
  18.             If F = 12 Then Exit For
  19.                S = S + 2
  20.                E = 1
  21.             End If
  22.         Next P
  23.     Next F
  24.    
  25.     For E = ActiveWorkbook.Names.Count To 1 Step -1 '清除定義名稱
  26.        If ActiveWorkbook.Names(E).Name <> "" Then
  27.           ActiveWorkbook.Names(E).Delete
  28.        End If
  29.     Next E
  30.    
  31.     Y = 65
  32.     For i = 3 To Cells(3, 1).End(xlDown).Row Step 2 '定義名稱
  33.         範圍名稱 = Chr(Y)
  34.         Names.Add Name:="第" & 範圍名稱 & "項", RefersTo:=Range(Cells(i, 1), Cells(i, 1).End(xlToRight))
  35.         Y = Y + 1
  36.     Next i
  37.    
  38.     Set AWN = ActiveWorkbook.Names '合併
  39.     For R = 1 To AWN.Count
  40.        If R <> 1 Then
  41.           K = Mid(AWN(R).RefersToR1C1Local, 2, Len(AWN(R))) & ","
  42.        Else
  43.           K = AWN(R).RefersToR1C1Local & ","
  44.        End If
  45.         U = U + K
  46.     Next R
  47.     Names.Add Name:="指定範圍", RefersTo:=Mid(U, 1, Len(U) - 1)
  48.    
  49.     For E = ActiveWorkbook.Names.Count To 1 Step -1 '清除定義名稱
  50.        If ActiveWorkbook.Names(E).Name <> "指定範圍" Then
  51.           ActiveWorkbook.Names(E).Delete
  52.        End If
  53.     Next E
  54.    
  55.    If Cells(1, 1) = "" Then
  56.       Cells(1, 1) = Cells(3, 1)
  57.    Else
  58.       Cells(1, 1) = Cells(1, 1)
  59.    End If
  60.    
  61.     D = Cells(1, 1)
  62.    
  63.     For Each G In Range("指定範圍") '周輪班
  64.     If G.Offset >= Cells(1, 1) Then
  65.         If Weekday(G) = 1 Or Weekday(G) = 7 Then '六日上色
  66.            G.Offset(-1, 0).Interior.Color = RGB(172, 199, 213)
  67.         End If
  68.    
  69.         K = G.Offset
  70.         
  71.         Select Case DateAdd("d", -1, K) Mod 6 + 1   '常日班
  72.         Case 1 To 4
  73.             G.Offset = "上班"
  74.             G.Offset.Font.Color = RGB(0, 0, 89)
  75.             G.Interior.Color = RGB(150, 201, 123)
  76.         Case 5 To 6
  77.             If G.Offset >= Cells(1, 1) Then
  78.             G.Offset = "休假"
  79.             G.Offset.Font.Color = RGB(114, 0, 55)
  80.             G.Offset.Interior.Color = RGB(255, 255, 92)
  81.             End If
  82.         End Select
  83.         
  84.         Select Case DateAdd("d", -1, K) Mod 12 + 1 '周輪班
  85.         Case 1 To 4
  86.             G.Offset = "夜班"
  87.             G.Offset.Font.Color = RGB(114, 0, 55)
  88.             G.Offset.Interior.Color = RGB(255, 208, 0)
  89.         Case 5 To 6
  90.         End Select
  91.         
  92.     End If
  93.     Next G
  94.    
  95. End Sub
複製代碼

TOP

本帖最後由 軒云熊 於 2020-6-29 02:29 編輯

問題解決了 >"< 但是這寫法 並不是很好  
後來想出來的辦法是用 顏色 去做判斷
日期的計算 實在沒辦法...真的想不出來
再把最後的顏色數值先存到儲存格
更改下一年之後 再把顏色數值存回變數...
實在想不出辦法的辦法...  希望有更好的辦法
準提大大的版本 排版更是清楚 
但我應該在哪一段修改 要怎麼寫?

TOP

謝謝n7822123大大的指點 讓我想到 把 -1 的位置改一下就可以了
DateAdd("d", -1, K) Mod 12 + 1
老實說我真的看不懂  只能F8 慢慢研究XD
準大的版本對我來說好複雜 ...如果有不懂的地方 再請教n7822123大大還有各位前輩們 ^^"

TOP

本帖最後由 軒云熊 於 2020-7-11 20:05 編輯

請問 有關於月輪班  跨月的部分問題
當一個月 的最後一輪 到 下個月的第一輪
該用怎麼樣的判斷方式 才可以完整的 把一整輪的天數輪完
而不會因為隔月的問題 導致 錯誤
嘗試了計數的方式 還是不行 會卡到月份問題 有的月份天數不一樣
請前輩們 幫幫忙

改版後的班表練習.rar (20.09 KB)

TOP

問題解決了  謝謝準提大大的檔案 還有  n7822123大大的 提點
後來想到的方式  是利用  False 跟 True  的 Boolean 變數  然後再把 輪班的顏色次數先存到 儲存格
再延用 就可以了 XD
  1.                 If Y <= Cells(6, 11) Then
  2.                
  3.                     Select Case DateDiff("d", DateValue(d & "/1/1"), Cells(K, 1)) Mod Cells(5, 11) + 1
  4.                     Case 1 To Cells(3, 11)
  5.                     
  6.                     If Cells(1, 10) <> Year(Date) Then sss = True
  7.                     
  8.                     If yyy <> 0 And yyy <> Cells(3, 11) And sss = True Then
  9.                     
  10.                         Cells(U + 1, W) = "夜班"
  11.                         Cells(U + 1, W).Font.Color = RGB(114, 0, 55)
  12.                         Cells(U + 1, W).Interior.Color = RGB(255, 208, 0)
  13.                         
  14.                         If Cells(U + 1, W) <> "休假" And Cells(U + 1, W) <> "上班" Then yyy = yyy + 1
  15.                         Cells(1, 13) = yyy '儲存延伸天數
  16.                         
  17.                         If yyy = Cells(3, 11) Then
  18.                            yyy = 0
  19.                            sss = False
  20.                         End If
  21.                         
  22.                     Else
  23.                         Cells(U + 1, W) = "上班"
  24.                         Cells(U + 1, W).Font.Color = RGB(0, 0, 89)
  25.                         Cells(U + 1, W).Interior.Color = RGB(150, 201, 123)
  26.                     End If
  27.                     
  28.                         If Cells(U + 1, W) <> "休假" And Cells(U + 1, W) <> "夜班" Then kkk = kkk + 1
  29.                         Cells(1, 12) = kkk '儲存延伸天數
  30.                         If kkk = Cells(3, 11) Then kkk = 0
  31.                         
  32.                     End Select
  33.                     
  34.                 ElseIf Y <= Cells(6, 11) * 2 Then
  35.                
  36.                     Select Case DateDiff("d", DateValue(d & "/1/1"), Cells(K, 1)) Mod Cells(5, 11) + 1
  37.                     Case 1 To Cells(3, 11)
  38.                     
  39.                     If Cells(1, 10) <> Year(Date) Then sss = True
  40.                     
  41.                     If kkk <> 0 And kkk <> Cells(3, 11) And sss = True Then
  42.                         
  43.                         Cells(U + 1, W) = "上班"
  44.                         Cells(U + 1, W).Font.Color = RGB(0, 0, 89)
  45.                         Cells(U + 1, W).Interior.Color = RGB(150, 201, 123)
  46.                         
  47.                         If Cells(U + 1, W) <> "休假" And Cells(U + 1, W) <> "夜班" Then kkk = kkk + 1
  48.                         Cells(1, 12) = kkk '儲存延伸天數

  49.                         If kkk = Cells(3, 11) Then
  50.                            kkk = 0
  51.                            sss = False
  52.                         End If
  53.                         
  54.                     Else
  55.                         Cells(U + 1, W) = "夜班"
  56.                         Cells(U + 1, W).Font.Color = RGB(114, 0, 55)
  57.                         Cells(U + 1, W).Interior.Color = RGB(255, 208, 0)
  58.                     End If
  59.                     
  60.                         If Cells(U + 1, W) <> "休假" And Cells(U + 1, W) <> "上班" Then yyy = yyy + 1
  61.                         Cells(1, 13) = yyy '儲存延伸天數
  62.                         If yyy = Cells(3, 11) Then yyy = 0
  63.                            
  64.                     End Select
  65.                     
  66.                 End If
複製代碼
謝謝大大們的指導

TOP

        靜思自在 : 人事的艱難與琢磨,就是一種考驗。
返回列表 上一主題