Skip to main content

UserForm object

Table of contents
  1. Syntax
  2. Remarks

UserForm object is a window or dialog box that makes up part of an application's user interface.

The UserForms collection is a collection whose elements represent each loaded UserForm in an application. The UserForms collection has a Count property, an Item method, and an Add method. Count specifies the number of elements in the collection; Item (the default member) specifies a specific collection member; Add places a new UserForm element in the collection.

Syntax

UserForm UserForms [ .Item ] (index)

The placeholder index represents an integer with a range from 0 to UserForms.Count - 1. Item is the default member of the UserForms collection and need not be specified.

Remarks

Use the UserForms collection to iterate through all loaded user forms in an application. It identifies an intrinsic global variable named UserForms. You can pass UserForms(index) to a function whose argument is specified as a UserForm class.

User forms have properties that determine appearance such as position, size, and color; and aspects of their behavior.

User forms can also respond to events initiated by a user or triggered by the system. For example, you can write code in the Initialize event procedure of the UserForm to initialize module-level variables before the UserForm is displayed.

In addition to properties and events, you can use methods to manipulate user forms by using code. For example, you can use the Move method to change the location and size of a UserForm.

When designing user forms, set the BorderStyle property to define borders, and set the Caption property to put text in the title bar. In code, you can use the Hide and Show methods to make a UserForm invisible or visible at run time.

UserForm is an Object data type. You can declare variables as type UserForm before setting them to an instance of a type of UserForm declared at design time. Similarly, you can pass an argument to a procedure as type UserForm. You can create multiple instances of user forms in code by using the New keyword in DimSet, and Static statements.

Access the collection of controls on a UserForm by using the Controls collection. For example, to hide all the controls on a UserForm, use code similar to the following.

For Each Control in UserForm1.Controls
    Control.Visible = False
Next Control

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>