Download presentation
Presentation is loading. Please wait.
Published byMerilyn Collins Modified over 9 years ago
1
Week 8
2
Recap User Forms
3
Input Validation Message Boxes Input Boxes Conversion Functions
4
Message Boxes and Input Boxes are preset versions of User Forms Known as Forms, User Forms, Dialog Boxes Can be used to: Initialize variables Select ranges Input Data Return messages to the user Allow the user to select options that affect the way a sub will run
5
Look at the Project window for Week 08.xls Note that there is now a Forms list in addition to the Modules list The form has an object view and a code view (use F7/shift F7 to toggle between them. Change form properties in the properties window Add form content using the Toolbox
6
You can Run or Show a form from any sub (macro) using frmXXX.Show If you want the form to show automatically when the workbook opens Then select This Workbook and add the following code: Private Sub Workbook_Open() frmIntro.Show End Sub
7
In the Toolbox click the Multipage tool, then click the form. A multipage control is placed on the form displaying two tabs.
8
One of the first features of an Excel VBA application might be to provide facilities to move to a worksheet. One way to achieve this is to provide Option buttons for a user to choose, and a Command button to put the option into effect. A user can only select one option.
9
Usually as a minimum you will need 2 command buttons, OK and Cancel For a Cancel button, set the Cancel property to True In the form’s code window, add a Private Sub to control each button. Private Sub cmdCancel_Click() Unload Me End Sub Private Sub btnOK_Click() If option1 Then Sheets("Data").Select If option2 Then Sheets("Analysis").Select Unload Me End Sub
10
Events are changes of state For example A mouse single or double click, Open or Close Activate Some objects have a default event – e.g. the default event for a workbook is the Open event, the default event for a command button is the Click event.
11
Build the following form Name the form frmBooking with the caption Course Booking Form
12
txtName txtPhone cboDept cboCourse btnClear btnCancel optIntro optInter optAdv btnOK
13
There are four procedures necessary, An initialize procedure to set the contents of the combo boxes and set the initial values of other options A procedure to exit the form on cancel A procedure to update the worksheet on ok A procedure to clear the selected values on clear
14
Private Sub UserForm_Initialize() txtName.Value = "" txtPhone.Value = "" With cboDept.AddItem "Finance".AddItem "Marketing".AddItem "Computer Services".AddItem "Personnel" End With cboDept.Value = "" With cboCourse.AddItem "Access".AddItem "Excel".AddItem "Word".AddItem "Powerpoint" End With cboCourse.Value = "" optIntro = True txtName.SetFocus End Sub
15
Private Sub btnCancel_Click() Unload Me End Sub Private Sub btnClear_Click() txtName.Value = "" txtPhone.Value = "" cboDept.Value = "" cboCourse.Value = "" End Sub
16
Private Sub btnOK_Click() ActiveWorkbook.Sheets("Course Bookings").Select Range("A1").Select Do While ActiveCell <> "" ActiveCell.Offset(1, 0).Select Loop ActiveCell.Value = txtName.Value ActiveCell.Offset(0, 1) = txtPhone.Value ActiveCell.Offset(0, 2) = cboDept.Value ActiveCell.Offset(0, 3) = cboCourse.Value If optIntro = True Then ActiveCell.Offset(0, 4) = "Intro" ElseIf optInter = True Then ActiveCell.Offset(0, 4) = "Inter" Else ActiveCell.Offset(0, 4) = "Adv" End If Range("A1").Select End Sub
17
You can use one or more of the following options: Keyboard shortcut to a show form sub A button on the worksheet A menu or toolbar option (more on this next week) The Worksheet’s Activate event The Workbook’s Open event
18
You can fill list and combo boxes using data from your spreadsheet You can run macros from the form by using: Call sub name in your btnOK click code You can extend the code inside your procedures to further calculations You can have a sub which calls multiple forms You will probably need to do some data validation when using forms for data input
19
Assessment 3 (revision in class) Modifying toolbars, using buttons etc
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.