Professional Documents
Culture Documents
kkl
Code Window
Property Inspector
-------------------------------------------
Do
statements…
Loop {While|Until} …condition
VBA & Excel.ppt 14
Loops(cont’d.)
For counter = start To end [Step increment]
…statements
Next counter
--------------------------------
Do
[statement block]
Exit Do
[statement block]
Loop Until Check = False
VBA & Excel.ppt 16
Code Modules
• Excel Objects (ThisWorkbook, Sheet#)
• Modules
– Typically we put our code here
– A Module is a collection of Subs and
Functions
– Insert Module
• More:
– Class Modules (see “master” used for simlib)
– User Forms
VBA & Excel.ppt 17
Procedures
• Sub (routines)
– no value returned
– Called when needed
Call mySub(param1, param2)
• Function
– value returned
– assign return value to function name
X = myFunction(2, 7, Z)
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
Worksheets(“mySheet”).Cells(2,20) = VBA.Abs(Fudge)
Worksheets(“mySheet”).Cells(I,J) = VBA.Abs(Fudge)
Worksheets(“mySheet”).Range(“T2”)=VBA.Abs(Fudge)
Range is general – see next page This is how you address
VBA intrinsic functions
VBA & Excel.ppt 24
Ways to use .Range Method
Range("A1") Cell A1
Range("A1:B5") Cells A1 through B5
Range("C5:D9,G9:H16") A multiple-area selection
Range("A:A") Column A
Range("1:1") Row 1
Range("A:C") Columns A through C
Range("1:5") Rows 1 through 5
X = Worksheets(“mySheet”).Range(“T2”).Value
note: t = column 20
Excel likes the (column, row) order rather
than (row, column)
W = WorksheetFunction.Max(0, W + S - a)