visual basic for applications in microsoft excel (1)sbbf565/week08/week08.pdf · to move to a...

19
Week 8

Upload: vanthien

Post on 25-Jul-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

In the Toolbox click the Multipage tool,

then click the form. A multipage control

is placed on the form displaying two

tabs.

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.

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

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.

Build the following form

Name the form frmBooking with the

caption Course Booking Form

txtName

txtPhone

cboDept

cboCourse

btnClear

btnCancel

optIntro

optInter

optAdv

btnOK

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

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

Private Sub btnCancel_Click()

Unload Me

End Sub

Private Sub btnClear_Click()

txtName.Value = ""

txtPhone.Value = ""

cboDept.Value = ""

cboCourse.Value = ""

End Sub

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

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

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

Assessment 3 (revision in class)

Modifying toolbars, using buttons etc