How to Edit UserForm in Visual Basic

Written by darrin koltow
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Edit UserForm in Visual Basic
Userforms are screens that users enter data into to interact with a program written in Visual Basic for Applications (VBA). (binary world image by Attila Toro from Fotolia.com)

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:
Moderate

Other People Are Reading

Things you need

  • Microsoft Word

Show MoreHide

Instructions

  1. 1

    Open Microsoft Word, then press "Alt" and "F11" to enter the VBA IDE.

  2. 2

    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.

  3. 3

    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.

  4. 4

    Drag a command button control from the tool box onto the form. Position the box near the middle of the form's bottom portion.

  5. 5

    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.

  6. 6

    Double-click the command button to enter the portion of the IDE for program code entry.

  7. 7

    Paste the following program code into the code window:

    If (ActiveDocument.Tables.Count = 0) Then

    ActiveDocument.Tables.Add Selection.Range, 1, 2

    End If

    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.

  8. 8

    Press "F5" to run the program. The userform does not place the text insertion caret in the first text box as it should.

  9. 9

    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.

  10. 10

    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.

  11. 11

    Click the "X" in the userform's upper, right corner to end the program.

  12. 12

    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.

  13. 13

    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.

  14. 14

    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.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.