SetFocus to newly visible TextBox

DavidPC

New Member
Joined
Apr 23, 2019
Messages
8
I have a UserForm with the first TextBox that if any text is entered then two new textbooks become visible. The problem is that I cannot get the Focus to move to the first of these just visible Textboxes. I have tried many things but unsuccessfully. Any help or ideas would be welcomed.
My simplified code is:

Option Explicit


Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Value <> "" Then
TextBox2.Visible = True
TextBox3.Visible = True
End If
End Sub


Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "TextBox2_Exit ", TextBox2
End Sub


Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "TextBox3_Exit ", TextBox3
End Sub


Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "TextBox4_Exit ", TextBox4
End Sub


Private Sub CommandButton1_Click()
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
TextBox2.Visible = False
TextBox3.Visible = False
TextBox1.SetFocus
End Sub

Thanks
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Have you tried putting the visibility code in the Change event?
Code:
Private Sub TextBox1_Change()
    TextBox2.Visible = (TextBox1.Value <> "")
    TextBox3.Visible = (TextBox1.Value <> "")
End Sub
 
Upvote 0
Have you tried putting the visibility code in the Change event?
Code:
Private Sub TextBox1_Change()
    TextBox2.Visible = (TextBox1.Value <> "")
    TextBox3.Visible = (TextBox1.Value <> "")
End Sub

Thanks for your quick reply. That works fine in my simplified test so I'll now try it in the full code.
I have been trying for hours to fix this so I really appreciate your help.

David
 
Upvote 0
Yes it works in my full code.
How did you know this, is there something i've missed in learning VBA?
Thanks again
David
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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