How to setfocus on Textbox1 when UserForm is Modeless?

JohnGow383

Board Regular
Joined
Jul 6, 2021
Messages
141
Office Version
  1. 2013
Platform
  1. Windows
Hi. I have a very simple problem but can't seem to resolve it so thought I'd ask here.
I have userforms where I want the cursor to blink on Textbox1 when the form is first opened. I have a test button for now but the various forms will automatically open when first opening the spreadsheet depending on what condition is met. I have tried the following codes which should work.
VBA Code:
With Me.TextBox1
    .SetFocus
   End With
and
VBA Code:
TextBox1.SetFocus
and
VBA Code:
Userform1.TextBox1.SetFocus

I have these in intialize and also in Userform_Activate
VBA Code:
Private Sub UserForm_Activate()
TextBox1.SetFocus

End Sub
No matter what I try I can't seem to get Textbox 1 to show the flashing cursor.
When I set the userform to ShowModal = TRUE then it works perfectly. I need to have it modeless as there are a couple of buttons on the userform that are hyperlinked to other spreadsheets. These open fine, but the users are telling me if they then close the other spreadsheets the the one with the userform also closes without saving.

I'm no doubt missing something obvious. Thanks in advance
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try adding a DoEvents statement

VBA Code:
Private Sub UserForm_Activate()
    Me.TextBox1.SetFocus
    DoEvents
End Sub
 
Upvote 0
Try adding a DoEvents statement

VBA Code:
Private Sub UserForm_Activate()
    Me.TextBox1.SetFocus
    DoEvents
End Sub
I've tried that but it's still the same.
It's not that important but thanks for trying. It's just a little annoying. I think looking on line this has been an issue for a few people.
 
Upvote 0
Does it work if you single step the code using the debugger?
 
Upvote 0
Pressing F5 from UserForm_Activate do you mean? No it's still the same. Calls the userform up but no cursor on TextBox1. I think it may just be one of the sacrifices of having it modal False
Does it work if you single step the code using the debugger?
 
Upvote 0
No, I mean set a breakpoint and single-step code using F8.

Last suggestion: What if you put another control in front, so the textbox is not the first to call the setfocus method?
VBA Code:
Private Sub UserForm_Activate()
    Me.ComboBox1.SetFocus
    Me.TextBox1.SetFocus
    DoEvents
End Sub
 
Upvote 0
Solution
No, I mean set a breakpoint and single-step code using F8.

Last suggestion: What if you put another control in front, so the textbox is not the first to call the setfocus method?
VBA Code:
Private Sub UserForm_Activate()
    Me.ComboBox1.SetFocus
    Me.TextBox1.SetFocus
    DoEvents
End Sub
, Ah, that seems to have fixed it. Thanks very much
 
Upvote 0
No, I mean set a breakpoint and single-step code using F8.

Last suggestion: What if you put another control in front, so the textbox is not the first to call the setfocus method?
VBA Code:
Private Sub UserForm_Activate()
    Me.ComboBox1.SetFocus
    Me.TextBox1.SetFocus
    DoEvents
End Sub
It would appear that the DoEvents statement doesn't actually do anything. But setting Combobox1 first then the textbox seems to fix it. I tried with DoEvents enabled and then disabled to test
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,694
Members
449,092
Latest member
snoom82

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