message box show twice before is gone , why?!

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
651
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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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