Userforms are screens that users enter data into to interact with a program written in Visual Basic for Applications (VBA). Programmers extending the default set of tools for Microsoft Word, Excel and Access create these custom forms and populate them with the buttons, sliders, and text boxes appropriate to the input their VBA programs need to run. Editing a VBA userform involves using the form design window of the VBA integrated development environment (IDE). This environment is embedded in each application of the Office suite. Editing userforms makes it possible for users to interact with your VBA programs more quickly and efficiently.
- Skill level:
Other People Are Reading
Things you need
- Microsoft Word
Open Microsoft Word, then press "Alt" and "F11" to enter the VBA IDE.
Click "Insert," then "UserForm" to create a new userform for your VBA program. You'll use the form to provide user data for a short program that populates a Word table.
Double-click the "Userform" item in the "Project" pane at the left of the screen. This will expose the tool box that contains controls for you to place on the form.
Drag a command button control from the tool box onto the form. Position the box near the middle of the form's bottom portion.
Drag a text box from the tool box onto the form, above and to the right of the command button. Drag a second text box from the tool box to any userform location that's directly left of the first text box.
Double-click the command button to enter the portion of the IDE for program code entry.
Paste the following program code into the code window:
If (ActiveDocument.Tables.Count = 0) Then
ActiveDocument.Tables.Add Selection.Range, 1, 2
ActiveDocument.Tables(1).Rows(1).Cells(1).Range.Text = TextBox1.Text
ActiveDocument.Tables(1).Rows(1).Cells(2).Range.Text = TextBox2.Text
This program copies the data the user enters in the text boxes into a new table in the current Word document. But the order in which the userform expects input differs from the order the user expects. You'll edit the form to fix that problem.
Press "F5" to run the program. The userform does not place the text insertion caret in the first text box as it should.
Click in the left most text box, then type any text. Press "Tab" to move to the next field in the form. The program highlights the command button instead of the expected second text box.
Click in the second text box and enter any text, then click the command button. The program creates a new table in the document and populates it with the text from the text boxes.
Click the "X" in the userform's upper, right corner to end the program.
Click the userform's leftmost text box, then click in the "Tabindex" field of the "Properties" pane. The "Tabindex" field currently reads "2," indicating that it is the last of the three controls on the form to receive the user input.
Type a "0" over the 2, then click the rightmost text box. Type "1" in the "Tabindex" field, then click the command button. Type a "2" in the "Tabindex" field.
Run the program again, using Step 8's instructions. The program now highlights the text boxes in the correct order as you press the "Tab" key.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for