Change Event of Textbox Was Not Firing on Second Userform

zzws524

New Member
Joined
Jun 8, 2015
Messages
6
[FONT=Tahoma, Microsoft Yahei, Simsun]I created 2 userforms. Each userform has a text box. When the last letter of textbox is “E", close current userform, and open another one. [/FONT]

However, when second userform is loaded, the change event was not working.

Can anybody tell me why or how to fix it? Thanks in advance.


Code for textbox on 1st userform:

Code:
[FONT=Trebuchet MS]Private Sub TextBox1_Change()[/FONT]
[I][FONT=Trebuchet MS]If (Right(UserForm1.TextBox1, 1) = "E") Then[/FONT]
[I][FONT=Trebuchet MS]Unload Me[/FONT]
[I][FONT=Trebuchet MS]Load UserForm2[/FONT]
[I][FONT=Trebuchet MS]UserForm2.Show[/FONT]
[I][FONT=Trebuchet MS]End If[/FONT]
[I][FONT=Trebuchet MS]End Sub[/FONT]

Code for texitbox on 2nd form. It' similar to 1st form.

Code:
[I][FONT=Trebuchet MS]Private Sub TextBox1_Change()[/FONT]
[I][FONT=Trebuchet MS]If (Right(UserForm2.TextBox1, 1) = "E") Then[/FONT]
[I][FONT=Trebuchet MS]Unload Me[/FONT]
[I][FONT=Trebuchet MS]Load UserForm1[/FONT]
[I][FONT=Trebuchet MS]UserForm1.Show[/FONT]
[I][FONT=Trebuchet MS]End If[/FONT]
[I][FONT=Trebuchet MS]End Sub[/FONT][/I][/I][/I][/I][/I][/I][/I]
[/I][/I][/I][/I][/I][/I]
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Use Ucase()

Code:
If UCase(Right(UserForm1.TextBox1, 1) = "E") Then

Code:
If UCase(Right(UserForm2.TextBox1, 1) = "E") Then
 
Upvote 0
I tested it and it is working fine at my end.

Also, instead of using the userform names you can simply replace it with the word Me like this...

Code:
Me.TextBox1

Me refers to the form on which the control resides.
 
Upvote 0
Do you mean you can use second form to call first form back? I tried different PC but no luck...May i send my excel to you?

Thanks, Sixthsense.
 
Upvote 0
Instead of using UserForm1.TextBox & UserForm2.TextBox you can simply use Me.TextBox since both the controls resides in the active userform.

I suspect you may be named the Textbox names differently that may be the cause for the event not getting triggered.
 
Upvote 0
I tried. However, it didn't work. It's weird that if i run userform2 first, the change event is working on it, but it will failed on userform1.
Anyway, i've sent you my excel. Thanks.
 
Upvote 0
i change the sequence a little bit. I think it can give us some hints to this problem.

Please refer to the code. "step1" and "step2" were popped up when I enter "E". userform2 were pop up simultaneously. However, "step3" was not popped up! And userform1 was NOT unloaded. Until i close userform2, "step3" will be popped up.

I think that means when new userform is created, system is still monitoring the change event of previous textbox.

Code:
Private Sub TextBox1_Change()
If UCase((Right(Me.TextBox1, 1) = "E")) Then
MsgBox ("step1")
Load UserForm2
MsgBox ("step2")
UserForm2.Show
MsgBox ("step3")
Unload Me
End If
End Sub
 
Upvote 0
The .Show command can suspend code execution. I don't fully appreciate all the reasoning and circumstances. A workaround is to show the form as vbModeless

UserForm1:
Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] TextBox1_Change()
    [COLOR=darkblue]If[/COLOR] Right(Me.TextBox1, 1) = "E" [COLOR=darkblue]Then[/COLOR]
        Unload Me
        UserForm2.Show [B]vbModeless[/B]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Sub[/COLOR]

UserForm2:
Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] TextBox1_Change()
    [COLOR=darkblue]If[/COLOR] Right(Me.TextBox1, 1) = "E" [COLOR=darkblue]Then[/COLOR]
        Unload Me
        UserForm1.Show [B]vbModeless[/B]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Sub[/COLOR]

I also read somewhere to use Unload Me or Me.Hide but not both at the same time.
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,314
Members
449,081
Latest member
tanurai

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