Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing UserForms Joanna Wyrobek 1. Ms Excel UserForms A UserForm object is a window or dialog box that makes up part of an application's user interface.

Similar presentations


Presentation on theme: "Introducing UserForms Joanna Wyrobek 1. Ms Excel UserForms A UserForm object is a window or dialog box that makes up part of an application's user interface."— Presentation transcript:

1 Introducing UserForms Joanna Wyrobek 1

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

3 How Excel Handles Custom Dialog Boxes Excel makes creating custom dialog boxes for your applications relatively easy. In fact, you can duplicate the look and feel of many of Excel's dialog boxes. A custom dialog box is created on a UserForm, and you access UserForms in Visual Basic Editor (VBE). (ThisWorkbook)->Add->UserForm (not module as with the VBA code) 3

4 How Excel Handles Custom Dialog Boxes Following is the typical sequence that you'll follow when you create a UserForm: 1. Insert a new UserForm into your workbooks VB Project. 2. Add controls to the UserForm 3. Adjust some of the properties of the controls that you added. 4. Write event-handler procedures for the controls. These procedures, which are located in the code window for the UserForm, are executed when various events (such as a button click) occur. 5. Write a procedure that will display the UserForm This procedure will be located in a VBA module (not in the code module for the UserForm). 6. Add a way to make it easy for the user to execute the procedure you created in Step 5. You can add a button to a worksheet, create a shortcut menu command, and so on. 4

5 Inserting a New UserForm To insert a new User Form, activate VBE (press Alt+F11, select your workbook’s project from the Project Window and then choose Insert- >UserForm. UserForms have default names: UserForm1, UserForm2, and so on. You can change the UserForm name to anything you with (do not use spaces and characters reserved by Excel such as !, *, etc.). 5

6 6

7 Adding Controls to a UserForm To add controls to a UserForm, use the Toolbox, as shown in Figure. If the Toolbox isn't displayed, choose View -> ToolBox 7

8 Adding Controls to a UserForm Click the Toolbox button that corresponds to the control that you want to add and then click inside the dialog box to create the control (using its default size). Or you can click the control and then drag in the dialog box to specify the dimensions for the control. When you add a new control, it's assigned a name that combines the control type with the numeric sequence for that type of control. For example, if you add a CommandButton control to an empty UserForm, it's named CommandButton1. If you then add a second CommandButton control, it's named CommandButton2. 8

9 Adding Controls to a UserForm Renaming all the controls that you'll be manipulating with your VBA code is a good idea. Doing so lets you refer to meaningful names (such as ProductListBox) rather than generic names (such as ListBox1). To change the name of a control, use the Properties window in VBE. Just select the object and change the Name property. 9

10 10

11 Toolbox Controls CheckBox A CheckBox control is useful for getting a binary choice: yes or no, true or false, on or off, and so on. When a CheckBox is checked, it has a value of True; when it's not checked, the CheckBox value is False. ComboBox A ComboBox control presents a list of items in a drop-down box and displays only one item at a time. Unlike a ListBox control, you can set up a ComboBox to allow the user to enter a value that doesn't appear in the list of items. CommandButton Every dialog box that you create will probably have at least one CommandButton control. Usually, your UserForms will have one CommandButton labeled OK and another labeled Cancel. 11

12 12

13 Toolbox Controls Frame A Frame control is used to enclose other controls. You enclose controls either for aesthetic purposes or to logically group a set of controls. A Frame is particularly useful when the dialog box contains more than one set of OptionButton controls. Image You can use an Image control to display a graphic image, which can come from a file or can be pasted from the Clipboard. You may want to use an Image control to display your company's logo in a dialog box. The graphics image is stored in the workbook. That way, if you distribute your workbook to someone else, you don’t have to include a copy of the graphics file. 13

14 14

15 Toolbox Controls Label A Label control simply displays text in your dialog box. ListBox The ListBox control presents a list of items, and the user can select an item (or multiple items). ListBox controls are very flexible. For example, you can specify a worksheet range that holds the ListBox items, and this range can consist of multiple columns. Or you can fill the ListBox with items by using VBA. MultiPage A MultiPage control lets you create tabbed dialog boxes, such as the Format Cells dialog box. By default, a MultiPage control has two pages, but you can add any number of additional pages. 15

16 16

17 Toolbox Controls OptionButton OptionButton controls are useful when the user needs to select one item from a small number of choices. OptionButtons are always used in groups of at least two. When one OptionButton is selected, the other OptionButtons in its group are deselected. If your UserForm contains more than one set of OptionButtons, the OptionButtons in each set must share a unique GroupName property value. Otherwise, all OptionButtons become part of the same set. Alternatively, you can enclose the OptionButtons in a Frame control, which automatically groups the OptionButtons contained in the frame. 17

18 18

19 Toolbox Controls RefEdit The RefEdit control is used when you need to let the user select a range in a worksheet. This control accepts a typed range address or a range address generated by pointing to the range in a worksheet. ScrollBar The ScrollBar control is similar to a SpinButton control. The difference is that the user can drag the ScrollBar button to change the control's value in larger increments. The ScrollBar control is most useful for selecting a value that extends across a wide range of possible values. 19

20 20

21 Toolbox Controls SpinButton The SpinButton control lets the user select a value by clicking either of two arrows: one to increase the value and the other to decrease the value. A SpinButton is often used with a TextBox control or Label control, which displays the current value of the SpinButton. A SpinButton can be oriented horizontally or vertically. TabStrip A TabStrip control is s imilar to a MultiPage control, but it's not as easy to use. A TabStrip control, unlike a MultiPage control, doesn't serve as a container for other objects. Generally, you'll find that the MultiPage control is much more versatile. TextBox A TextBox control lets the user type text or a value. 21

22 22

23 Toolbox Controls TextBox A TextBox control lets the user type text or a value. ToggleButton A ToggleButton control has two states: on and off. Clicking the button toggles between these two states, and the button changes its appearance. Its value is either True (pressed) or False (not pressed). I never use this control because I think a CheckBox is much clearer. 23

24 Using controls on a worksheet You can embed many of the UserForm controls directly into a worksheet. You can access these controls by using Excel’s Developer-> Controls-> Insert command. Adding such controls to a worksheet requires much less effort thatn creating a UserForm. In addition, you may not need macros because you can link a control to a worksheet cell. For example, you can insert a CheckBox control on a worksett, and link to to a particular cell by setting its LinkedCell property. Then the CheckBox is checked, the linked cell displays TRUE. Then it is unchecked, the linked cell displays FALSE. 24

25 25

26 Using controls on a worksheet Adding controls to a worksheet can be a bit confusing because controls can come from two sources: Form controls: These controls are insertable objects. ActiveX controls: These controls are a subset of those that are available for use on UserForms. You can use the controls from either of these sources, but it's important that you understand the distinctions between them. The Form controls work much differently than the ActiveX controls. When you add an ActiveX control to a worksheet, Excel goes into design mode. In this mode, you can adjust the properties of any controls on your worksheet, add or edit event-handler procedures for the control, or change its size or position. To display the Properties window for an ActiveX control, use the Developer-> Controls-> Properties commands. 26

27 Using controls on a worksheet For simple buttons and forms created directly in a worksheet use the Button control from the Form controls because you can attach any macro to it. Often you will not need any macro. Controls from the ActiveX controls, clicking it will execute its event-handler procedure (for example, CommandButton1_Click) in the code module for the Sheet object — you can't attach just any macro to it. When Excel is in design mode, you can't try out the controls. To test the controls, you must exit design mode by clicking the Developer ⇒ Controls->Desing mode button (which is a toggle). 27

28 Adjusting UserForm Controls After you place a control in a UserForm, you can move and resize the control by using standard mouse techniques. A UserForm can contain vertical and horizontal gridlines (displayed as dots) that help you align the controls that you add. When you add or move a control, it snaps to the grid to help you line up the controls. If you don't like to see these gridlines, you can turn them off by choosing Tools ⇒ Options-> in VBE. In the Options dialog box, select the General Tab and set your desired option in the Form Grid Settings Option. These gridlines are for design only and do not appear when the dialog box is displayed to the user. The Format menu in the VBE window provides several commands to help you precisely align and space the controls in a dialog box. Before you use these commands, select the controls with which you want to work. These commands work just as you'd expect, so I don't explain them here. 28

29 29

30 Using the Properties window 30 Caption is the text displayed on the control, name is how you refer to the control in the VBA code

31 Common controls properties TextAlign property can have any of the following values: 1 - fmTextAlignLeft, 2 - fmTextAlignCenter, or 3 - fmTextAlignRight. Caption – what is displayed. Name - how you refer to the control in the VBA code. BackColor – color of the controls’ background. Accelerator – Alt+this key selects the control BorderColor – color of the rim 31

32 Using the Properties window Instead of the Properties window, you can change the control’s properties in the code: OptionButton1.Value=TruePrivate Sub CommandButton1_Click() CommandButton1.BackColor = &H80FF& End Sub 32

33 33

34 Accommodating keyboard users Many users prefer to navigate through a dialog box by using the keyboard: The Tab and Shift+Tab keystrokes cycle through the controls, and pressing a hot key (an underlined letter) operates the control. To make sure that your dialog box works properly for keyboard users, you must be mindful of two issues: tab order and accelerator keys. (In the Properties Window change Accelerator to a letter or number, later on, when UserForm is active, press Ctlr+ the chosen character to check the control) 34

35 Accommodating keyboard users Changing the tab order of controls The tab order determines the sequence in which the controls are activated when the user presses Tab or Shift+Tab. It also determines which control has the initial focus. If a user is entering text in a TextBox control, for example, the TextBox has the focus. If the user clicks an OptionButton, the OptionButton has the focus. The control that's first in the tab order has the focus when a dialog box is first displayed. 35

36 36

37 37 The number of TabIndex determines sequence in which controls are activated when the used presses Tab.

38 Accommodating keyboard users To set the tab order of your controls, choose View ⇒ Tab Order or right click the UserForm and choose TabOrder from the shortcut Menu. In either case Excel displays the Tab Order dialog box, which lists all the controls, the sequence of which corresponds to the order in which controls pass the focus between each other in the UserForm. To move a control, select it and click the arrow keys up or down. You can choose more than one control (by Shift- or Ctrl-clicking) and move them all at once. 38

39 Accommodating keyboard users Alternatively, you can set an individual control's position in the tab order by using the Properties window. The first control in the tab order has a TabIndex property of 0. Changing the TabIndex property for a control may also affect the TabIndex property of other controls. These adjustments are made automatically to ensure that no control has a TabIndex greater than the number of controls. If you want to remove a control from the tab order, set its TabStop property to False. 39

40 40

41 41

42 Setting Hot Keys You can assign an accelerator key, or hot key, to most dialog box controls. An accelerator key allows the user to access the control by pressing Alt and the hot key. Use the Accelerator property in the Properties window for this purpose. For example, Alt + x or Alt+1 Some controls don’t have Accelerator property because they don’t display a caption (for example TextBox) 42

43 Displaying a UserForm To display a UserForm from VBA, you create a procedure that uses the Show method of the UserForm object. If your UserForm is named UserForm1, the following procedure displays the dialog box on that form: Sub ShowForm() UserForm1.Show End Sub This procedure must be located in a standard VBA module and not in the code module for the UserForm. 43

44 Displaying a UserForm When the UserForm is displayed, it remains visible onscreen until it's dismissed. Usually, you'll add a CommandButton control to the UserForm that executes a procedure that dismisses the UserForm. The procedure can either unload the UserForm (with the Unload command) or hide the UserForm (with the Hide method of the UserForm object). Unload – remove from the random-access memory, lose selected settings. Hide – keep in RAM, PC selected settings. 44

45 Adjusting the display position The StartUpPosition property of the UserForm object determines where on the screen the dialog box will be displayed. You can specify this property in the Properties box or at runtime. The default value is 1 – CenterOwner, which displays the dialog box in the center of the Excel window. If you use a dual-monitor system, however, you'll find that sometimes the StartUpPosition property seems to be ignored. Specifically, if the Excel window is on the secondary monitor, the UserForm may appear on the left edge of the primary window. The following code ensures that the UserForm is always displayed in the center of the Excel window: With UserForm1.StartUpPosition = 0.Left = Application.Left + (0.5 * Application.Width) - (0.5 *.Width).Top = Application.Top + (0.5 * Application.Height) - (0.5 *.Height).Show End With 45


Download ppt "Introducing UserForms Joanna Wyrobek 1. Ms Excel UserForms A UserForm object is a window or dialog box that makes up part of an application's user interface."

Similar presentations


Ads by Google