返回列表 上一主題 發帖

[發問] 請問編排勤務班表問題

小弟初試VBA,請您指教
Sub s1()
    Worksheets("勤務表").Select
    For j = 0 To 23
            Cells(9 + j, 19) = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,"
        For i = 0 To 11
              '刪除輪休
              If Cells(34, 4 + i) <> "" Then
                  Cells(9 + j, 19) = Replace(Cells(9 + j, 19), Cells(34, 4 + i), "")
              End If
            
              '刪除外宿
              If Cells(35, 4 + i) <> "" Then
                  Cells(9 + j, 19) = Replace(Cells(9 + j, 19), Cells(35, 4 + i), "")
              End If
            
              '刪除差假
              If Cells(34, 19 + i) <> "" Then
                  Cells(9 + j, 19) = Replace(Cells(9 + j, 19), Cells(34, 19 + i), "")
              End If
              
              '刪除休假
              If Cells(35, 19 + i) <> "" Then
                  Cells(9 + j, 19) = Replace(Cells(9 + j, 19), Cells(35, 19 + i), "")
              End If
        Next i
        
        '刪除已派
        For i = 0 To 13
              If Cells(9 + j, 5 + i) <> "" Then
                  Cells(9 + j, 19) = Replace(Cells(9 + j, 19), Cells(9 + j, 5 + i), "")
              End If
        Next i
        For i = 0 To 1
             If Cells(9 + j, 29 + i) <> "" Then
                  Cells(9 + j, 19) = Replace(Cells(9 + j, 19), Cells(9 + j, 29 + i), "")
              End If
        Next i


    Next j
End Sub

TOP

回復 1# 馬納
第二版VBA程式碼放在Worksheet_SelectionChange中,只要該時段有輪休、外宿.....,在設勤務時可用下拉式清單

TOP

回復 1# 馬納
我完成的第二版,程式碼如下,請指教!

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    For j = 0 To 23
            Cells(9 + j, 19) = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,"
        For i = 0 To 11
              '刪除輪休
              If Cells(34, 4 + i) <> "" Then
                  Cells(9 + j, 19) = Replace(Cells(9 + j, 19), Cells(34, 4 + i) & ",", "")
              End If
            
              '刪除外宿
              If Cells(35, 4 + i) <> "" Then
                  Cells(9 + j, 19) = Replace(Cells(9 + j, 19), Cells(35, 4 + i) & ",", "")
              End If
            
              '刪除差假
              If Cells(34, 19 + i) <> "" Then
                  Cells(9 + j, 19) = Replace(Cells(9 + j, 19), Cells(34, 19 + i) & ",", "")
              End If
              
              '刪除休假
              If Cells(35, 19 + i) <> "" Then
                  Cells(9 + j, 19) = Replace(Cells(9 + j, 19), Cells(35, 19 + i) & ",", "")
              End If
        Next i
        
        '刪除已派
        For i = 0 To 13
              If Cells(9 + j, 5 + i) <> "" Then
                  Cells(9 + j, 19) = Replace(Cells(9 + j, 19), Cells(9 + j, 5 + i) & ",", "")
              End If
        Next i
        For i = 0 To 1
             If Cells(9 + j, 29 + i) <> "" Then
                  Cells(9 + j, 19) = Replace(Cells(9 + j, 19), Cells(9 + j, 29 + i) & ",", "")
              End If
        Next i
        
        
        '設定職務派任清單
        With Range(Cells(9 + j, 4), Cells(9 + j, 15)).Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Cells(9 + j, 19)
        End With
        With Range(Cells(9 + j, 29), Cells(9 + j, 30)).Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Cells(9 + j, 19)
        End With
    Next j
End Sub

TOP

        靜思自在 : 為自己找藉口的人永遠不會進步。
返回列表 上一主題