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

Performing basic computation (Microsoft Excel)
Performing basic computation (Microsoft Excel)Performing basic computation (Microsoft Excel)
Performing basic computation (Microsoft Excel)Mirea Mizushima
 
Summarize Data Using a Formula
Summarize Data Using a FormulaSummarize Data Using a Formula
Summarize Data Using a Formulacoachhahn
 
Excel functions formulas
Excel functions formulasExcel functions formulas
Excel functions formulasLearnIT@UD
 
Fill series. Data validation. Excel Tutorial
Fill series. Data validation. Excel TutorialFill series. Data validation. Excel Tutorial
Fill series. Data validation. Excel TutorialIlgar Zarbaliyev
 
New Dynamic Array Functions. Excel Tutorial
New Dynamic Array Functions. Excel TutorialNew Dynamic Array Functions. Excel Tutorial
New Dynamic Array Functions. Excel TutorialIlgar Zarbaliyev
 
Lesson 5
Lesson 5Lesson 5
Lesson 5Kishan786
 
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 afzal1995786
 
Excel presentation
Excel presentationExcel presentation
Excel presentationDUSPviz
 
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 EngibaryanAshot 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 Worksheetdpd
 
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 InterfaceRonnel de Jesus
 
Excel Basics 2
Excel Basics 2Excel Basics 2
Excel Basics 2Leslie Noggle
 
Using Formulas in Excel
Using Formulas in ExcelUsing Formulas in Excel
Using Formulas in ExcelCasey Robertson
 
Basic Formulas - Excel 2013 Tutorial
Basic Formulas - Excel 2013 TutorialBasic Formulas - Excel 2013 Tutorial
Basic Formulas - Excel 2013 TutorialSpreadsheetTrainer
 
Functions and Charts in Microsoft Excel
Functions and Charts in Microsoft ExcelFunctions and Charts in Microsoft Excel
Functions and Charts in Microsoft ExcelNorth Bend Public Library
 

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 tonightteonamaud
 
The ultimate guide to cost management
The ultimate guide to cost managementThe ultimate guide to cost management
The ultimate guide to cost managementTim Kalbitzer
 
SPSHOU SharePoint 2013 Best Practices
SPSHOU SharePoint 2013 Best PracticesSPSHOU SharePoint 2013 Best Practices
SPSHOU SharePoint 2013 Best PracticesTheresa Lubelski
 
Forms, Getting Your Money's Worth
Forms, Getting Your Money's WorthForms, Getting Your Money's Worth
Forms, Getting Your Money's WorthAlex Gaynor
 
Cem 350 control systems 9 2016
Cem 350 control systems 9 2016Cem 350 control systems 9 2016
Cem 350 control systems 9 2016miresmaeil
 
Sketchnote Collection 1 - Sacha Chua
Sketchnote Collection 1 - Sacha ChuaSketchnote Collection 1 - Sacha Chua
Sketchnote Collection 1 - Sacha ChuaSacha Chua
 
Project Monitoring and Control
Project Monitoring and ControlProject Monitoring and Control
Project Monitoring and Controlguy_davis
 
Project Controlling and Project Monitoring
Project Controlling and Project MonitoringProject Controlling and Project Monitoring
Project Controlling and Project Monitoringharoldtaylor1113
 
Practical Sketchnoting
Practical SketchnotingPractical Sketchnoting
Practical SketchnotingJason Alderman
 
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 featuresGregory Zelfond
 
Hvac presentation for beginers
Hvac presentation for beginersHvac presentation for beginers
Hvac presentation for beginersguestf11b52
 
The Sketchnote Mini-Workshop
The Sketchnote Mini-WorkshopThe Sketchnote Mini-Workshop
The Sketchnote Mini-WorkshopMike Rohde
 

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

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/3WEZuYJDannySingh23
 
Excel useful tips
Excel useful tipsExcel useful tips
Excel useful tipsMohsin Azad
 
35 Useful Excel Tips
35 Useful Excel Tips35 Useful Excel Tips
35 Useful Excel TipsMukunda Adhikari
 
Excel useful tips
Excel useful tipsExcel useful tips
Excel useful tipsanmolbansal09
 
Advanced Spreadsheet Skills-1.pptx
Advanced Spreadsheet Skills-1.pptxAdvanced Spreadsheet Skills-1.pptx
Advanced Spreadsheet Skills-1.pptxCliffordBorromeo
 
Spreadsheets[1]
Spreadsheets[1]Spreadsheets[1]
Spreadsheets[1]BBAMUMU2014
 
Excel Formulas Functions
Excel Formulas FunctionsExcel Formulas Functions
Excel Formulas Functionssimply_coool
 
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 Indiaibinstitute0
 
Excel Formulas Functions 2007
Excel Formulas Functions 2007Excel Formulas Functions 2007
Excel Formulas Functions 2007simply_coool
 
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 Delhiibinstitute0
 

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

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

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
USPSÂŽ Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSÂŽ Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPSÂŽ Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSÂŽ Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 

Recently uploaded (20)

OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
USPSÂŽ Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSÂŽ Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPSÂŽ Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSÂŽ Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 

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