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
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
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