Upcoming SlideShare
×

# Unit 6: Functions and Subroutines - Part 2/2

696 views

Published on

Published in: Education
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
696
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
11
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Unit 6: Functions and Subroutines - Part 2/2

1. 1. Excel Macros Level 1<br />Functions and Subroutines<br />
2. 2. ByRef<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByRefiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />2<br />p. 73<br />This displays 6 as x had 1 added to it by AddOne<br />
3. 3. ByVal<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByValiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />3<br />p. 73<br />
4. 4. ByVal<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByValiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />4<br />p. 73<br />Initializes x to 5<br />
5. 5. ByVal<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByValiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />5<br />p. 73<br />Displays 5<br />
6. 6. ByVal<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByValiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />6<br />p. 73<br />Calls AddOne and sends value of x (in this case 5)<br />
7. 7. ByVal<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByValiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />7<br />p. 73<br />AddOne is sent the value of x only, so i is set to 5<br />
8. 8. ByVal<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByValiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />8<br />p. 73<br />1 is added to i to get 6<br />However, ProcedureA cannot see i<br />
9. 9. ByVal<br />SubProcedureA()<br /> x = 5<br />MsgBox x<br />CallAddOne(x)<br />MsgBox x<br />End Sub<br />SubAddOne(ByValiAs Integer)<br />i = i + 1<br />End Sub<br />M. Campbell - 2010<br />9<br />p. 73<br />This displays 5 as x was not changed by AddOne<br />
10. 10. p. 75<br />ByRef<br />ByVal<br />Pass By Reference<br />Allows the argument to be changed<br />Takes less memory and time<br />If not specified, VBA defaults to ByRef<br />Pass By Value<br />Prevents the argument from being changed<br />Takes more memory and time<br />4/29/2010<br />M. Campbell - 2010<br />10<br />
11. 11. Exiting a Procedure<br />To exit a function early:<br />Exit Function<br />To exit a subroutine early:<br />Exit Sub<br />4/29/2010<br />M. Campbell - 2010<br />11<br />p. 76<br />