SlideShare a Scribd company logo
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 p. 76 Indicates the Function's scope: Public: ,[object Object],Private: ,[object Object],[object Object]
Functions In Activity 4 you saw the function declaration: Public Function Uppercase2(Text As String) _ 	As String 4/29/2010 M. Campbell - 2010 7 p. 69 The Function's name This is something that you define
Functions In Activity 4 you saw the function declaration: Public Function Uppercase2(Text As String) _ 	As String 4/29/2010 M. Campbell - 2010 8 p. 71 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
Functions In Activity 4 you saw the function declaration: Public Function Uppercase2(Text As String) _ As String 4/29/2010 M. Campbell - 2010 9 p. 69 This is the Function's Return Type In this Function, it will return a value of type String
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 p. 70 Declares this code to be a Subroutine
Subroutines In Activity 1 you saw the subroutine declaration: Sub Uppercase() 4/29/2010 M. Campbell - 2010 15 p. 70 The Subroutine's name This is something that you define
Subroutines In Activity 1 you saw the subroutine declaration: Sub Uppercase() 4/29/2010 M. Campbell - 2010 16 p. 76 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
Calling Subroutines Has the form: [Public or Private] Sub SubroutineName (_ Param1 As DataType1, Param2 As DataType2,_ …) To call: Call SubroutineName(parameters, …) Or SubroutineNameparameters, … 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 SubDisplayName(firstNameAs String, _ OptionallastNameAs 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 SubDisplayName(firstNameAs String, _ OptionallastNameAs String, _ 			Optional midName As String) To call procedure with only firstName and midName: CallDisplayName("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: CallDisplayName("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 CallDisplayName(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 SubProcedureA() 	x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(ByRefiAs Integer) i = i + 1 End Sub M. Campbell - 2010 24 p. 73
ByRef SubProcedureA() 	x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(ByRefiAs Integer) i = i + 1 End Sub M. Campbell - 2010 25 p. 73 Initializes x to 5
ByRef SubProcedureA() 	x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(ByRefiAs Integer) i = i + 1 End Sub M. Campbell - 2010 26 p. 73 Displays 5
ByRef SubProcedureA() 	x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(ByRefiAs Integer) i = i + 1 End Sub M. Campbell - 2010 27 p. 73 Calls AddOne and sends x
ByRef SubProcedureA() 	x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(ByRefiAs 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 SubProcedureA() 	x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(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 SubProcedureA() 	x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(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
Unit 6: Functions and Subroutines

More Related Content

What's hot

functions
functionsfunctions
functions
Itishree Soni
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
SaraswathiTAsstProfI
 
Basics of Functional Programming
Basics of Functional ProgrammingBasics of Functional Programming
Basics of Functional Programming
Sartaj Singh
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
primeteacher32
 
Functions
FunctionsFunctions
Functions
Septi Ratnasari
 
c.p function
c.p functionc.p function
c.p function
giri5624
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
Learn By Watch
 
Qbesic programming class 9
Qbesic programming class 9Qbesic programming class 9
Qbesic programming class 9
bhuwanbist1
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Shakti Singh Rathore
 
F# Eye For The C# Guy - Seattle 2013
F# Eye For The C# Guy - Seattle 2013F# Eye For The C# Guy - Seattle 2013
F# Eye For The C# Guy - Seattle 2013
Phillip Trelford
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
Docent Education
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
tjunicornfx
 
polymorphism and virtual function
polymorphism and virtual functionpolymorphism and virtual function
polymorphism and virtual function
Bhanuprataparya
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedure
pragya ratan
 
Modular programming
Modular programmingModular programming
Modular programming
bhuwanbist1
 
Function
Function Function
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
Megha V
 
structured programming
structured programmingstructured programming
structured programming
Ahmad54321
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
Functional Java 8 in everyday life
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday life
Andrea Iacono
 

What's hot (20)

functions
functionsfunctions
functions
 
3. functions modules_programs (1)
3. functions modules_programs (1)3. functions modules_programs (1)
3. functions modules_programs (1)
 
Basics of Functional Programming
Basics of Functional ProgrammingBasics of Functional Programming
Basics of Functional Programming
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
 
Functions
FunctionsFunctions
Functions
 
c.p function
c.p functionc.p function
c.p function
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
Qbesic programming class 9
Qbesic programming class 9Qbesic programming class 9
Qbesic programming class 9
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
F# Eye For The C# Guy - Seattle 2013
F# Eye For The C# Guy - Seattle 2013F# Eye For The C# Guy - Seattle 2013
F# Eye For The C# Guy - Seattle 2013
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
 
polymorphism and virtual function
polymorphism and virtual functionpolymorphism and virtual function
polymorphism and virtual function
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedure
 
Modular programming
Modular programmingModular programming
Modular programming
 
Function
Function Function
Function
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
 
structured programming
structured programmingstructured programming
structured programming
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Functional Java 8 in everyday life
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday life
 

Viewers also liked

Chapter 3 Excel Macros
Chapter 3 Excel MacrosChapter 3 Excel Macros
Chapter 3 Excel Macros
Matthew Campbell, OCT
 
Unit 6: Functions and Subroutines - Part 2/2
Unit 6: Functions and Subroutines - Part 2/2Unit 6: Functions and Subroutines - Part 2/2
Unit 6: Functions and Subroutines - Part 2/2
Matthew Campbell, OCT
 
Chapter 2: Preliminaries
Chapter 2: PreliminariesChapter 2: Preliminaries
Chapter 2: Preliminaries
Matthew Campbell, OCT
 
Unit 7: Built-In Functions
Unit 7: Built-In FunctionsUnit 7: Built-In Functions
Unit 7: Built-In Functions
Matthew Campbell, OCT
 
Vba part 1
Vba part 1Vba part 1
Vba part 1
Morteza Noshad
 
Vba Excel Level 2
Vba Excel Level 2Vba Excel Level 2
Vba Excel Level 2
Ben Miu CIM® FCSI A+
 
Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
Ben Miu CIM® FCSI A+
 
Tableau Server Basics
Tableau Server BasicsTableau Server Basics
Tableau Server Basics
Nithyamoorthy Sadaiyan
 
Tableau Drive, A new methodology for scaling your analytic culture
Tableau Drive, A new methodology for scaling your analytic cultureTableau Drive, A new methodology for scaling your analytic culture
Tableau Drive, A new methodology for scaling your analytic culture
Tableau Software
 
Tableau Software - Business Analytics and Data Visualization
Tableau Software - Business Analytics and Data VisualizationTableau Software - Business Analytics and Data Visualization
Tableau Software - Business Analytics and Data Visualization
lesterathayde
 
Learning Tableau - Data, Graphs, Filters, Dashboards and Advanced features
Learning Tableau -  Data, Graphs, Filters, Dashboards and Advanced featuresLearning Tableau -  Data, Graphs, Filters, Dashboards and Advanced features
Learning Tableau - Data, Graphs, Filters, Dashboards and Advanced features
Venkata Reddy Konasani
 

Viewers also liked (11)

Chapter 3 Excel Macros
Chapter 3 Excel MacrosChapter 3 Excel Macros
Chapter 3 Excel Macros
 
Unit 6: Functions and Subroutines - Part 2/2
Unit 6: Functions and Subroutines - Part 2/2Unit 6: Functions and Subroutines - Part 2/2
Unit 6: Functions and Subroutines - Part 2/2
 
Chapter 2: Preliminaries
Chapter 2: PreliminariesChapter 2: Preliminaries
Chapter 2: Preliminaries
 
Unit 7: Built-In Functions
Unit 7: Built-In FunctionsUnit 7: Built-In Functions
Unit 7: Built-In Functions
 
Vba part 1
Vba part 1Vba part 1
Vba part 1
 
Vba Excel Level 2
Vba Excel Level 2Vba Excel Level 2
Vba Excel Level 2
 
Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
 
Tableau Server Basics
Tableau Server BasicsTableau Server Basics
Tableau Server Basics
 
Tableau Drive, A new methodology for scaling your analytic culture
Tableau Drive, A new methodology for scaling your analytic cultureTableau Drive, A new methodology for scaling your analytic culture
Tableau Drive, A new methodology for scaling your analytic culture
 
Tableau Software - Business Analytics and Data Visualization
Tableau Software - Business Analytics and Data VisualizationTableau Software - Business Analytics and Data Visualization
Tableau Software - Business Analytics and Data Visualization
 
Learning Tableau - Data, Graphs, Filters, Dashboards and Advanced features
Learning Tableau -  Data, Graphs, Filters, Dashboards and Advanced featuresLearning Tableau -  Data, Graphs, Filters, Dashboards and Advanced features
Learning Tableau - Data, Graphs, Filters, Dashboards and Advanced features
 

Similar to Unit 6: Functions and Subroutines

User defined functions in matlab
User defined functions in  matlabUser defined functions in  matlab
User defined functions in matlab
Infinity Tech Solutions
 
Unit 5: Variables
Unit 5: VariablesUnit 5: Variables
Unit 5: Variables
Matthew Campbell, OCT
 
ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptx
AaliyanShaikh
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
Mehul Desai
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
Terry Yoast
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
Terry Yoast
 
Savitch ch 04
Savitch ch 04Savitch ch 04
Savitch ch 04
Terry Yoast
 
Materi 6 user definedfunction
Materi 6 user definedfunctionMateri 6 user definedfunction
Materi 6 user definedfunction
Al Frilantika
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
MikialeTesfamariam
 
Your 1ab8 c file is doe by Friday 1159pm to be submitted.pdf
Your 1ab8 c file is doe by Friday 1159pm to be submitted.pdfYour 1ab8 c file is doe by Friday 1159pm to be submitted.pdf
Your 1ab8 c file is doe by Friday 1159pm to be submitted.pdf
abhijitmaskey
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
Arpit Meena
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
KavithaChekuri3
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
Liju Thomas
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
nmahi96
 
Function
FunctionFunction
Savitch ch 11
Savitch ch 11Savitch ch 11
Savitch ch 11
Terry Yoast
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
prasnt1
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
Ruth Marvin
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
AnuragBharti27
 

Similar to Unit 6: Functions and Subroutines (20)

User defined functions in matlab
User defined functions in  matlabUser defined functions in  matlab
User defined functions in matlab
 
Unit 5: Variables
Unit 5: VariablesUnit 5: Variables
Unit 5: Variables
 
ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptx
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
 
Savitch Ch 04
Savitch Ch 04Savitch Ch 04
Savitch Ch 04
 
Savitch ch 04
Savitch ch 04Savitch ch 04
Savitch ch 04
 
Materi 6 user definedfunction
Materi 6 user definedfunctionMateri 6 user definedfunction
Materi 6 user definedfunction
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 
Your 1ab8 c file is doe by Friday 1159pm to be submitted.pdf
Your 1ab8 c file is doe by Friday 1159pm to be submitted.pdfYour 1ab8 c file is doe by Friday 1159pm to be submitted.pdf
Your 1ab8 c file is doe by Friday 1159pm to be submitted.pdf
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
 
functions.pptx
functions.pptxfunctions.pptx
functions.pptx
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
 
Function
FunctionFunction
Function
 
Savitch ch 11
Savitch ch 11Savitch ch 11
Savitch ch 11
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 
Python Functions.pptx
Python Functions.pptxPython Functions.pptx
Python Functions.pptx
 

Recently uploaded

How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
Celine George
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
deepaannamalai16
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
IsmaelVazquez38
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
ImMuslim
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
nitinpv4ai
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
danielkiash986
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
David Douglas School District
 
Skimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S EliotSkimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S Eliot
nitinpv4ai
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 

Recently uploaded (20)

How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
 
Skimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S EliotSkimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S Eliot
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 

Unit 6: Functions and Subroutines

  • 1. Excel Macros Level 1 Functions and Subroutines
  • 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 M. Campbell - 2010 2 p. 69
  • 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 3
  • 4. Functions In Activity 4 you saw the function declaration: Public Function Uppercase2(Text As String) _ As String 4/29/2010 M. Campbell - 2010 4
  • 5.
  • 6. Functions In Activity 4 you saw the function declaration: Public Function Uppercase2(Text As String) _ As String 4/29/2010 M. Campbell - 2010 7 p. 69 The Function's name This is something that you define
  • 7. Functions In Activity 4 you saw the function declaration: Public Function Uppercase2(Text As String) _ As String 4/29/2010 M. Campbell - 2010 8 p. 71 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
  • 8. Functions In Activity 4 you saw the function declaration: Public Function Uppercase2(Text As String) _ As String 4/29/2010 M. Campbell - 2010 9 p. 69 This is the Function's Return Type In this Function, it will return a value of type String
  • 9. Functions In Activity 4 you saw this line of code: Uppercase2 = UCase(Text) 4/29/2010 M. Campbell - 2010 10
  • 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
  • 11. 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
  • 12. Subroutines In Activity 1 you saw the subroutine declaration: Sub Uppercase() 4/29/2010 M. Campbell - 2010 13
  • 13. Subroutines In Activity 1 you saw the subroutine declaration: Sub Uppercase() 4/29/2010 M. Campbell - 2010 14 p. 70 Declares this code to be a Subroutine
  • 14. Subroutines In Activity 1 you saw the subroutine declaration: Sub Uppercase() 4/29/2010 M. Campbell - 2010 15 p. 70 The Subroutine's name This is something that you define
  • 15. Subroutines In Activity 1 you saw the subroutine declaration: Sub Uppercase() 4/29/2010 M. Campbell - 2010 16 p. 76 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
  • 16. Calling Subroutines Has the form: [Public or Private] Sub SubroutineName (_ Param1 As DataType1, Param2 As DataType2,_ …) To call: Call SubroutineName(parameters, …) Or SubroutineNameparameters, … 4/29/2010 M. Campbell - 2010 17 p. 70
  • 17. Activities In the Unit 6 Activities complete: Activity 5: Calling a Function with a Subroutine 4/29/2010 M. Campbell - 2010 18
  • 18. Optional Arguments Can set some arguments to be optional SubDisplayName(firstNameAs String, _ OptionallastNameAs 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
  • 19. Optional Arguments SubDisplayName(firstNameAs String, _ OptionallastNameAs String, _ Optional midName As String) To call procedure with only firstName and midName: CallDisplayName("Winnie", , "the") Must include blank spot as arguments expected in order defined in parameter list 4/29/2010 M. Campbell - 2010 20 p. 71
  • 20. Positional Arguments In previous example, we used positional arguments: CallDisplayName("Winnie", , "the") The position of arguments tells VBA which parameters they fill 4/29/2010 M. Campbell - 2010 21 p. 73
  • 21. Named Arguments Naming the arguments: Improves readability Removes the need for blank spaces Allows arguments to go in any order CallDisplayName(midName:= "the", _ firstName:= Winnie") 4/29/2010 M. Campbell - 2010 22 p. 73
  • 22. Activities In the Unit 6 Activities complete: Activity 6: Optional and Named Arguments 4/29/2010 M. Campbell - 2010 23
  • 23. ByRef SubProcedureA() x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(ByRefiAs Integer) i = i + 1 End Sub M. Campbell - 2010 24 p. 73
  • 24. ByRef SubProcedureA() x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(ByRefiAs Integer) i = i + 1 End Sub M. Campbell - 2010 25 p. 73 Initializes x to 5
  • 25. ByRef SubProcedureA() x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(ByRefiAs Integer) i = i + 1 End Sub M. Campbell - 2010 26 p. 73 Displays 5
  • 26. ByRef SubProcedureA() x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(ByRefiAs Integer) i = i + 1 End Sub M. Campbell - 2010 27 p. 73 Calls AddOne and sends x
  • 27. ByRef SubProcedureA() x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(ByRefiAs 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
  • 28. ByRef SubProcedureA() x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(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
  • 29. ByRef SubProcedureA() x = 5 MsgBox x CallAddOne(x) MsgBox x End Sub SubAddOne(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