Is it possible to do this in the code below. If the StartTime or EndTime numeric hour…
- User
Is it possible to do this in the code below. If the StartTime or EndTime numeric hour and minute values are seprated by a syntax other than '.', Then MsgBox "Hour and minute can only be seperated by '.'", vbExclamation, "Validation Error" Exit Sub
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 adrng = wt.Range("AD3:AD2003") For Each cell In adrng 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 aerng = wt.Range("AE3:AE2003") For Each cell In aerng 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
Yes, it is possible to add the validation check for the syntax of the StartTime and EndTime values in the code. You can add the following code at the beginning of the subroutine:
VBAPrivate Sub StartEndButton1_Click() ' Check if hour and minute values are separated by '.' If (InStr(StartTime.Value, ".") = 0 Or InStr(EndTime.Value, ".") = 0) And (StartTime.Value <> "" And EndTime.Value <> "") Then MsgBox "Hour and minute can only be separated by '.'", vbExclamation, "Validation Error" Exit Sub End If ' Rest of your code... End SubThis code uses the
InStrfunction to check if the '.' character exists in the StartTime and EndTime values. If it does not exist and both StartTime and EndTime are not empty, it will display the validation error message and exit the subroutine. - User
Will the message at the end of this code be part of the loop or will the message pop up at the end of the loop : If Target.CountLarge > 1 Then Exit Sub If Target.Column = 15 And Target.Value = "Calculated" Then ' Column O Target.Offset(0, 1).Value = Date Dim rowAddress As Integer rowAddress = Target.Row Dim lastValue As Variant lastValue = Cells(Rows.Count, 1).End(xlUp).Value ' Last value in column A Dim stopRow As Integer stopRow = rowAddress Dim c As Integer For c = rowAddress To 1 Step -1 If IsEmpty(Range("A" & c).Value) Then Range("A" & c).Value = lastValue Else Exit For ' Exit loop if a non-empty cell is encountered End If Next c MsgBox "Enter Staff Names in column I, K, M for the new month" End If
- Assistant
The message at the end of the code will only be displayed once, after the loop has finished executing. The loop will continue executing until a non-empty cell is encountered in column A or until it reaches the first row (row 1).
The
Exit Forstatement inside the loop will exit the loop immediately when a non-empty cell is encountered, so the code after the loop (including the message box) will only be executed once, after the loop has ended.