17
Sep-05 Slide:1 VBA in Excel VBA in Excel Walter Milner

Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Embed Size (px)

Citation preview

Page 1: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:1VBA in Excel

VBA in Excel

Walter Milner

Page 2: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:2VBA in Excel

Introduction

• VBA = Visual Basic for Applications• Enables end-user programming• In MS Office applications• Formulae and macros OK for simple actions,

but..• Advantages over formulae and macros:

– Multiple nested if is easy– Loops easy– Debugging easy

Page 3: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:3VBA in Excel

Getting started

View..Toolbars..Control

Page 4: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:4VBA in Excel

Add a button

Click the button buttonDrag on sheet to create one

Page 5: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:5VBA in Excel

Format it

Right click on buttonSelect propertiesSet as requiredNote distinction between name and caption

Page 6: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:6VBA in Excel

Program it

Right click buttonSelect View code:

Page 7: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:7VBA in Excel

Test it

On Control Toolbox, click set squareThis toggles run/designClick the button

Page 8: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:8VBA in Excel

Referring to cells

Private Sub CommandButton1_Click()Dim x As IntegerDim y As IntegerDim z As IntegerDim result As Integerx = Cells(1, 2).Valuey = Cells(2, 2).Valuez = Cells(3, 2).Valueresult = x * y + zCells(4, 2).Value = resultEnd Sub

Page 9: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:9VBA in Excel

The VBA Excel Object model

• Hierarchy of classes• Use to refer to things

in an Excel application• Very large number of

classes, properties and methods

• Have to use on-screen help for reference

Page 10: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:10VBA in Excel

The VBA Excel Object model

• Object is a thing• Property is some

property of an object• Method is something

the object can be told to do

• Event is something that can happen to the object

• Collection is a set of related objects

• Enumeration is action returning complete set of things (eg all fonts on system)

Page 11: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:11VBA in Excel

Example object - Application

Page 12: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:12VBA in Excel

Example properties of Application

Program a button which sets a value for this property and see what happens

Try it in a loop for a bizarre effect

Page 13: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:13VBA in Excel

Example method

Program a button which calls the findfile method of the application

Page 14: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:14VBA in Excel

Example collection -

Display the count property of the worksheets collection in a msgbox to show how many sheets there are in the workbook

Page 15: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:15VBA in Excel

Using For Each .. Next in a collection

Private Sub CommandButton4_Click()Dim w As WorksheetFor Each w In Application.Worksheets MsgBox (w.Name)NextEnd Sub

Page 16: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:16VBA in Excel

The RangeSelection property of the ActiveWindow

This is a Range objectSo it has the properties of a Range objectUse them to program a button which displays the totalof the numbers in the cells selected

Page 17: Sep-05 Slide:1 VBA in Excel Walter Milner. Sep-05 Slide:2 VBA in Excel Introduction VBA = Visual Basic for Applications Enables end-user programming In

Sep-05 Slide:17VBA in Excel

2d array exercise

Use the RangeSelection property to program a button which does a vertical flip of selected cells