Professional Documents
Culture Documents
VBA & Excel
VBA & Excel
Barry L. Nelson
IEMS 465
Fall Quarter 2003
VBA
Is a significant subset of the stand-alone
Visual Basic programming language
It is integrated into Microsoft Office
applications (and others, like Arena)
It is the macro language of Excel
You can add
Forms for dialog boxes with user
Classes for object definitions
Modules containing procedures
Visual Basic
Editor
VBA Design
Mode
Design mode is the time during which no code from the project is running and events from Excel or project will not
execute. You can leave design mode by executing a macro or using the Immediate window.
Edit
Window
Options
VB Edit Window
Project Explorer
Code Window
Property Inspector
Variables
Declare by Dim
Better to use Data Types:
Dim amount As Double
Dim year As Integer
Dim name As String
Other data types: Boolean, Byte,
Currency, Date
Default (no type) is Variant
Variable(contd.)
% - integer
& - long integer
! - single # - double
@ currency
$ - string
anIntegerValue% =3, aString$ = "hallo"
Constants
[Public|Private] Const constantName [As
type] = expression
Public Const PI = 3.1, NumPLANETS = 9
Const PI2 = PI * 2
Const RELEASE = #1/1/99/#
Control Structures
Decision
If anyDate < Now Then anyDate =
Now
If anyDate < Now Then
anyDate = Now
End If
If Then Else
Decisions(contd.)
If Index = 0 Then
CopyActiveControl
ClearActiveControl
Else If Index = 1 Then
CopyActiveControl
Else If Index = 2 Then
ClearActiveControl
Else
PasteActive Control
End If
Decisions(contd.)
Select Case Index
Case 0
CopyActiveControl
ClearActiveControl
Case 1
CopyActiveControl
Case 2
ClearActiveControl
Case 3
PasteActive Control
Case Else
frmFind.Show
End Select
Loops/Iterations
Do Loop
Do While condition
statements
Loop
Do
statements
Loop While condition
Loops(contd.)
For Next
For counter = start To end [Step increment]
statements
Next counter
For Each Next
For Each element In group
statements
Next element
Exiting(contd.)
Can also exit from a procedure:
Exit Sub
Code Modules
Excel Objects (ThisWorkbook, Sheet#)
Modules
Typically we put our code here
A Module is a collection of Subs and
Functions
Insert Module
Class Modules
User Forms
Procedures
Sub (routines)
no value returned
Called without parenthesis
mySub param1, param2
Called with parenthesis
Call mySub(param1, param2)
Functions
value returned
assign return value to function name
stuff(car)
Pass by Value (must be declared)
sub stuff(ByVal item As String)
Note that arguments must be formally declared (as
must the return type of functions)
Finding/Creating a Sheet
Dim found As Boolean
Dim sheetNext As Worksheet
' Set up mySheet sheet for output
found = False
For Each sheetNext In Worksheets
If sheetNext.Name = mySheet" Then
found = True
Exit For
End If
Next sheetNext
If found = True Then
Worksheets(mySheet").Select
ActiveSheet.UsedRange.Clear
Else
Worksheets.Add
ActiveSheet.Name = mySheet"
End If
Writing to a Sheet
Put the absolute value of the variable
Fudge in row 2, column 20 of the Sheet
named mySheet.
Worksheets(mySheet).Cells(2,20) = VBA.Abs(Fudge)
Finishing Up
Explore what is available in the IEMS 465
code
Exercise: Write a Sub that inserts a
worksheet named Count into the
Workbook, then writes the numbers 1,2,
,10 in the first row, the first ten columns.
Use a loop to do this.