In the VBA code bellow, I have added additional coding to the Form code after the…
- User
In the VBA code bellow, I have added additional coding to the Form code after the statement ' NEW CODE ADDED Can you please check and see if this will work.
Private Sub StartEndButton1_Click()
If StartTime.Value = "" And EndTime.Value = "" And AorH.Value = "" Then MsgBox "Please fill in either Start Time and End Time or AorH", vbExclamation, "Validation Error" Exit Sub End If
If StartTime.Value <> "" And EndTime.Value <> "" Then If StartTime.Value >= EndTime.Value Then MsgBox "Start Time must be less than End Time", vbExclamation, "Validation Error" Exit Sub End If End If
If (StartTime.Value <> "" And EndTime.Value = "") Or (StartTime.Value = "" And EndTime.Value <> "") Then MsgBox "Both Start Time and End Time must be filled", vbExclamation, "Validation Error" Exit Sub End If
If (StartTime.Value <> "" Or EndTime.Value <> "") And AorH.Value <> "" Then MsgBox "If Start Time or End Time is filled, AorH must be blank", vbExclamation, "Validation Error" Exit Sub End If
If AorH.Value <> "" And AorH.Value <> "a" And AorH.Value <> "h" Then MsgBox "AorH can only be 'a' or 'h'", vbExclamation, "Validation Error" Exit Sub End If
If FullName.Value = "" Then MsgBox "Employee Full Name must be entered", vbExclamation, "Validation Error" Exit Sub End If
newActiveCellRange.Offset(0, 29).Value = StartTime.Value newActiveCellRange.Offset(0, 30).Value = EndTime.Value newActiveCellRange.Offset(0, 32).Value = AorH.Value newActiveCellRange.Offset(0, 2).Value = FullName.Value
' NEW CODE ADDED
Dim wt As Worksheet Dim rng As Range Dim cell As Range Set wt = ThisWorkbook.Worksheets("Overtime") ' Check cell values in AD3:AD2003 Set wt.rng = Range("AD3:AD2003") For Each cell In wt.rng If Right(cell.Value, 3) = ".15" Then cell.Value = Left(cell.Value, Len(cell.Value) - 3) & ".25" ElseIf Right(cell.Value, 3) = ".30" Then cell.Value = Left(cell.Value, Len(cell.Value) - 3) & ".50" ElseIf Right(cell.Value, 3) = ".45" Then cell.Value = Left(cell.Value, Len(cell.Value) - 3) & ".75" End If Next cell ' Check cell values in AE3:AE2003 Set wt.rng = Range("AE3:AE2003") For Each cell In wt.rng If Right(cell.Value, 3) = ".15" Then cell.Value = Left(cell.Value, Len(cell.Value) - 3) & ".25" ElseIf Right(cell.Value, 3) = ".30" Then cell.Value = Left(cell.Value, Len(cell.Value) - 3) & ".50" ElseIf Right(cell.Value, 3) = ".45" Then cell.Value = Left(cell.Value, Len(cell.Value) - 3) & ".75" End If Next cell ' Calculate formulas wt.Range("AF3:AF2003").Calculate wt.Range("AH3:AH2003").Calculate wt.Range("B3:B2003").Calculate
'newActiveCellRange.Offset(0, 31).Calculate 'newActiveCellRange.Offset(0, 33).Calculate 'newActiveCellRange.Offset(0, 1).Calculate newActiveCellRange.Offset(0, 3).Select
Me.Hide
MsgBox "Details recorded, Enter Reason", vbInformation, "OVERTIME HOURS"
End Sub
- Assistant
The additional code you added appears to be functional. It checks the values in cells AD3:AD2003 and AE3:AE2003, and replaces the last 3 characters (.15, .30, or .45) with .25, .50, or .75, respectively. It then calculates the formulas in ranges AF3:AF2003, AH3:AH2003, and B3:B2003.
The commented lines that calculate formulas in newActiveCellRange.Offset(0, 31), newActiveCellRange.Offset(0, 33), and newActiveCellRange.Offset(0, 1) are not being used, but if they are valid formulas, you can uncomment them.
Finally, the code selects newActiveCellRange.Offset(0, 3), hides the form, and displays a message box.
Overall, the code should work as intended, but please test it thoroughly to ensure it performs as expected.