message box show twice before is gone , why?!

abdo meghari

Active Member
Joined
Aug 3, 2021
Messages
465
Office Version
  1. 2019
Hello
I need help to fix my problem .
when write in textbox2 and the optionbutton1,2 are not selected then should show message and clear textbox2 , but it will show the message twice before clear textbox2 !
VBA Code:
Private Sub TextBox2_Change()
If Not (OptionButton1.Value) * (OptionButton2.Value) Then
MsgBox "please select one button at least": TextBox2.Value = ""
Exit Sub
Else
Call FilterData
End If

End Sub
thanks in advanced
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Because, after the MessageBox shows you change TextBox2.Value which calls the TextBox2_Change again before the Sub ends the fist time.
 
Upvote 0
Try adding the two lines I show (one at the beginning and one at the end) and see if that solves your problem...
VBA Code:
Private Sub TextBox2_Change()

  Application.EnableEvents = False
 
  If Not (OptionButton1.Value) * (OptionButton2.Value) Then
    MsgBox "please select one button at least": TextBox2.Value = ""
    Exit Sub
  Else
    Call FilterData
  End If
 
  Application.EnableEvents = True

End Sub
 
Upvote 0
As @Skyybot has already mentioned, changing the value for TextBox2.value triggers the change event again. And, unfortunately, setting EnableEvents to False won't disable events within a userform. However, you can use a module-level variable to help you avoid triggering the code again when the textbox value is changed. By the way, the following line of code will always evaluate to true...

VBA Code:
If Not (OptionButton1.Value) * (OptionButton2.Value) Then

So maybe try something like this...

VBA Code:
Option Explicit

Dim m_locked As Boolean

Private Sub TextBox2_Change()

    If m_locked = True Then Exit Sub

    If OptionButton1.Value = False And OptionButton2.Value = False Then
        m_locked = True
        MsgBox "please select one button at least": TextBox2.Value = ""
        m_locked = False
        Exit Sub
    Else
        Call FilterData
    End If

End Sub

Private Sub UserForm_Initialize()

    m_locked = False
  
End Sub

Hope this helps!
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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