Week 8. Recap User Forms Input Validation Message Boxes Input Boxes Conversion Functions.

19
Week 8

Transcript of Week 8. Recap User Forms Input Validation Message Boxes Input Boxes Conversion Functions.

Page 1: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

Week 8

Page 2: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

Recap User Forms

Page 3: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

Input Validation• Message Boxes• Input Boxes

Conversion Functions

Page 4: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

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

Page 5: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

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

Page 6: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

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.ShowEnd Sub

Page 7: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

In the Toolbox click the Multipage tool, then click the form. A multipage control is placed on the form displaying two tabs.

Page 8: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

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.

Page 9: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

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 MeEnd Sub

Private Sub btnOK_Click()If option1 Then Sheets("Data").SelectIf option2 Then Sheets("Analysis").SelectUnload MeEnd Sub

Page 10: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

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.

Page 11: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

Build the following form Name the form frmBooking with the

caption Course Booking Form

Page 12: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

txtName

txtPhone

cboDept

cboCourse

btnClear

btnCancel

optIntro

optInter

optAdv

btnOK

Page 13: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

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

Page 14: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

Private Sub UserForm_Initialize()txtName.Value = ""txtPhone.Value = ""With cboDept .AddItem "Finance" .AddItem "Marketing" .AddItem "Computer Services" .AddItem "Personnel"End WithcboDept.Value = ""With cboCourse .AddItem "Access" .AddItem "Excel" .AddItem "Word" .AddItem "Powerpoint"End WithcboCourse.Value = ""optIntro = TruetxtName.SetFocusEnd Sub

Page 15: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

Private Sub btnCancel_Click()Unload MeEnd Sub

Private Sub btnClear_Click()txtName.Value = "" txtPhone.Value = ""cboDept.Value = ""cboCourse.Value = ""End Sub

Page 16: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

Private Sub btnOK_Click() ActiveWorkbook.Sheets("Course Bookings").SelectRange("A1").SelectDo While ActiveCell <> "" ActiveCell.Offset(1, 0).SelectLoopActiveCell.Value = txtName.ValueActiveCell.Offset(0, 1) = txtPhone.ValueActiveCell.Offset(0, 2) = cboDept.ValueActiveCell.Offset(0, 3) = cboCourse.ValueIf optIntro = True Then ActiveCell.Offset(0, 4) = "Intro"ElseIf optInter = True Then ActiveCell.Offset(0, 4) = "Inter"Else ActiveCell.Offset(0, 4) = "Adv"End IfRange("A1").SelectEnd Sub

Page 17: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

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

Page 18: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

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

Page 19: Week 8.  Recap  User Forms  Input Validation Message Boxes Input Boxes  Conversion Functions.

Assessment 3 (revision in class) Modifying toolbars, using buttons etc