我有一个记录员工工作时间的模板。第 5 栏显示了他们每周的合同工时,第 14 栏显示了他们的额外工作时间。加班的兼职员工(每周少于 37.5 小时)按标准收费。但是,一旦他们超过一周的 37.50 小时,他们就会按时支付一半(这记录在单独的栏中)。
下面的代码获取一周的总小时数(第 18 列),如果超过 37.5,它将提示用户在一个半小时记录一些小时数。这是确保人们得到正确报酬的一种万无一失的方法。
下面的代码几乎可以完美运行,但是如果约定的小时数少于 10,则无论如何都会弹出消息框。我认为这是因为我有一个字符串数据类型,代码中的小时数是字符串,但我似乎无法让它与其他数据类型一起使用。任何帮助将不胜感激。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = 14 Then
Dim I As Integer, CheckHours As Boolean
Dim MonthX As Worksheet
I = 6
CheckHours = False
Set MonthX = ThisWorkbook.ActiveSheet
Dim FT As String
FT = 37.5
Application.ScreenUpdating = False
'Use the Employee Number column to perform the check
Do While MonthX.Cells(I, 3) <> ""
'Declare variables
Dim ContractHours As String
Dim HoursPaid As String
Dim TotalHours As String
ContractHours = MonthX.Cells(I, 5)
HoursPaid = MonthX.Cells(I, 14)
TotalHours= MonthX.Cells(I, 18)
'If the contract hours plus the additional hours are greater than 37.50 then display warning
If TotalHours > FT Then
MsgBox "WARNING: Check the additional hours entered for " & _
MonthX.Cells(I, 2).Value & " " & MonthX.Cells(I, 1).Value & _
" as they will need to be split between Additional Basic and Overtime." & _
vbNewLine & vbNewLine & _
"Please refer to the Additional Hours Guidelines tab for more information.", vbOKOnly, "Please Check"
CheckHours = True
End If
I = I + 1
Loop
'Cancel boolean
If CheckHours = True Then
Cancel = True
End If
Application.ScreenUpdating = True
End If
End Sub