I have annoying problem on my created userform with lengthy codes. It doesn't want to show the blinking cursor inside the textbox although the textbox is active.
So, i close the Excel App.
Open Excel App, which by default it has a blank workbook Book1
Create a sub in regular module :
Sub ShowFrmTest()
frmTest.Show vbModeless
End Sub
Then create a userform "frmTest", with one textbox "tb" and one button "btn".
Inside the userform module, i have like below :
Private Sub UserForm_Initialize()
btn_Click
End Sub
Private Sub btn_Click()
'style-X
tb.SetFocus
Debug.Print ActiveControl.Name
End Sub
then I run the ShowFrmTest sub.
result-A
The debug.print result show "tb".
So, tb is active, but I don't understand why it doesn't show the blinking cursor inside the tb.
So, I try like this for the btn_Click sub:
Private Sub UserForm_Initialize()
btn_Click
End Sub
Private Sub btn_Click()
'style-Y
btn.SetFocus
Debug.Print ActiveControl.Name
End Sub
then I run the ShowFrmTest sub.
result-B
the debug.print result show "btn",
and the btn is active because i see the button has a shadow.
Next, i try like below for the btn_Click sub:
Private Sub UserForm_Initialize()
btn_Click
End Sub
Private Sub btn_Click()
'style-Z
btn.SetFocus
tb.SetFocus
Debug.Print ActiveControl.Name
End Sub
then I run the ShowFrmTest sub, same result like in result-A.
Next i try style X, Y and Z inside the uf_init sub and I delete the btn_Click sub.
Then I run the ShowFrmTest sub.
The same result happen just like when X, Y and Z inside the btn_Click.
Next, i make like below:
Private Sub UserForm_Initialize()
btn_Click
End Sub
Private Sub UserForm_Activate()
tb.SetFocus
Debug.Print ActiveControl.Name
End Sub
Private Sub btn_Click()
tb.SetFocus
End Sub
Then I run the ShowFrmTest sub.
The same result like result-A.
BUT.... if the UserForm_Activate like below:
Private Sub UserForm_Activate()
'style-Final
btn.SetFocus
tb.SetFocus
Debug.Print ActiveControl.Name
End Sub
After I run the ShowFrmTest sub,
finally I can get the desired result.
The debug.print result show "tb",
and the tb is in focus because I can see the blinking cursor inside the tb.
My Excel version is 2010.
My question :
- A. Is my Excel app corrupted ? that's why it doesn't show the blinking cursor inside the tb if the codes is written in the style X or Z ?
- B. If my Excel app is not corrupted, why Excel behaves like that ? the tb is active but the blinking cursor is not there in the tb if the codes written in the style X or Z (either in tb_click sub or in uf_init only without tb_click sub). Is there any logic behind it ?
- C. Is there maybe a tb property setting which I missed ? That's why it behaves like that.
Also, in the style-Final ... why it need to btn.setfocus first then tb.setfocus in order to see the blinking cursor in the tb ?
Any kind of response would be greatly appreciated.
Thanks in advanced.
Just now I've tried like below :
Sub ShowFrmTest()
'sub in regular module which i run
frmTest.Show vbModeless
Debug.Print frmTest.ActiveControl.Name
frmTest.tb.SetFocus
End Sub
Private Sub UserForm_Initialize()
'sub in uf module
btn_Click
End Sub
Private Sub btn_Click()
'sub in uf module
tb.SetFocus
End Sub
Because I thought "oh... maybe the tb.setfocus in btn_click can't be applied before the userform is shown on the page", So I write the code like above. I still don't see the blinking cursor inside tb although the debug.print result say that "tb" is the active control name.
try adjusting your userform intialize to this: