I have 2 text boxes in my user form.
1- txtpresencehours
2- txtproductionhours
I want production hours value does not exceed presence hour (users can not enter)and if it happened a warning message window pops up
1-What if i have for example 50 textboxes in my form, is there a a code just to put the boxes name in it instead of writing this code for each boxCode:Private Sub txtPresenceHours_Afterupdate() Dim tString As String With txtPresenceHours 'Check if user put in a colon or not If InStr(1, .Value, ":", vbTextCompare) = 0 And Len(.Value) > 1 Then 'If not, make string 4 digits and insert colon tString = Format(.Value, "0000") tString = Left(tString, 2) & ":" & Right(tString, 2) txtPresenceHours.Value = Format(TimeValue(tString), "hh:mm") Else 'Otherwise, take value as given .Value = Format(.Value, "hh:mm") End If End With End Sub Private Sub txtProductionHours_AfterUpdate() Dim tString As String With txtProductionHours 'Check if user put in a colon or not If InStr(1, .Value, ":", vbTextCompare) = 0 And Len(.Value) > 1 Then 'If not, make string 4 digits and insert colon tString = Format(.Value, "0000") tString = Left(tString, 2) & ":" & Right(tString, 2) txtProductionHours.Value = Format(TimeValue(tString), "hh:mm") Else 'Otherwise, take value as given .Value = Format(.Value, "hh:mm") End If End With If txtProductionHours.Value > txtPresenceHours.Value Then MsgBox "Productuin hours must not exceed Presence hours", vbExclamation, "Userform1" Me.txtProductionHours.SetFocus Exit Sub End If End Sub
2- Above code gives error for value higher than 23:59 ,also I don't want to round to days and hours i want them to be shown as they are. I changed the format to [h]:mm but didn't workCode:Private Sub txtPresenceHours_Afterupdate() Dim tString As String With txtPresenceHours 'Check if user put in a colon or not If InStr(1, .Value, ":", vbTextCompare) = 0 And Len(.Value) > 1 Then 'If not, make string 4 digits and insert colon tString = Format(.Value, "0000") tString = Left(tString, 2) & ":" & Right(tString, 2) txtPresenceHours.Value = Format(TimeValue(tString), "hh:mm") Else 'Otherwise, take value as given .Value = Format(.Value, "hh:mm") End If End With




Reply With Quote
