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:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try :
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
Application.EnableEvents = False
TextBox1.Value = vbNullString
TextBox1.SetFocus
Application.EnableEvents = True
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
Try :
Code:
[COLOR=#333333]Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)[/COLOR]
[COLOR=#FF0000]Application.EnableEvents = False
[/COLOR][COLOR=#333333]If Mid(TextBox1.Value, 4, 2) > 12 Then[/COLOR]
[COLOR=#333333]Cancel = True[/COLOR]
[COLOR=#333333]MsgBox "Invalid date, please re-enter", vbCritical[/COLOR]
[COLOR=#333333]TextBox1.Value = vbNullString[/COLOR]
[COLOR=#333333]TextBox1.SetFocus[/COLOR]
[COLOR=#ff0000]Application.EnableEvents = True[/COLOR]
[COLOR=#333333]Exit Sub[/COLOR]
[COLOR=#333333]End If[/COLOR]
[COLOR=#333333]dDate = DateSerial(Year(Date), Month(Date), Day(Date))[/COLOR]
[COLOR=#333333]TextBox1.Value = Format(CDate(TextBox1.Value), "dd-mmm-yyyy")[/COLOR]
[COLOR=#FF0000]Application.EnableEvents = True
[/COLOR][COLOR=#333333]End Sub[/COLOR]

If still no good, put a breakpoint on the first line (F9) then step through the code (F8) to see where the macro is being triggered twice.
 
Upvote 0
No Footoo Everything tried but still no success

It exits at Exits Sub and cant see the macro being triggered twice
 
Upvote 0
How are you closing the userform?

PS Application.EnableEvents has no bearing on a userform, but what you could do is introduce your own code to disable events.
 
Upvote 0
Well Norie this is the only code in Userform.

PS Application.EnableEvents has no bearing on a userform, but what you could do is introduce your own code to disable events.
I thought so as nothing happened
What is the code to disable events. in just simple matter like this
 
Upvote 0
just tried
Code:
If Mid(TextBox1.Value, 4, 2) > 12 Then
        Cancel = True
        MsgBox "Invalid date, please re-enter", vbCritical
        TextBox1.Value = vbNullString
        TextBox1.SetFocus
[COLOR=#ff0000]                Cancel = False[/COLOR]
        Exit Sub
    End If
    dDate = DateSerial(Year(Date), Month(Date), Day(Date))
    TextBox1.Value = Format(CDate(TextBox1.Value), "dd-mmm-yyyy")
    dDate = TextBox1.Value
What happens is now not triggering twice Ok. but focus on textbox1 is not there anymore
Any where Cancel = False you incorporate above, the Focus from Textbox1 is not there
 
Last edited:
Upvote 0
Try running it as an Exit event
Code:
Private Sub TextBox1_Exit(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
 
Upvote 0
Yes Fluff tried in exit event but error at following line
Type Mismatch
TextBox1.Value = Format(CDate(TextBox1.Value), "dd-mmm-yyyy")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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