SlideShare a Scribd company logo
1 of 42
Excel and
the VBELECTURE 1
Important things to know in Excel
The VBE environment
A first go at some coding!
SUMMARY
IMPORTANT THINGS TO
KNOW ABOUT EXCEL
CELL REFERENCES IN EXCEL
Each cell has an ‘address’:
“B5” Row 5, Column B.
R5C2 Row 5, Column 2.
R5
C2
B5
CELL REFERENCES IN EXCEL
Each cell has an ‘address’:
“B5” Row 5, Column B.
R5C2 Row 5, Column 2.
Cells(5,2) Row 5, Column 2.
Preferred notation
within VBA because
it can be looped.
R5
C2
Cells(5,2)
 A range can be:
One cell (e.g., G2), or
NAMING RANGES
 A range can be:
One cell (e.g., G2), or
Multiple cells, which are next to each other (e.g., A2:E2)
NAMING RANGES
 What’s the benefit of naming ranges?
If you move a named range:
 The cell reference changes, but
 The range name does not change
 What’s that mean for you, the programmer?
If you refer to a cell reference (e.g., G2), in your code,
…and then move the contents of that cell
…you have to go back and update your code
If you refer to a named range (e.g., “PortfolioReturn”), in
your code
…and then move the contents of that cell (cut + paste)
…you don’t have to update your code.
NAMING RANGES
A few rules…
MUST start with a letter
CANNOT contain any spaces
CANNOT be the name of another range
CANNOT be the name of the cell reference for another cell
in Excel.
NAMING RANGES
“RET1” is already a cell in Excel so I can’t give another cell this name.
1. SAVE AS .xlsm
2. Save FREQUENTLY!! Excel crashes!
3. Saving your Excel workbook ALSO saves your VBA.
4. Use good files names so that you know which file
is the newest version you’re working on.
• Dissertation.xlsm
• Disso.xlsm
• myDisso.xlsm
• DissoNew.xlsm
• DissoNewest.xlsm
SAVING YOUR WORK
m for Macro Enabled
• 13oct04Dissertation.xlsm
• 2013oct04_Disso.xlsm
• 04oct13_myDisso.xlsm
Visual
Basic
Editor
THE VBE
GETTING FAMILIAR WITH THE VBE
Alt + F11: Toggle between Excel and VBE, OR
Go to the VBE
from Excel
Go back to Excel
from the VBE
GETTING FAMILIAR WITH THE VBE
Excel File This is where you
write your code
GETTING FAMILIAR WITH THE VBE
Code for
data in
Worksheets
goes here
Code in a specific sheet can
only be executed for data
and cells in that sheet.
You CANNOT refer to cells
in another worksheet or on
a userform
GETTING FAMILIAR WITH THE VBE
Commands
for the
Workbook
go here
For example
•Open the software when
the workbook opens
•Save the workbook when
the software closes
• Etc…
GETTING FAMILIAR WITH THE VBE
Code for
Userforms
go here
Code which uses or refers
to information obtained on
the userform
Code which executes when
tools on the userform are
used (e.g., command
buttons)
GETTING FAMILIAR WITH THE VBE
Other code,
not specific
to any one
userform or
worksheet
goes in a
module
For
calculations, optimisation,
or any code you want to be
able to use at any time
 Insert a Userform
GETTING FAMILIAR WITH THE VBE
 Insert a Userform
 Insert a module
GETTING FAMILIAR WITH THE VBE
 Insert a Userform
 Insert a module
 Insert a procedure
GETTING FAMILIAR WITH THE VBE
We’ll come back to this
later in the term
INSERT A PROCEDURE
You will use both
Sub’s and Function’s
during your FYP.
We will discuss the
difference later.
The name should be
descriptive of your
Sub or Function.
Cannot contain
spaces.
Click OK. This is what you should get:
Write your code here.
The Sub Procedure is now in the Module
EDITING EXCEL USING VBA
ACTIVATE A WORKBOOK
Name of my workbook in “ ”.
Tell VBA which workbook
you want to work with
ACTIVATE A WORKSHEET
Tell VBA which worksheet
you want to work with
Name of my worksheet in “ ”.
NAMING RANGES
Use .Name
Assign range names to…
• One cell, or
Must be in “ ”
because it’s text!
NAMING RANGES
Use .Name
Assign range names to…
• One cell, or
• A range of cells
Must be in “ ”
because it’s text!
NAMING RANGES
Assign range names to…
• One cell, or
• A range of cells
Row 7, Column 1 (top left)
Through to
Row 9, Column 3 (btm right)
ASSIGNING VALUES
Enter a number into…
• One cell, or
• A range of cells
InputBox(“ ”)
Used to get input from the user
ASSIGNING VALUES
Enter text into…
• One cell, or
• A range of cells
Text MUST be in “ ”
 Check the range names by clicking on the cells
 Did the code do what you thought it would?
RUN THIS CODE (PRESS F5)
USE EXCEL BUILT-IN FUNCTIONS FROM VBA
Activate the correct
worksheet
USE EXCEL BUILT-IN FUNCTIONS FROM VBA
Name the range where the
numbers are in Excel
USE EXCEL BUILT-IN FUNCTIONS FROM VBA
Enter column headings
USE EXCEL BUILT-IN FUNCTIONS FROM VBA
Three ways to do the same thing
Use what is best for the situation
Enters the equation
=Sum(numSeries)
into Row 2, Col 3.
USE EXCEL BUILT-IN FUNCTIONS FROM VBA
Uses the built in Min function
to find the minimum of the
range named numSeries.
Three ways to do the same thing
Use what is best for the situation
USE EXCEL BUILT-IN FUNCTIONS FROM VBA
Uses the built in Max function
to find the maximum of the range
from Rows 1 to 7 in Col 1.
Three ways to do the same thing
Use what is best for the situation
 In column 1 of your worksheet, create a list of 7 random
numbers.
 Insert a new Sub procedure called Exercise1.
 Enter & run the following code:
EXERCISE #1
 From the Developer Tab:
Choose Insert
Under Form Controls,
Select command Button
 Draw a command button.
 Select the macro you want to assign to this
button. Only procedures in Modules will appear
in the list.
EXECUTING CODE USING FORM CONTROLS.
Choose Exercise1
Turn off Design Mode.
 Make sure it is not highlighted
 Your button now runs the code from the Sub
Exercise1.
Try changing your random numbers & re-running
your code by using your button.
EXECUTING CODE USING FORM CONTROLS.
Create a Sub procedure called “Exercise2”
Code the following in this sub:
Activate your worksheet (use the sheet called ‘Example 3’)
Assign a range name to the range of weights
Assign a range name to the range of mean returns
Assign a range name to cell G2, which will contain a
formula for the portfolio return (next step)
Assign equal weights into the weights range
Enter the formula to calculate the portfolio return into G2
(use the SUMPRODUCT formula)
Run your code (Put your cursor inside the sub & hit F5).
Go back to Excel and make sure it’s correct.
EXERCISE #2
You are ready to move on when:
LO1: You can name the address of a cell using Cells(r,c) or
RC notation.
LO2: In the VBE, you know the difference between adding
code in a worksheet, a userform and a module.
LO3: You can name ranges within Excel as well as name
Excel ranges within VBA.
LO4: You can assign numerical or text values to individual
cells and ranges of cells from within VBA.
LO5: You can describe the difference between the 3 ways
of using Excel built-in functions within VBA.
LEARNING OUTCOMES
THE END

More Related Content

What's hot

Summarize Data Using a Formula
Summarize Data Using a FormulaSummarize Data Using a Formula
Summarize Data Using a Formula
coachhahn
 
Excel functions formulas
Excel functions formulasExcel functions formulas
Excel functions formulas
LearnIT@UD
 
Excel presentation
Excel presentationExcel presentation
Excel presentation
DUSPviz
 
Excel Chapter 2 - Inserting Formulas in a Worksheet
Excel Chapter 2 - Inserting Formulas in a WorksheetExcel Chapter 2 - Inserting Formulas in a Worksheet
Excel Chapter 2 - Inserting Formulas in a Worksheet
dpd
 
Chapter 1 lesson 2 MS Excel and its Interface
Chapter 1 lesson 2 MS Excel and its InterfaceChapter 1 lesson 2 MS Excel and its Interface
Chapter 1 lesson 2 MS Excel and its Interface
Ronnel de Jesus
 

What's hot (20)

Performing basic computation (Microsoft Excel)
Performing basic computation (Microsoft Excel)Performing basic computation (Microsoft Excel)
Performing basic computation (Microsoft Excel)
 
Summarize Data Using a Formula
Summarize Data Using a FormulaSummarize Data Using a Formula
Summarize Data Using a Formula
 
Excel functions formulas
Excel functions formulasExcel functions formulas
Excel functions formulas
 
50 MS Excel Tips and Tricks
50 MS Excel Tips and Tricks 50 MS Excel Tips and Tricks
50 MS Excel Tips and Tricks
 
Fill series. Data validation. Excel Tutorial
Fill series. Data validation. Excel TutorialFill series. Data validation. Excel Tutorial
Fill series. Data validation. Excel Tutorial
 
New Dynamic Array Functions. Excel Tutorial
New Dynamic Array Functions. Excel TutorialNew Dynamic Array Functions. Excel Tutorial
New Dynamic Array Functions. Excel Tutorial
 
Lesson 5
Lesson 5Lesson 5
Lesson 5
 
How to apply a formula and macro in excel......by irfan afzal
How to apply a formula and macro in excel......by irfan afzalHow to apply a formula and macro in excel......by irfan afzal
How to apply a formula and macro in excel......by irfan afzal
 
Fieldsymbols
FieldsymbolsFieldsymbols
Fieldsymbols
 
Excel presentation
Excel presentationExcel presentation
Excel presentation
 
Common MS Excel and MS Excel 2013 useful tricks. By Ashot Engibaryan
Common MS Excel and MS Excel 2013 useful tricks. By Ashot EngibaryanCommon MS Excel and MS Excel 2013 useful tricks. By Ashot Engibaryan
Common MS Excel and MS Excel 2013 useful tricks. By Ashot Engibaryan
 
Excel Chapter 2 - Inserting Formulas in a Worksheet
Excel Chapter 2 - Inserting Formulas in a WorksheetExcel Chapter 2 - Inserting Formulas in a Worksheet
Excel Chapter 2 - Inserting Formulas in a Worksheet
 
Chapter 1 lesson 2 MS Excel and its Interface
Chapter 1 lesson 2 MS Excel and its InterfaceChapter 1 lesson 2 MS Excel and its Interface
Chapter 1 lesson 2 MS Excel and its Interface
 
Excel Basics 2
Excel Basics 2Excel Basics 2
Excel Basics 2
 
Using Formulas in Excel
Using Formulas in ExcelUsing Formulas in Excel
Using Formulas in Excel
 
090225 Excel Training
090225 Excel Training090225 Excel Training
090225 Excel Training
 
Basic Formulas - Excel 2013 Tutorial
Basic Formulas - Excel 2013 TutorialBasic Formulas - Excel 2013 Tutorial
Basic Formulas - Excel 2013 Tutorial
 
Functions and Charts in Microsoft Excel
Functions and Charts in Microsoft ExcelFunctions and Charts in Microsoft Excel
Functions and Charts in Microsoft Excel
 
MS Excel 2nd
MS Excel 2ndMS Excel 2nd
MS Excel 2nd
 
Excel-VBA
Excel-VBAExcel-VBA
Excel-VBA
 

Viewers also liked

Unit 8 assignment 2a teona mc fadden finish tonight
Unit 8 assignment 2a  teona mc fadden finish tonightUnit 8 assignment 2a  teona mc fadden finish tonight
Unit 8 assignment 2a teona mc fadden finish tonight
teonamaud
 
Cem 350 control systems 9 2016
Cem 350 control systems 9 2016Cem 350 control systems 9 2016
Cem 350 control systems 9 2016
miresmaeil
 
Project Controlling and Project Monitoring
Project Controlling and Project MonitoringProject Controlling and Project Monitoring
Project Controlling and Project Monitoring
haroldtaylor1113
 

Viewers also liked (13)

Unit 8 assignment 2a teona mc fadden finish tonight
Unit 8 assignment 2a  teona mc fadden finish tonightUnit 8 assignment 2a  teona mc fadden finish tonight
Unit 8 assignment 2a teona mc fadden finish tonight
 
The ultimate guide to cost management
The ultimate guide to cost managementThe ultimate guide to cost management
The ultimate guide to cost management
 
SPSHOU SharePoint 2013 Best Practices
SPSHOU SharePoint 2013 Best PracticesSPSHOU SharePoint 2013 Best Practices
SPSHOU SharePoint 2013 Best Practices
 
Forms, Getting Your Money's Worth
Forms, Getting Your Money's WorthForms, Getting Your Money's Worth
Forms, Getting Your Money's Worth
 
Cem 350 control systems 9 2016
Cem 350 control systems 9 2016Cem 350 control systems 9 2016
Cem 350 control systems 9 2016
 
Sketchnote Collection 1 - Sacha Chua
Sketchnote Collection 1 - Sacha ChuaSketchnote Collection 1 - Sacha Chua
Sketchnote Collection 1 - Sacha Chua
 
Project Monitoring and Control
Project Monitoring and ControlProject Monitoring and Control
Project Monitoring and Control
 
Project Controlling and Project Monitoring
Project Controlling and Project MonitoringProject Controlling and Project Monitoring
Project Controlling and Project Monitoring
 
Practical Sketchnoting
Practical SketchnotingPractical Sketchnoting
Practical Sketchnoting
 
How to build an Intranet portal in SharePoint using out of the box features
How to build an Intranet portal in SharePoint using out of the box featuresHow to build an Intranet portal in SharePoint using out of the box features
How to build an Intranet portal in SharePoint using out of the box features
 
Hvac presentation for beginers
Hvac presentation for beginersHvac presentation for beginers
Hvac presentation for beginers
 
The Sketchnote Mini-Workshop
The Sketchnote Mini-WorkshopThe Sketchnote Mini-Workshop
The Sketchnote Mini-Workshop
 
Sketchnoting: 10 Tips to get Started
Sketchnoting: 10 Tips to get StartedSketchnoting: 10 Tips to get Started
Sketchnoting: 10 Tips to get Started
 

Similar to Ma3696 Lecture 1

Advanced Spreadsheet Skills-1.pptx
Advanced Spreadsheet Skills-1.pptxAdvanced Spreadsheet Skills-1.pptx
Advanced Spreadsheet Skills-1.pptx
CliffordBorromeo
 
Excel Formulas Functions
Excel Formulas FunctionsExcel Formulas Functions
Excel Formulas Functions
simply_coool
 
Excel Formulas Functions 2007
Excel Formulas Functions 2007Excel Formulas Functions 2007
Excel Formulas Functions 2007
simply_coool
 

Similar to Ma3696 Lecture 1 (20)

MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 
Vba part 1
Vba part 1Vba part 1
Vba part 1
 
200 Mega eBook Collection- http://bit.ly/3WEZuYJ
200 Mega eBook Collection- http://bit.ly/3WEZuYJ200 Mega eBook Collection- http://bit.ly/3WEZuYJ
200 Mega eBook Collection- http://bit.ly/3WEZuYJ
 
Vba part 1
Vba part 1Vba part 1
Vba part 1
 
Excel useful tips
Excel useful tipsExcel useful tips
Excel useful tips
 
Excel tips
Excel tipsExcel tips
Excel tips
 
Excel tips
Excel tipsExcel tips
Excel tips
 
Excel Useful Tips
Excel Useful TipsExcel Useful Tips
Excel Useful Tips
 
35 Useful Excel Tips
35 Useful Excel Tips35 Useful Excel Tips
35 Useful Excel Tips
 
Excel useful tips
Excel useful tipsExcel useful tips
Excel useful tips
 
Excel useful tips
Excel useful tipsExcel useful tips
Excel useful tips
 
Advanced Spreadsheet Skills-1.pptx
Advanced Spreadsheet Skills-1.pptxAdvanced Spreadsheet Skills-1.pptx
Advanced Spreadsheet Skills-1.pptx
 
Excel Tips
Excel TipsExcel Tips
Excel Tips
 
VBA
VBAVBA
VBA
 
Spreadsheets[1]
Spreadsheets[1]Spreadsheets[1]
Spreadsheets[1]
 
Excel Formulas Functions
Excel Formulas FunctionsExcel Formulas Functions
Excel Formulas Functions
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in India
 
Excel Formulas Functions 2007
Excel Formulas Functions 2007Excel Formulas Functions 2007
Excel Formulas Functions 2007
 
Learn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in DelhiLearn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in Delhi
 
SQL Views
SQL ViewsSQL Views
SQL Views
 

More from Brunel University (8)

MA3696 Lecture 8
MA3696 Lecture 8MA3696 Lecture 8
MA3696 Lecture 8
 
MA3696 Lecture 7
MA3696 Lecture 7MA3696 Lecture 7
MA3696 Lecture 7
 
MA3696 Lecture 6
MA3696 Lecture 6MA3696 Lecture 6
MA3696 Lecture 6
 
MA3696 Lecture 5
MA3696 Lecture 5MA3696 Lecture 5
MA3696 Lecture 5
 
Ma3696 lecture 4
Ma3696 lecture 4Ma3696 lecture 4
Ma3696 lecture 4
 
Ma3696 Lecture 3
Ma3696 Lecture 3Ma3696 Lecture 3
Ma3696 Lecture 3
 
Ma3696 Lecture 2
Ma3696 Lecture 2Ma3696 Lecture 2
Ma3696 Lecture 2
 
Ma3696 Lecture 0
Ma3696 Lecture 0Ma3696 Lecture 0
Ma3696 Lecture 0
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 

Ma3696 Lecture 1

  • 2. Important things to know in Excel The VBE environment A first go at some coding! SUMMARY
  • 4. CELL REFERENCES IN EXCEL Each cell has an ‘address’: “B5” Row 5, Column B. R5C2 Row 5, Column 2. R5 C2 B5
  • 5. CELL REFERENCES IN EXCEL Each cell has an ‘address’: “B5” Row 5, Column B. R5C2 Row 5, Column 2. Cells(5,2) Row 5, Column 2. Preferred notation within VBA because it can be looped. R5 C2 Cells(5,2)
  • 6.  A range can be: One cell (e.g., G2), or NAMING RANGES
  • 7.  A range can be: One cell (e.g., G2), or Multiple cells, which are next to each other (e.g., A2:E2) NAMING RANGES
  • 8.  What’s the benefit of naming ranges? If you move a named range:  The cell reference changes, but  The range name does not change  What’s that mean for you, the programmer? If you refer to a cell reference (e.g., G2), in your code, …and then move the contents of that cell …you have to go back and update your code If you refer to a named range (e.g., “PortfolioReturn”), in your code …and then move the contents of that cell (cut + paste) …you don’t have to update your code. NAMING RANGES
  • 9. A few rules… MUST start with a letter CANNOT contain any spaces CANNOT be the name of another range CANNOT be the name of the cell reference for another cell in Excel. NAMING RANGES “RET1” is already a cell in Excel so I can’t give another cell this name.
  • 10. 1. SAVE AS .xlsm 2. Save FREQUENTLY!! Excel crashes! 3. Saving your Excel workbook ALSO saves your VBA. 4. Use good files names so that you know which file is the newest version you’re working on. • Dissertation.xlsm • Disso.xlsm • myDisso.xlsm • DissoNew.xlsm • DissoNewest.xlsm SAVING YOUR WORK m for Macro Enabled • 13oct04Dissertation.xlsm • 2013oct04_Disso.xlsm • 04oct13_myDisso.xlsm
  • 12. GETTING FAMILIAR WITH THE VBE Alt + F11: Toggle between Excel and VBE, OR Go to the VBE from Excel Go back to Excel from the VBE
  • 13. GETTING FAMILIAR WITH THE VBE Excel File This is where you write your code
  • 14. GETTING FAMILIAR WITH THE VBE Code for data in Worksheets goes here Code in a specific sheet can only be executed for data and cells in that sheet. You CANNOT refer to cells in another worksheet or on a userform
  • 15. GETTING FAMILIAR WITH THE VBE Commands for the Workbook go here For example •Open the software when the workbook opens •Save the workbook when the software closes • Etc…
  • 16. GETTING FAMILIAR WITH THE VBE Code for Userforms go here Code which uses or refers to information obtained on the userform Code which executes when tools on the userform are used (e.g., command buttons)
  • 17. GETTING FAMILIAR WITH THE VBE Other code, not specific to any one userform or worksheet goes in a module For calculations, optimisation, or any code you want to be able to use at any time
  • 18.  Insert a Userform GETTING FAMILIAR WITH THE VBE
  • 19.  Insert a Userform  Insert a module GETTING FAMILIAR WITH THE VBE
  • 20.  Insert a Userform  Insert a module  Insert a procedure GETTING FAMILIAR WITH THE VBE We’ll come back to this later in the term
  • 21. INSERT A PROCEDURE You will use both Sub’s and Function’s during your FYP. We will discuss the difference later. The name should be descriptive of your Sub or Function. Cannot contain spaces. Click OK. This is what you should get: Write your code here. The Sub Procedure is now in the Module
  • 23. ACTIVATE A WORKBOOK Name of my workbook in “ ”. Tell VBA which workbook you want to work with
  • 24. ACTIVATE A WORKSHEET Tell VBA which worksheet you want to work with Name of my worksheet in “ ”.
  • 25. NAMING RANGES Use .Name Assign range names to… • One cell, or Must be in “ ” because it’s text!
  • 26. NAMING RANGES Use .Name Assign range names to… • One cell, or • A range of cells Must be in “ ” because it’s text!
  • 27. NAMING RANGES Assign range names to… • One cell, or • A range of cells Row 7, Column 1 (top left) Through to Row 9, Column 3 (btm right)
  • 28. ASSIGNING VALUES Enter a number into… • One cell, or • A range of cells InputBox(“ ”) Used to get input from the user
  • 29. ASSIGNING VALUES Enter text into… • One cell, or • A range of cells Text MUST be in “ ”
  • 30.  Check the range names by clicking on the cells  Did the code do what you thought it would? RUN THIS CODE (PRESS F5)
  • 31. USE EXCEL BUILT-IN FUNCTIONS FROM VBA Activate the correct worksheet
  • 32. USE EXCEL BUILT-IN FUNCTIONS FROM VBA Name the range where the numbers are in Excel
  • 33. USE EXCEL BUILT-IN FUNCTIONS FROM VBA Enter column headings
  • 34. USE EXCEL BUILT-IN FUNCTIONS FROM VBA Three ways to do the same thing Use what is best for the situation Enters the equation =Sum(numSeries) into Row 2, Col 3.
  • 35. USE EXCEL BUILT-IN FUNCTIONS FROM VBA Uses the built in Min function to find the minimum of the range named numSeries. Three ways to do the same thing Use what is best for the situation
  • 36. USE EXCEL BUILT-IN FUNCTIONS FROM VBA Uses the built in Max function to find the maximum of the range from Rows 1 to 7 in Col 1. Three ways to do the same thing Use what is best for the situation
  • 37.  In column 1 of your worksheet, create a list of 7 random numbers.  Insert a new Sub procedure called Exercise1.  Enter & run the following code: EXERCISE #1
  • 38.  From the Developer Tab: Choose Insert Under Form Controls, Select command Button  Draw a command button.  Select the macro you want to assign to this button. Only procedures in Modules will appear in the list. EXECUTING CODE USING FORM CONTROLS. Choose Exercise1
  • 39. Turn off Design Mode.  Make sure it is not highlighted  Your button now runs the code from the Sub Exercise1. Try changing your random numbers & re-running your code by using your button. EXECUTING CODE USING FORM CONTROLS.
  • 40. Create a Sub procedure called “Exercise2” Code the following in this sub: Activate your worksheet (use the sheet called ‘Example 3’) Assign a range name to the range of weights Assign a range name to the range of mean returns Assign a range name to cell G2, which will contain a formula for the portfolio return (next step) Assign equal weights into the weights range Enter the formula to calculate the portfolio return into G2 (use the SUMPRODUCT formula) Run your code (Put your cursor inside the sub & hit F5). Go back to Excel and make sure it’s correct. EXERCISE #2
  • 41. You are ready to move on when: LO1: You can name the address of a cell using Cells(r,c) or RC notation. LO2: In the VBE, you know the difference between adding code in a worksheet, a userform and a module. LO3: You can name ranges within Excel as well as name Excel ranges within VBA. LO4: You can assign numerical or text values to individual cells and ranges of cells from within VBA. LO5: You can describe the difference between the 3 ways of using Excel built-in functions within VBA. LEARNING OUTCOMES

Editor's Notes

  1. Show how to change excel to RC notation