Getting Msgbox Error displayed Twice when closing userform

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
688
Hello

i am getting Msgbox Error Twice when i close the userform

Can someone check and help me to get rid of the MsgboxError when i close the userform

Basically if the date is wrong for which
I puroposely incorporated Cancel = True in the If EndIf so that textbox1 is set focussed

but when i close the userform after the executing the above. The msgbox Error is displayed not once but Twice

if the date is right and closing the userform then no message

Code in Userform
Code:
Option Explicit
Public dDate As Date


Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

If Mid(TextBox1.Value, 4, 2) > 12 Then
        Cancel = True
        MsgBox "Invalid date, please re-enter", vbCritical
        TextBox1.Value = vbNullString
        TextBox1.SetFocus
        Exit Sub
    End If
    dDate = DateSerial(Year(Date), Month(Date), Day(Date))
    TextBox1.Value = Format(CDate(TextBox1.Value), "dd-mmm-yyyy")
End Sub
Thanks NimishK
 
Last edited:
Maybe :
Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If textbox1.Value = vbNullString Then
    Exit Sub
ElseIf Not IsDate(CDate(textbox1.Value)) Then
    Cancel = True
    textbox1.Value = vbNullString
    textbox1.SetFocus
    MsgBox "Invalid date, please re-enter", vbCritical
    Exit Sub
End If
dDate = DateSerial(Year(Date), Month(Date), Day(Date))
textbox1.Value = Format(CDate(textbox1.Value), "dd-mmm-yyyy")
End Sub
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Another option
Code:
Option Explicit
Public dDate As Date
Dim DisableEvents As Boolean


Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
   If DisableEvents Then Exit Sub
   If Mid(TextBox1.Value, 4, 2) > 12 Or Not IsDate(Me.TextBox1.Value) Then
      Cancel = True
      MsgBox "Invalid date, please re-enter", vbCritical
      TextBox1.Value = vbNullString
      TextBox1.SetFocus
      Exit Sub
   End If
   dDate = DateSerial(Year(Date), month(Date), day(Date))
   TextBox1.Value = Format(CDate(TextBox1.Value), "dd-mmm-yyyy")
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
DisableEvents = True
End Sub
 
Upvote 0
Fluff
Yes 100% accurate.
Thanks you so much :beerchug:

Footoo Dear,

Tried yours got Type Mismatch at
ElseIf Not IsDate(CDate(TextBox1.Value)) Then

Sincere request Footoo Please try the code at your End and reply. Anyways Thanks:)
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Maybe :
Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If textbox1.Value = vbNullString Then
    Exit Sub
ElseIf Not IsDate(CDate(textbox1.Value)) Then
    Cancel = True
    textbox1.Value = vbNullString
    textbox1.SetFocus
    MsgBox "Invalid date, please re-enter", vbCritical
    Exit Sub
End If
dDate = DateSerial(Year(Date), Month(Date), Day(Date))
textbox1.Value = Format(CDate(textbox1.Value), "dd-mmm-yyyy")
End Sub
Don't know how it happened but the Msgbox line got moved. Should be :
Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If textbox1.Value = vbNullString Then
    Exit Sub
ElseIf Not IsDate(CDate(textbox1.Value)) Then
    Cancel = True
    MsgBox "Invalid date, please re-enter", vbCritical
    textbox1.Value = vbNullString
    textbox1.SetFocus
    Exit Sub
End If
dDate = DateSerial(Year(Date), Month(Date), Day(Date))
textbox1.Value = Format(CDate(textbox1.Value), "dd-mmm-yyyy")
End Sub
 
Upvote 0
Footoo Dear,

Tried yours got Type Mismatch at
ElseIf Not IsDate(CDate(TextBox1.Value)) Then

Sincere request Footoo Please try the code at your End and reply. Anyways Thanks:)

ElseIf Not IsDate(TextBox1.Value) Then
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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