• Save
Ma3696 Lecture 1
Upcoming SlideShare
Loading in...5
×
 

Ma3696 Lecture 1

on

  • 275 views

 

Statistics

Views

Total Views
275
Views on SlideShare
275
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Show how to change excel to RC notation

Ma3696 Lecture 1 Ma3696 Lecture 1 Presentation Transcript

  • 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 View slide
  • CELL REFERENCES IN EXCEL Each cell has an ‘address’: “B5” Row 5, Column B. R5C2 Row 5, Column 2. R5 C2 B5 View slide
  • 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