unit 6: functions and subroutines

30
Excel Macros Level 1 Functions and Subroutines

Upload: matthew-campbell

Post on 24-Apr-2015

766 views

Category:

Education


6 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Unit 6: Functions and Subroutines

Excel Macros Level 1

Functions and Subroutines

Page 2: Unit 6: 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

Page 3: Unit 6: Functions and Subroutines

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

Page 4: Unit 6: Functions and Subroutines

M. Campbell - 2010 4

Functions

In Activity 4 you saw the function declaration:

Public Function Uppercase2(Text As String) _As String

4/29/2010

Page 5: Unit 6: Functions and Subroutines

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

Page 6: Unit 6: Functions and Subroutines

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

Page 7: Unit 6: Functions and Subroutines

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

Page 8: Unit 6: Functions and Subroutines

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

Page 9: Unit 6: Functions and Subroutines

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

Page 10: Unit 6: Functions and Subroutines

M. Campbell - 2010 10

Functions

In Activity 4 you saw this line of code:

Uppercase2 = UCase(Text)

4/29/2010

Page 11: Unit 6: Functions and Subroutines

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

Page 12: Unit 6: Functions and Subroutines

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

Page 13: Unit 6: Functions and Subroutines

M. Campbell - 2010 13

Subroutines

In Activity 1 you saw the subroutine declaration:

Sub Uppercase()

4/29/2010

Page 14: Unit 6: Functions and Subroutines

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

Page 15: Unit 6: Functions and Subroutines

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

Page 16: Unit 6: Functions and Subroutines

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

Page 17: Unit 6: Functions and Subroutines

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

Page 18: Unit 6: Functions and Subroutines

M. Campbell - 2010 18

Activities

In the Unit 6 Activities complete: Activity 5: Calling a Function with a Subroutine

4/29/2010

Page 19: Unit 6: Functions and Subroutines

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

Page 20: Unit 6: Functions and Subroutines

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

Page 21: Unit 6: Functions and Subroutines

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

Page 22: Unit 6: Functions and Subroutines

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

Page 23: Unit 6: Functions and Subroutines

M. Campbell - 2010 23

Activities

In the Unit 6 Activities complete: Activity 6: Optional and Named Arguments

4/29/2010

Page 24: Unit 6: Functions and Subroutines

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

Page 25: Unit 6: Functions and Subroutines

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

Page 26: Unit 6: Functions and Subroutines

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

Page 27: Unit 6: Functions and Subroutines

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

Page 28: Unit 6: Functions and Subroutines

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

Page 29: Unit 6: Functions and Subroutines

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

Page 30: Unit 6: Functions and Subroutines

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