Upload
matthew-campbell
View
767
Download
6
Embed Size (px)
DESCRIPTION
Citation preview
Excel Macros Level 1
Functions and Subroutines
M. Campbell - 2010 2
Functions & Subroutines
Functions: Return a value
Subroutines Do not return a value
Both are used to: Make code modular (i.e. reusable) Make code more readable
4/29/2010
p. 69
M. Campbell - 2010 3
Activities
In the Unit 6 Activities complete: Activity 1: Uppercase Subroutine Activity 2: Lowercase Subroutine Activity 3: Title Case Subroutine Activity 4: Uppercase Function
Note that these can all go in the same module
4/29/2010
M. Campbell - 2010 4
Functions
In Activity 4 you saw the function declaration:
Public Function Uppercase2(Text As String) _As String
4/29/2010
M. Campbell - 2010 5
Functions
In Activity 4 you saw the function declaration:
Public Function Uppercase2(Text As String) _As String
4/29/2010
Indicates the Function's scope:Public:
Can be called from any modulePrivate:
Can only be called from module it was declared in
p. 76
M. Campbell - 2010 6
Functions
In Activity 4 you saw the function declaration:
Public Function Uppercase2(Text As String) _As String
4/29/2010
Declares this code to be a Function
p. 69
M. Campbell - 2010 7
Functions
In Activity 4 you saw the function declaration:
Public Function Uppercase2(Text As String) _As String
4/29/2010
The Function's nameThis is something that you define
p. 69
M. Campbell - 2010 8
Functions
In Activity 4 you saw the function declaration:
Public Function Uppercase2(Text As String) _As String
4/29/2010
This is the Function's Parameter listValue is a ParameterWhen you ran the Function, the value of Text was set
to helloThe value placed in a Parameter is known as an
Argument
p. 71
M. Campbell - 2010 9
Functions
In Activity 4 you saw the function declaration:
Public Function Uppercase2(Text As String) _ As String
4/29/2010
This is the Function's Return TypeIn this Function, it will return a value of type String
p. 69
M. Campbell - 2010 10
Functions
In Activity 4 you saw this line of code:
Uppercase2 = UCase(Text)
4/29/2010
M. Campbell - 2010 11
Functions
In Activity 4 you saw this line of code:
Uppercase2 = UCase(Text)
4/29/2010
This is the Function's Return statementThe value of the expression on the right side of the
assignment operator will be returned by the Function
M. Campbell - 2010 12
Functions
In Activity 4 you saw this line of code:
Uppercase2 = UCase(Text)
4/29/2010
Remember that Text is the parameter to the Function.
It currently holds the value of helloThis sends the value to the UCase Function which
converts hello to uppercase
M. Campbell - 2010 13
Subroutines
In Activity 1 you saw the subroutine declaration:
Sub Uppercase()
4/29/2010
M. Campbell - 2010 14
Subroutines
In Activity 1 you saw the subroutine declaration:
Sub Uppercase()
4/29/2010
Declares this code to be a Subroutine
p. 70
M. Campbell - 2010 15
Subroutines
In Activity 1 you saw the subroutine declaration:
Sub Uppercase()
4/29/2010
The Subroutine's nameThis is something that you define
p. 70
M. Campbell - 2010 16
Subroutines
In Activity 1 you saw the subroutine declaration:
Sub Uppercase()
4/29/2010
Note the absence of the scope keyword Public or Private
If it is omitted, the procedure (subroutine or function) is declared to be Public
It should always be included for readability
p. 76
M. Campbell - 2010 17
Calling Subroutines
Has the form:[Public or Private] Sub SubroutineName (_Param1 As DataType1, Param2 As DataType2,_ …)
To call:Call SubroutineName(parameters, …)
OrSubroutineName parameters, …4/29/2010
p. 70
M. Campbell - 2010 18
Activities
In the Unit 6 Activities complete: Activity 5: Calling a Function with a Subroutine
4/29/2010
M. Campbell - 2010 19
Optional Arguments
Can set some arguments to be optional
Sub DisplayName(firstName As String, _Optional lastName As
String, _Optional midName As String)
Note that all optional arguments must come at end of parameter list
4/29/2010
p. 71
M. Campbell - 2010 20
Optional Arguments
Sub DisplayName(firstName As String, _Optional lastName As String, _Optional midName As String)
To call procedure with only firstName and midName:Call DisplayName("Winnie", , "the")
Must include blank spot as arguments expected in order defined in parameter list
4/29/2010
p. 71
M. Campbell - 2010 21
Positional Arguments
In previous example, we used positional arguments:
Call DisplayName("Winnie", , "the") The position of arguments tells VBA which parameters
they fill
4/29/2010
p. 73
M. Campbell - 2010 22
Named Arguments
Naming the arguments: Improves readability Removes the need for blank spaces Allows arguments to go in any order
Call DisplayName(midName:= "the", _ firstName:= Winnie")
4/29/2010
p. 73
M. Campbell - 2010 23
Activities
In the Unit 6 Activities complete: Activity 6: Optional and Named Arguments
4/29/2010
M. Campbell - 2010 24
ByRef
Sub ProcedureA()x = 5MsgBox xCall AddOne(x)MsgBox x
End Sub
Sub AddOne(ByRef i As Integer)i = i + 1
End Sub
p. 73
M. Campbell - 2010 25
ByRef
Sub ProcedureA()x = 5MsgBox xCall AddOne(x)MsgBox x
End Sub
Sub AddOne(ByRef i As Integer)i = i + 1
End Sub
p. 73
Initializes x to 5
M. Campbell - 2010 26
ByRef
Sub ProcedureA()x = 5MsgBox xCall AddOne(x)MsgBox x
End Sub
Sub AddOne(ByRef i As Integer)i = i + 1
End Sub
p. 73
Displays 5
M. Campbell - 2010 27
ByRef
Sub ProcedureA()x = 5MsgBox xCall AddOne(x)MsgBox x
End Sub
Sub AddOne(ByRef i As Integer)i = i + 1
End Sub
p. 73
Calls AddOne and sends x
M. Campbell - 2010 28
ByRef
Sub ProcedureA()x = 5MsgBox xCall AddOne(x)MsgBox x
End Sub
Sub AddOne(ByRef i As Integer)i = i + 1
End Sub
p. 73
AddOne is sent a reference to x, effectively replacing the i with x
M. Campbell - 2010 29
ByRef
Sub ProcedureA()x = 5MsgBox xCall AddOne(x)MsgBox x
End Sub
Sub AddOne(ByRef x As Integer)x = x + 1
End Sub
p. 73
AddOne is sent a reference to x, effectively replacing the i with x
M. Campbell - 2010 30
ByRef
Sub ProcedureA()x = 5MsgBox xCall AddOne(x)MsgBox x
End Sub
Sub AddOne(ByRef x As Integer)x = x + 1
End Sub
p. 73
1 is added to x to get 6This is the same x as in ProcedureA