MS-Excel / General Formatting

Changing the Form's Design-Time Properties

Forms (and all the control objects you can add to a form) have an extensive list of properties that you can manipulate by entering or selecting values in the Properties window. (Recall that you display the Properties window by activating View, Properties Window or by pressing F4.)

For a form, there are more than three dozen properties arranged into seven categories (in the Properties window, activate the Categorized tab to see the properties arranged by category), as described in the next few sections.

Note: In addition to modifying form properties at design time (that is, before you run the form), you can also modify many of the properties at runtime by including the appropriate statements in your VBA procedures.

The Appearance Category

The properties in the Appearance category control the look of the form:

  • BackColor-Sets the color of the form's background. For this and all the color properties, you select a color by first clicking the drop-down arrow to display a color menu. In this menu, you can either choose a predefined color from the System tab or a builtin color from the Palette tab.
  • BorderColor-Sets the color of the form's border. Note that for this property to have any effect, you have to assign a border to the form by using the BorderStyle property.
  • BorderStyle-Choose fmBorderStylSingle to apply a border around the form. Use fmBorderStyleNone for no border.
  • Caption-Specifies the text that's displayed in the form's title bar.
  • ForeColor-Sets the default color of text used in the form's controls.
  • SpecialEffect-Controls how the form appears relative to the form window (for example, raised or sunken).

Note: If you're running Windows Vista's Aero color scheme, the transparency effects will apply to your VBA forms, as well.

The Behavior Category

The properties in the Behavior category control aspects of how the user interacts with the form:

  • Cycle-Determines what happens when the user presses Tab while the focus is on the last control in the form. If this property is set to fmCycleAllForms and the form has multiple pages, focus is set to the first control on the next page. If this property is set to fmCycleCurrentForm, focus is set to the first control on the current page.
  • Enabled-Set this property to True to enable the form or False to disable it. The latter prevents the user from manipulating the form or any of its controls.

    Why would you want to disable a form? This is handy if you want to display the form for a certain amount of time and then close it automatically.To set this up, first create a procedure that closes the form:
    Public Sub CloseForm()
        Unload UserForm1
    End Sub
    
    Next, in the form's initialize event, add the OnTime method and set it up to run the CloseForm procedure a specified number of seconds or minutes after the form loads. For example, the following code runs CloseForm 30 seconds from when the form loads:
    Private Sub UserForm_Initialize()
        Application.OnTime Now + TimeValue("00:00:30"), "Close Form"
    End Sub
    
  • RightToLeft-When True, this property changes the tab order of the form so that pressing Tab moves the highlight among the controls from right to left (instead of the usual left to right).
  • ShowModal-Set this property to True to display the form as modal, which means the user won't be able to interact with the underlying application until he or she closes the form.

The Font Category

The Font property determines the default font used throughout the form. When you activate this property, click the ellipsis (...) button to display the Font dialog box, from which you can select the font, style, size, and effects.

The Misc Category

As its name implies, the Misc category contains a collection of eight properties that don't fit anywhere else, although almost all these properties are obscure and can be safely ignored. The one exception is the Name property, which you use to give a name to your form. (You'll use this name to refer to the form in your VBA code, so use only alphanumeric characters in the name.)

Tip: Although you might be tempted to stick with the default form name supplied by VBA (such as UserForm1), your code will be easier to read if you give the form a more descriptive name. Indeed, this advice applies not only to forms, but to all controls. Note that it's conventional to precede the form name with the frm prefix (for example,frmBudget).

The Picture Category

In the Picture category, use the Picture property to set a background image for the form. (Again, click the ellipsis button to select a picture file from a dialog box.) The other properties determine how the picture is displayed:

  • PictureAlignment-Specifies where on the form the picture is displayed. You can align it in the center of the form, the top left, top right, bottom left, or bottom right.
  • PictureSizeMode-Specifies how the picture is displayed relative to the form:
    fmPictureSizeModeClip-Crops any part of the picture that's larger than the form.

    fmPictureSizeModeStretch-Stretches or shrinks the picture so that it fits the entire form.

    fmPictureSizeModeZoom-Enlarges or reduces the picture until it hits the vertical or horizontal edge of the form.
  • PictureTiling-For small images, set this property to True to fill the background with multiple copies of the image.

The Position Category

The properties in the Position category specify the dimensions of the form (Height and Width), and the position of the form within the application window. For the latter, you can either use the StartUpPosition property to center the form relative to the application window (CenterOwner) or to the screen (CenterScreen), or you can choose Manual and specify the Left and Top properties. (The latter two properties set the form's position in points from the application window's left and top edges, respectively.)

The Scrolling Category

The properties in the Scrolling category determine whether the form displays scroll bars and, if it does, what format the scroll bars have:

  • KeepScrollBarsVisible-Determines which of the form's scroll bars remain visible even if they aren't needed.
  • ScrollBars-Determines which scrollbars are displayed on the form. That is, VBA only displays a scrollbar if it's necessary.
  • ScrollHeight-Specifies the total height of the form's scrollable region. For example, if the form's Height property is set to 200 and you set the ScrollHeight property to 400, you double the total vertical area available in the form.
  • ScrollLeft-If ScrollWidth is greater than the width of the form, use the ScrollLeft property to set the initial position of the horizontal scroll bar's scroll box. For example, if the ScrollWidth is 200, setting ScrollLeft to 100 starts the horizontal scroll bar at the halfway position.
  • ScrollTop-If ScrollHeight is greater than the height of the form, use the ScrollTop property to set the initial position of the vertical scroll bar's scroll box.
  • ScrollWidth-Specifies the total width of the form's scrollable region.
[Previous] [Contents] [Next]