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.

Slides:



Advertisements
Similar presentations
Excel Navigation. Instructions Use this PowerPoint presentation as you answer the Excel Navigation worksheet questions. Have Excel open also and use ALT.
Advertisements

WORKING SMART Crystal M. Thomas Henrico County DSS (804) POSSESS Central Region Member October 31, 2007.
Using Macros and Visual Basic for Applications (VBA) with Excel
Developing an Excel Application
Tutorial 8: Developing an Excel Application
Understanding Microsoft Excel
EXCEL Spreadsheet Basics
XP New Perspectives on Microsoft Access 2002 Tutorial 61 Microsoft Access 2002 Tutorial 6 – Creating Custom Reports.
Advanced Microsoft Word Hosted by Jared Hoffman Topics Keyboard Shortcuts Customizing Toolbars and Menus Auto Format & Auto Correct Tabs Inserting Pictures.
© by Pearson Education, Inc. All Rights Reserved. continued …
Excel and VBA Creating an Excel Application
Automating Tasks With Macros
Using Charts in a Presentation Lesson 6. Software Orientation Charts can help your audience understand relationships among numerical values. The figure.
Chapter 6 Navigating Presentations Using Hyperlinks and Action Buttons
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need.
© 2002 ComputerPREP, Inc. All rights reserved. Word 2000: Forms, Merges, and Macros.
Microsoft Visual Basic 2012 CHAPTER TWO Program and Graphical User Interface Design.
Laboratory Exercise # 13 – Font and Number Format Styles Office Productivity Tools 1 Laboratory Exercise # 13 Font and Number Format Styles Objectives:
Lesson 1 – Microsoft Excel The goal of this lesson is for students to successfully explore and describe the Excel window and to create a new worksheet.
Tutorial 6 Using Form Tools and Creating Custom Forms
BIM313 – Advanced Programming Simple Controls 1. Contents Traditional Controls – Labels, Text Boxes, Buttons, Check Boxes, List Boxes, Combo Boxes Advanced.
XP Week 6 – ABC By Aurino Djamaris Bakrie School of Management.
Lab 9 – User Forms Design. User Forms What are user forms? –Known as dialog boxes –Major ways for getting user input An example of using user forms: Monthly.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 22 Macros.
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
Microsoft PowerPoint Getting Started Guide Prepared for Towson University Dr. Jeff M. Kenton Amy Chase Martin 2007.
IE 411/511: Visual Programming for Industrial Applications
 Starting Excel 2003  Using Help  Workbook Management  Cursor Management  Manipulating Data  Using Formulae and Functions  Formatting Spreadsheet.
Chapter 2 More Controls Programming in C#. NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Creating your own form from scratch.. To create a custom form, you can modify an existing form or design and create a form from scratch. In either case,
Microsoft Office Excel 2003 Tutorial 3 – Developing a Professional-Looking Worksheet.
CTS130 Spreadsheet Lesson 4 Working with Cells, Columns, Rows, and Sheets.
Spreadsheets in Finance and Forecasting Presentation 9 Macros.
Microsoft Access Lesson 1 Lexington Technology Center February 11, 2003 Bob Herring On the Web at
Chapter 3: Using GUI Objects and the Visual Studio IDE.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 2 Creating a User Interface.
1 The EDIT Program The Edit program is a full screen text editor that allows you to: Create text files Create text files Edit an existing text files Edit.
1. Chapter 4 Customizing Paragraphs 3 More Paragraph Changes Highlight a paragraph in Word by applying borders and shading. Sort paragraphs to control.
Key Applications Module Lesson 21 — Access Essentials
COMPREHENSIVE Access Tutorial 6 Using Form Tools and Creating Custom Forms.
IC 3 BASICS, Internet and Computing Core Certification Key Applications Lesson 11 Organizing the Worksheet.
LINKS TO OBJECTIVES Multiple Worksheets Multiple Worksheets Move, Copy and Paste Cells Move, Copy and Paste Cells Split into Windows Freeze Panes Range.
Basic Editing Lesson 2.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 15 Advanced Tables.
Microsoft Access 2010 Chapter 8 Advanced Form Techniques.
Formatting WorksheetsFormatting Worksheets Lesson 7.
More Controls and Their Properties Part 4 dbg --- TextBox, CheckBox, RadioButton, GroupBox, ToolTips,
Key Applications Module Lesson 17 — Organizing Worksheets Computer Literacy BASICS.
Lesson 1 – Microsoft Excel * The goal of this lesson is for students to successfully explore and describe the Excel window and to create a new worksheet.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 2 More Controls.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Microsoft Visual Basic 2010 CHAPTER TWO Program and Graphical User Interface Design.
Lesson 6 Formatting Cells and Ranges. Objectives:  Insert and delete cells  Manually format cell contents  Copy cell formatting with the Format Painter.
The Excel model for information processing The Excel model is a grid of cells in which items of information are stored and processed. Any information that.
Key Applications Module Lesson 14 — Working with Tables Computer Literacy BASICS.
Chapter 2 More Controls Programming in C#. NET Objectives Use text boxes, group boxes, check boxes, radio buttons, and picture boxes effectively.
Microsoft Office 2013 Try It! Chapter 4 Storing Data in Access.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
Laboratory Exercise # 10 – Microsoft Word Additional Topics Office Productivity Tools 1 Laboratory Exercise # 10 Microsoft Word Additional Topics Objectives:
Chapter 11 Collaboration Features for Workbooks Microsoft Excel 2013.
 2002 Prentice Hall. All rights reserved. 1 Introduction to the Visual Studio.NET IDE Outline Introduction Visual Studio.NET Integrated Development Environment.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Adding Tables to Slides Lesson 5. Software Orientation Tables are designed to organize data in columns and rows, as shown at right. The Table Tools Design.
Working with UserForms Dr Joanna Wyrobek 1. Displaying a modeless UserForm By default, UserForms are displayed modally. This means that the UserForm must.
 You won’t write a single line of program code.  Instead, you’ll use visual programming techniques.  Visual Studio processes your actions (such as mouse.
User Forms.
Chapter 7 Advanced Form Techniques
Presentation transcript:

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. 2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

41

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

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

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

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