unit 7: built-in functions
TRANSCRIPT
Excel Macros Level 1
Built-in Functions and Statements
M. Campbell - 2010 2
Built-in Functions
VBA has large number built-in functions and statements
Some are shown in Table 7-1 (p. 78) and Table 7-2 (p. 80)
4/29/2010
p. 78
M. Campbell - 2010 3
Procedure Template
Sub ChangeFormat(FontName [, FontSize])
Parameters listed in italics Optional parameters listed in [square brackets] Type declarations will be listed separately
4/29/2010
p. 78
M. Campbell - 2010 4
MsgBox
MsgBox(prompt [, buttons] [, title])
prompt (String) Message to display in body
buttons (Long) Sum of values to specify properties of message box See Table 7-2 for enum of argument values
title (String) Message to display in title bar
4/29/2010
p. 78
M. Campbell - 2010 5
MsgBox
MsgBox function returns number indicating which button pressed
Defined as VbMsgBoxResult enumeratorSee Table 7-3 for return values
4/29/2010
p. 78
M. Campbell - 2010 6
Activities
In the Unit 7 Activities complete: Activity 1: MsgBox Procedure
4/29/2010
M. Campbell - 2010 7
InputBox
InputBox(prompt [, title] [, default])
prompt (String) Message to display
title (String) Message to display in title bar
default (String) Default value for the text box
4/29/2010
p. 81
M. Campbell - 2010 8
InputBox
Returns the string the user enters in text boxtitle (String)
Message to display in title bardefault (String)
Default value for the text box
4/29/2010
p. 81
M. Campbell - 2010 9
Activities
In the Unit 7 Activities complete: Activity 2: InputBox Subroutine
4/29/2010
M. Campbell - 2010 10
VBA String Functions
Please review pages 82-84 especially: Len UCase and LCase Left, Right, and Mid Str and Val Trim, LTrim, and RTrim
4/29/2010
p. 82
M. Campbell - 2010 11
Activities
In the Unit 7 Activities complete: Activity 3: Phone Number Processing
4/29/2010
M. Campbell - 2010 12
The Is Functions
IsDate Returns true if variable is a date
IsEmpty Returns true if variable is empty
IsNull Returns true if variable is Null
IsNumeric Returns true if variable is a numeric type
4/29/2010
p. 85
M. Campbell - 2010 13
Immediate If Function
IIf(Expression, TruePart, FalsePart)
If Expression is True returns TruePartIf Expression is False returns FalsePart
Always evaluates both parts Can lead to errors if one part should not be
evaluated
4/29/2010
p. 85
M. Campbell - 2010 14
Switch Function
Switch(expr1, value1, expr2, value2, _ … , exprn, valuen)
When it finds an expression that matches it returns the corresponding value
If no match is made returns Null
4/29/2010
p. 86
M. Campbell - 2010 15
Dim number As VariantDim check As Integer
number = Switch(check = 1, "one", _ check = 2, "two", _ check = 3, "three")
If Not IsNull(number) ThenDebug.Print(number)
ElseDebug.Print("Unrecognized number")
End If
4/29/2010
M. Campbell - 2010 16
Units Conversion
InchesToPoints(inchValue)PointsToInches(pointValue)
Many Excel functions take values in pointsMay prefer to work in inches
72 points in an inch
4/29/2010
p. 87
M. Campbell - 2010 17
Handling Errors
VBA has methods to handle run-time errorsError handling is vital in applications meant for
public release
4/29/2010
p. 88
M. Campbell - 2010 18
On Error Goto
On Error Goto Label
If a run-time error occurs VBA will goto the code following the label
4/29/2010
p. 88
M. Campbell - 2010 19
On Error Resume Next
On Error Resume Next
VBA will continue executing code immediately following line that caused error
4/29/2010
p. 90
M. Campbell - 2010 20
Resume Statement
Resume Resume with line that caused error Good if your code corrected the error
Resume Next Resume with the line after the one that caused
errorResume label
Resume with code at line labelled label
4/29/2010
p. 91