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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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!
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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.:)
 
Upvote 0
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
 
Upvote 0
Are you still trying to call the CommandButton_Click event in a way other than by actually clicking the button?
 
Upvote 0
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
 
Upvote 0
Did you try my code?

Worked for me.:)
 
Upvote 0
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....
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,209
Members
448,874
Latest member
b1step2far

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