Excel VBA Keep Focus on TextBox

hendyhellen

New Member
Joined
Jul 29, 2023
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
  2. MacOS
Hello all,

How to make the cursor always focus on the textbox after user pressing enter on textbox ?

Using this code, the cursor not focus on textbox after input and pressing enter

VBA Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1.Text) <> 0 Then
    Product = UCase(TextBox1.Value)
    Call Print_Label
    Me.TextBox1.Text = vbNullString
    Me.TextBox1.SetFocus
    Cancel = True
End If
End Sub

Thank you all~
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
This is on a userform?
Works for me in Excel 2016 (Office 365). If Len = 0 then of course it won't set focus but I don't expect that is your case. Note that if you're changing textbox via vba code then this event won't run until you move off of the control and even then it may not function as expected. I mention that only because if I alter textbox value via immediate window, the code runs but does not set the cursor in the textbox. That should not be an issue either since you say you're hitting Enter key.

Since you are using BeforeUpdate event you could try
VBA Code:
If Len(TextBox1.Text) <> 0 Then
    Product = UCase(TextBox1.Value)
    Call Print_Label
    Me.TextBox1.Text = vbNullString
    Cancel = True
End If
 
Upvote 0
Try setting your TextBox1 as the ActiveControl (if your form name is not "UserForm1" change according to your naming convention.)
e.g.
VBA Code:
   UserForm1.ActiveControl = TextBox1
   TextBox1.SetFocus

Here is a quick example. I have 3 TextBoxes and 3 corresponding OptionButtons.

The option buttons change which control is Active and has Focus (both are needed in Excel 2016)

1690658620677.png


Test Code:
VBA Code:
Private Sub OptionButton1_Click()
  SetActiveControlAndFocus TextBox1
End Sub
Private Sub OptionButton2_Click()
  SetActiveControlAndFocus TextBox2
End Sub
Private Sub OptionButton3_Click()
  SetActiveControlAndFocus TextBox3
End Sub

Sub SetActiveControlAndFocus(ctl As Control)
  UserForm1.ActiveControl = ctl
  ctl.SetFocus
End Sub
 
Upvote 0
Yes, you are correct on userform.
on userform after type something on textbox ---- ENTER --- then print out the label ---- after print out, i must click again on textbox then start typing again.


This is on a userform?
Works for me in Excel 2016 (Office 365). If Len = 0 then of course it won't set focus but I don't expect that is your case. Note that if you're changing textbox via vba code then this event won't run until you move off of the control and even then it may not function as expected. I mention that only because if I alter textbox value via immediate window, the code runs but does not set the cursor in the textbox. That should not be an issue either since you say you're hitting Enter key.

Since you are using BeforeUpdate event you could try
VBA Code:
If Len(TextBox1.Text) <> 0 Then
    Product = UCase(TextBox1.Value)
    Call Print_Label
    Me.TextBox1.Text = vbNullString
    Cancel = True
End If
 
Upvote 0
This one also not working, sorry since i am very newbie on VBA
Can i share the xlsm file here and then help me which code i make it wrong ?

https://www.mediafire.com/file/4m02cdrtiwafr83/Scanning-LABEL.xlsm/file

Try setting your TextBox1 as the ActiveControl (if your form name is not "UserForm1" change according to your naming convention.)
e.g.
VBA Code:
   UserForm1.ActiveControl = TextBox1
   TextBox1.SetFocus

Here is a quick example. I have 3 TextBoxes and 3 corresponding OptionButtons.

The option buttons change which control is Active and has Focus (both are needed in Excel 2016)

View attachment 96216

Test Code:
VBA Code:
Private Sub OptionButton1_Click()
  SetActiveControlAndFocus TextBox1
End Sub
Private Sub OptionButton2_Click()
  SetActiveControlAndFocus TextBox2
End Sub
Private Sub OptionButton3_Click()
  SetActiveControlAndFocus TextBox3
End Sub

Sub SetActiveControlAndFocus(ctl As Control)
  UserForm1.ActiveControl = ctl
  ctl.SetFocus
End Sub
 
Upvote 0
This one also not working, sorry since i am very newbie on VBA
Can i share the xlsm file here and then help me which code i make it wrong ?

https://www.mediafire.com/file/4m02cdrtiwafr83/Scanning-LABEL.xlsm/file
I can't download the file:
This site can’t be reached
Check if there is a typo in scanning-label.


How to make the cursor always focus on the textbox after user pressing enter on textbox ?
Using this code, the cursor not focus on textbox after input and pressing enter
Where did the focus go after you press Enter?
 
Upvote 0
Sorry looks like i make wrong on the hyperlink

Scanning-LABEL

I would like to make the textbox always focus after pressing enter, so after scanning the barcode i don't need to click the mouse again on the textbox
SCAN -- PRINT --- SCAN -- PRINT

Thank you for the help :)
I can't download the file:
This site can’t be reached
Check if there is a typo in scanning-label.



Where did the focus go after you press Enter?
 
Upvote 0
I can't download the file:
This site can’t be reached
Check if there is a typo in scanning-label.



Where did the focus go after you press Enter?

On my code right now, after pressing Enter
the focus is not on the textbox, so i need to click the textbox again and again
 
Upvote 0
Can't see what your problem is. Before pressing Enter:
1690685941972.png

After pressing Enter:
1690685991799.png

The cursor is in the textbox (hopefully the image shows this)

This is the code (but it doesn't seem to matter if the SetFocus line is commented out or not).
VBA Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1.Text) <> 0 Then
    Product = UCase(TextBox1.Value)
    Call Print_Label
    Me.TextBox1.Text = vbNullString
    'Me.TextBox1.SetFocus
    Cancel = True
End If
End Sub
 
Upvote 0
After input text then enter, start to printout
and then the cursor not focus on the textbox again (here is on my excel; tried in 2021 & 2019 the same)

BEFORE
Screenshot_14.png



AFTER PRINT OUT

Screenshot_13.png


Can't see what your problem is. Before pressing Enter:
View attachment 96223
After pressing Enter:
View attachment 96224
The cursor is in the textbox (hopefully the image shows this)

This is the code (but it doesn't seem to matter if the SetFocus line is commented out or not).
VBA Code:
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1.Text) <> 0 Then
    Product = UCase(TextBox1.Value)
    Call Print_Label
    Me.TextBox1.Text = vbNullString
    'Me.TextBox1.SetFocus
    Cancel = True
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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