Unit 6: Functions and Subroutines

538 views 30 slides Sep 20, 2010
Slide 1
Slide 1 of 30
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30

About This Presentation

No description available for this slideshow.


Slide Content

Excel Macros Level 1 Functions and Subroutines

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 M. Campbell - 2010 2 p. 69

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 3

Functions In Activity 4 you saw the function declaration: Public Function Uppercase2(Text As String ) _ As String 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 Indicates the Function's scope: Public: Can be called from any module Private: Can only be called from module it was declared in p. 76

Functions In Activity 4 you saw the function declaration: Public Function Uppercase2(Text As String ) _ As String 4/29/2010 M. Campbell - 2010 6 Declares this code to be a Function p. 69

Functions In Activity 4 you saw the function declaration: Public Function Uppercase2(Text As String ) _ As String 4/29/2010 M. Campbell - 2010 7 The Function's name This is something that you define p. 69

Functions In Activity 4 you saw the function declaration: Public Function Uppercase2(Text As String ) _ As String 4/29/2010 M. Campbell - 2010 8 This is the Function's Parameter list Value is a Parameter When you ran the Function, the value of Text was set to hello The value placed in a Parameter is known as an Argument p. 71

Functions In Activity 4 you saw the function declaration: Public Function Uppercase2(Text As String ) _ As String 4/29/2010 M. Campbell - 2010 9 This is the Function's Return Type In this Function, it will return a value of type String p. 69

Functions In Activity 4 you saw this line of code: Uppercase2 = UCase (Text) 4/29/2010 M. Campbell - 2010 10

Functions In Activity 4 you saw this line of code: Uppercase2 = UCase (Text) 4/29/2010 M. Campbell - 2010 11 This is the Function's Return statement The value of the expression on the right side of the assignment operator will be returned by the Function

Functions In Activity 4 you saw this line of code: Uppercase2 = UCase (Text) 4/29/2010 M. Campbell - 2010 12 Remember that Text is the parameter to the Function. It currently holds the value of hello This sends the value to the UCase Function which converts hello to uppercase

Subroutines In Activity 1 you saw the subroutine declaration: Sub Uppercase() 4/29/2010 M. Campbell - 2010 13

Subroutines In Activity 1 you saw the subroutine declaration: Sub Uppercase() 4/29/2010 M. Campbell - 2010 14 Declares this code to be a Subroutine p. 70

Subroutines In Activity 1 you saw the subroutine declaration: Sub Uppercase() 4/29/2010 M. Campbell - 2010 15 The Subroutine's name This is something that you define p. 70

Subroutines In Activity 1 you saw the subroutine declaration: Sub Uppercase() 4/29/2010 M. Campbell - 2010 16 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

Calling Subroutines Has the form: [Public or Private] Sub SubroutineName (_ Param1 As DataType1 , Param2 As DataType2 ,_ …) To call: Call SubroutineName ( parameters , …) Or SubroutineName parameters , … 4/29/2010 M. Campbell - 2010 17 p. 70

Activities In the Unit 6 Activities complete: Activity 5: Calling a Function with a Subroutine 4/29/2010 M. Campbell - 2010 18

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 M. Campbell - 2010 19 p. 71

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 M. Campbell - 2010 20 p. 71

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 M. Campbell - 2010 21 p. 73

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 M. Campbell - 2010 22 p. 73

Activities In the Unit 6 Activities complete: Activity 6: Optional and Named Arguments 4/29/2010 M. Campbell - 2010 23

ByRef Sub ProcedureA () x = 5 MsgBox x Call AddOne (x) MsgBox x End Sub Sub AddOne ( ByRef i As Integer ) i = i + 1 End Sub M. Campbell - 2010 24 p. 73

ByRef Sub ProcedureA () x = 5 MsgBox x Call AddOne (x) MsgBox x End Sub Sub AddOne ( ByRef i As Integer ) i = i + 1 End Sub M. Campbell - 2010 25 p. 73 Initializes x to 5

ByRef Sub ProcedureA () x = 5 MsgBox x Call AddOne (x) MsgBox x End Sub Sub AddOne ( ByRef i As Integer ) i = i + 1 End Sub M. Campbell - 2010 26 p. 73 Displays 5

ByRef Sub ProcedureA () x = 5 MsgBox x Call AddOne (x) MsgBox x End Sub Sub AddOne ( ByRef i As Integer ) i = i + 1 End Sub M. Campbell - 2010 27 p. 73 Calls AddOne and sends x

ByRef Sub ProcedureA () x = 5 MsgBox x Call AddOne (x) MsgBox x End Sub Sub AddOne ( ByRef i As Integer ) i = i + 1 End Sub M. Campbell - 2010 28 p. 73 AddOne is sent a reference to x, effectively replacing the i with x

ByRef Sub ProcedureA () x = 5 MsgBox x Call AddOne (x) MsgBox x End Sub Sub AddOne ( ByRef x As Integer ) x = x + 1 End Sub M. Campbell - 2010 29 p. 73 AddOne is sent a reference to x, effectively replacing the i with x

ByRef Sub ProcedureA () x = 5 MsgBox x Call AddOne (x) MsgBox x End Sub Sub AddOne ( ByRef x As Integer ) x = x + 1 End Sub M. Campbell - 2010 30 p. 73 1 is added to x to get 6 This is the same x as in ProcedureA
Tags