How to focus on a specific control (TextBox1) on a user-form in MS-Excel

Excel 2007 Excel 2003 Macros Excel 2007 Macros Excel 2003 Excel 2010

Home


Barbara from Mumbai sent us this email:
"Hello,
I just created a user-form to calculate the perks and total salary package based on the entry of a basic salary. The form works fine but when I run the form the command box to calculate the perks and package is highlighted and I have to click in the textbox against the label name to start my entries. How can I avoid this situation so thet the cursor appears inside the textbox against the label name automatically?
Barbara"

This problem has two solutions and you can use any one you like. You can watch the training video below (15.2 MB) before diving into the macro code.

The macro code for the various controls is given below:

Private Sub CommandButton1_Click()
TextBox3 = TextBox2.Value * (60 / 100) + TextBox2.Value * (15 / 100)
TextBox4 = Val(TextBox3) + Val(TextBox2)
End Sub

Private Sub CommandButton2_Click()
TextBox1 = ""
TextBox2 = ""
TextBox3 = ""
TextBox4 = ""
End Sub

Private Sub CommandButton3_Click()
End 'You could also use Unload Me
End Sub



Private Sub UserForm_Activate()
TextBox1.SetFocus
End Sub


Home
Excel 2007 Excel 2003 Macros Excel 2007 Macros Excel 2003 Excel 2010