Excel 2019 Bible

(singke) #1

Chapter 44: Creating UserForms


44


Looking at a UserForm Example


The preceding section is, admittedly, rudimentary. This section demonstrates, in detail,
how to develop a UserForm. This example is rather simple. The UserForm displays a message
to the user—something that can be accomplished more easily by using the MsgBox func-
tion. However, a UserForm gives you more flexibility in terms of formatting and layout of
the message.

This workbook is available on this book’s website at http://www.wiley.com/go/excel2019bible. The
file is named show message.xlsm..

Creating the UserForm
If you’re following along on your computer, start with a new workbook. Then follow these
steps:


  1. Choose Developer ➪ Code ➪ Visual Basic (or press Alt+F11). The VBE appears.

  2. Click your workbook’s name in the Project Explorer to activate it.

  3. Choose Insert ➪ UserForm. The VBE adds an empty form named UserForm1 and
    displays the Toolbox.

  4. Press F4 to display the Properties window, and then change the following prop-
    erties of the UserForm object:


Property Change to
Name AboutBox
Caption About This Workbook


  1. Use the Toolbox to add a Label object to the UserForm. If the Toolbox is not
    visible, choose View ➪ Toolbox.

  2. Select the Label object, and in the Properties window change the Name prop-
    erty to lblMessage and enter any text you want in the Caption property.

  3. In the Properties window, click the Font property and adjust the font. You
    can change the typeface, size, and so on. The changes then appear in the form.
    Figure 44.8 shows an example of a formatted Label control. In this example, the
    TextAlign property was set to the code that center aligns the text.


2 - fmTextAlignCenter
Free download pdf