use one code for multiple uses

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,075
Office Version
  1. 2019
Platform
  1. Windows
I have this code that will inform the user they have enter an invalid time.

I have 20 textboxes that requires validating the correct time. Can someone assist with editing this code to allow me to use this 1 code in all 20 textboxes. Thanks for your help.

Code:
Private Sub txtReport_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim t

If Len(Me.txtReport) Then
    t = Me.txtReport
    If Not ISTIME(t) Then
        MsgBox "Please enter a valid time."
        txtReport = ""
        Cancel = True
    End If
End If
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this function.

Code:
Function InvalidTime(TB As MSForms.TextBox) As Boolean
    If Len(TB.Text) Then
        If Not IsTime(TB.Text) Then
            MsgBox "Please enter a valid time."
            TB.Text = ""
            InvalidTime = True
        End If
    End If
End Function


Call it this way in each of your textbox subs.

Code:
Private Sub txtReport_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Cancel = InvalidTime(txtReport)
End Sub

It returns True if the time is invalid, and false if it is valid. Kind of backwards, but that is so you can set the cancel correctly.
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,421
Members
452,913
Latest member
JWD210

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top