Pre select a textbox in a userform

UHsoccer

Well-known Member
Joined
Apr 3, 2002
Messages
1,023
When my userform is displayed, I would like to have the input field preselected. It is the same as using the Shift-Home key.

I current ly use a shortcut ( Ctrl-M ) to select the sheet and display the userform, as in

Code:
    Sheets("Select").Activate
    Call Worksheets("Select").CommandButton1_Click
    
    UserForm1.TextBox1.Select   ' NOT correct

The last line does not work, but it was my stab at the problem

Any ideas appreciated
 

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

Lewiy

Well-known Member
Joined
Jan 5, 2007
Messages
4,284
Firstly, you cannot call a CommandButton_Click event from another sub routine as it is a private sub. I assume that behind the CommandButton you have something along the lines of:
Code:
UserForm1.Show

What I think you are trying to achieve is for the cursor to start in a particular TextBox on the UserForm. In which case you want to go to the UserForm_Activate event and enter the line:
Code:
TextBox1.SetFocus

Hope that helps!
 

UHsoccer

Well-known Member
Joined
Apr 3, 2002
Messages
1,023
The cursor is already in the textbox, albeit at the END.

However, I would like the entire filed preselected (highlighted) so when I start typing, the current information get overtyped
 

Lewiy

Well-known Member
Joined
Jan 5, 2007
Messages
4,284
Do you need the current text to be visible before you start typing? If not then just stick:
Code:
TextBox1.Value = ""
in the UserForm_Activate event.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
UHSoccer

You should probably start by looking into Tab Order.

Then if you want the text in the textbox selected look at SelStart/SelLength.
Code:
Private Sub UserForm_Initialize()
    TextBox1.SelStart = 0
    TextBox1.SelLength = Len(TextBox1.Value)
End Sub
Note this assumes there's actually something in the textbox.:)
 

UHsoccer

Well-known Member
Joined
Apr 3, 2002
Messages
1,023
I would like to retain the original text but have it "selected"

By the way, the addition of UserForm1.TextBox1.Value = "" did NOT clear the information in the textbox
 

Lewiy

Well-known Member
Joined
Jan 5, 2007
Messages
4,284
Are you still trying to call the CommandButton_Click event in a way other than by actually clicking the button?
 

UHsoccer

Well-known Member
Joined
Apr 3, 2002
Messages
1,023
I am using the code
Code:
Sub Display_Select()

    Sheets("Select").Activate
    Call Worksheets("Select").CommandButton1_Click
    UserForm1.TextBox1.Value = " "
    
End Sub
Which is assigned to Ctrl-M to select a sheet and bring up the user form
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
Did you try my code?

Worked for me.:)
 

UHsoccer

Well-known Member
Joined
Apr 3, 2002
Messages
1,023
Norie,

Missed your post.

I added your code to the existing form code with no effect

Then I added the two lines to the "Private Sub TextBox1_Change()" routine. It did highlite, but the tab action would not leave the cell and go to the next one.

I did experiment with other options to no avail

What I did discover was the Shift-Home action highlites the entry in the cell that is active. Do not know how to "program" that step....
 

Forum statistics

Threads
1,181,648
Messages
5,931,213
Members
436,784
Latest member
amuljono

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
Top