Can you please write me a VBA code that will work with the form code below. What I want…
- User
Can you please write me a VBA code that will work with the form code below. What I want the new code to do is; when the values have been written to AD & AE, force AF on the same row to calculate, then check if the value in column B of the same row is the same value as that in AF of the same row. If the values are the same then Pop up a message "Overtime hours have been recorded" If the values are not the same pop up a critical message like this: "Incorrect Hours recorded", line break "Hours calculated" & column AF value, line break "Hours Entered" & column B value, line break "Please check your entry"
Here is the current Form code: Private newActiveCellRange As Range
Public Property Let activeCellRange(rng As Range) Set newActiveCellRange = rng End Property
Public Property Get activeCellRange() As Range Set activeCellRange = newActiveCellRange End Property
Private Sub UserForm_Initialize() Me.activeCellRange = ActiveCell End Sub
Private Sub StartEndButton1_Click() newActiveCellRange.Offset(0, 28).Value = StartTime.Value newActiveCellRange.Offset(0, 29).Value = EndTime.Value Me.Hide End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Set activeCellRange = Target End Sub
- Assistant
Sure! Here's the VBA code that will work with the form code you provided:
Private Sub StartEndButton1_Click() newActiveCellRange.Offset(0, 28).Value = StartTime.Value newActiveCellRange.Offset(0, 29).Value = EndTime.Value newActiveCellRange.Offset(0, 30).Formula = "=IF(B" & newActiveCellRange.Row & "=AF" & newActiveCellRange.Row & ", ""Overtime hours have been recorded"", ""Incorrect Hours recorded" & vbCrLf & "Hours calculated: "" & AF" & newActiveCellRange.Row & vbCrLf & "Hours entered: "" & B" & newActiveCellRange.Row & vbCrLf & "Please check your entry"")" Me.Hide End SubMake sure to replace the code of the existing
StartEndButton1_Click()subroutine with the code above. This code will write the values from the form to columns AD and AE, and then calculate the formula in column AF using the values from columns B and AF. It will compare the values and display the appropriate message based on the comparison. - User
There is already a formula in AF that calculates the difference of the values entered in the values entered. What I want is for the code to check if the value in AF matches the value in B. If they match then pop up the first message, if they do not match then pop up the second message
- Assistant
Apologies for misunderstanding your requirements. Here's the modified code that checks if the calculated value in column AF matches the value in column B:
Private Sub StartEndButton1_Click() newActiveCellRange.Offset(0, 28).Value = StartTime.Value newActiveCellRange.Offset(0, 29).Value = EndTime.Value Dim calculatedHours As Double calculatedHours = newActiveCellRange.Offset(0, 30).Value If calculatedHours = newActiveCellRange.Offset(0, 1).Value Then MsgBox "Overtime hours have been recorded" Else MsgBox "Incorrect Hours recorded" & vbCrLf & _ "Hours calculated: " & calculatedHours & vbCrLf & _ "Hours entered: " & newActiveCellRange.Offset(0, 1).Value & vbCrLf & _ "Please check your entry" End If Me.Hide End SubThis code saves the calculated value in column AF to the variable
calculatedHours. Then, it checks ifcalculatedHoursmatches the value in column B. If they match, it displays the message "Overtime hours have been recorded". If they do not match, it displays the message "Incorrect Hours recorded" along with the calculated and entered values.Please replace the existing subroutine with the modified code above.