![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Mar 2002
Posts: 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.
|
|
|
|
|
|
#2 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
|
Quote:
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
D |
|
|
|
|
|
|
#3 |
|
New Member
Join Date: Mar 2002
Posts: 5
|
|
|
|
|
|
|
#4 |
|
New Member
Join Date: Mar 2002
Posts: 5
|
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.
|
|
|
|
|
|
#5 |
|
Banned
Join Date: Feb 2002
Posts: 1,582
|
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 |
|
|
|
|
|
#6 |
|
New Member
Join Date: Mar 2002
Posts: 5
|
Thanks alot for the suggestion. It works perfectly. I much appreciate your help.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|