SlideShare ist ein Scribd-Unternehmen logo
1 von 30
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

Weitere ähnliche Inhalte

Was ist angesagt?

Basics of Functional Programming
Basics of Functional ProgrammingBasics of Functional Programming
Basics of Functional ProgrammingSartaj Singh
 
c.p function
c.p functionc.p function
c.p functiongiri5624
 
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 9bhuwanbist1
 
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 2013Phillip Trelford
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Nettjunicornfx
 
polymorphism and virtual function
polymorphism and virtual functionpolymorphism and virtual function
polymorphism and virtual functionBhanuprataparya
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedurepragya ratan
 
Modular programming
Modular programmingModular programming
Modular programmingbhuwanbist1
 
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 programmingAhmad54321
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide shareDevashish Kumar
 
Functional Java 8 in everyday life
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday lifeAndrea Iacono
 

Was ist angesagt? (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
 

Andere mochten auch

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/2Matthew Campbell, OCT
 
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 cultureTableau 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 Visualizationlesterathayde
 
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 featuresVenkata Reddy Konasani
 

Andere mochten auch (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
 

Ähnlich wie Unit 6: Functions and Subroutines

ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxAaliyanShaikh
 
Materi 6 user definedfunction
Materi 6 user definedfunctionMateri 6 user definedfunction
Materi 6 user definedfunctionAl Frilantika
 
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.pdfabhijitmaskey
 
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
 
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 programmingnmahi96
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdfprasnt1
 
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_argumentsRuth Marvin
 

Ähnlich wie 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
 

Kürzlich hochgeladen

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 

Kürzlich hochgeladen (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 

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