Validating user form textboxes

tallpaul321

New Member
Joined
Mar 7, 2002
Messages
5
I have a textbox on a userform in visual basics which will have the date manually typed into it. When the OK button is pressed the date is cut and pasted into a cell in Excel. Is there any way to validate the textbox to prevent an incorrect date being inserted? I have tried validating the cell that the date is pasted into but if an incorrect date is entered the debug box is displayed.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I have a textbox on a userform in visual basics which will have the date manually typed into it. When the OK button is pressed the date is cut and pasted into a cell in Excel. Is there any way to validate the textbox to prevent an incorrect date being inserted? I have tried validating the cell that the date is pasted into but if an incorrect date is entered the debug box is displayed.

Hello,

You could use some code like this to ensure that a date has been entered:-

Code:
Private Sub cmdOK_Click()
If IsDate(Me.txtDate) = False Then
    MsgBox "A valid date must be entered", vbExclamation, "Invalid Entry"
    Me.txtDate.Text = ""
    Me.txtDate.SetFocus
    Exit Sub
End If

'Rest of your code will execute here if a valid date has been entered

End Sub

Hope it helps,

D
 
Upvote 0
hi, Thanks for the code. I have inserted it into the userform. The only problem is that even if i enter a valid date the debug appears and the .Txtdate in 'If(Me.txtdate)' is highlighted in blue. A message box appears saying "Comppile Error: Method or data member not found". Do you have any suggestions as to how i can ammend this problem? Thanks in advance for any help you can provide me with.
 
Upvote 0
Hi

Try this

Private Sub CommandButton1_Click()
If Not IsDate(TextBox1.Value) Then
TextBox1.SetFocus
MsgBox "A valid date must be entered", vbExclamation, "Invalid Entry"
TextBox1 = vbNullString
Else
Sheet1.Range("A65536").End(xlUp).Offset(1, 0) = TextBox1.Value
TextBox1 = vbNullString
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,142
Members
448,551
Latest member
Sienna de Souza

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