Ma3696 Lecture 1

358 views

Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

Ma3696 Lecture 1

  1. 1. Excel and the VBELECTURE 1
  2. 2. Important things to know in Excel The VBE environment A first go at some coding! SUMMARY
  3. 3. IMPORTANT THINGS TO KNOW ABOUT EXCEL
  4. 4. CELL REFERENCES IN EXCEL Each cell has an ‘address’: “B5” Row 5, Column B. R5C2 Row 5, Column 2. R5 C2 B5
  5. 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. 6.  A range can be: One cell (e.g., G2), or NAMING RANGES
  7. 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. 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. 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. 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
  11. 11. Visual Basic Editor THE VBE
  12. 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. 13. GETTING FAMILIAR WITH THE VBE Excel File This is where you write your code
  14. 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. 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. 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. 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. 18.  Insert a Userform GETTING FAMILIAR WITH THE VBE
  19. 19.  Insert a Userform  Insert a module GETTING FAMILIAR WITH THE VBE
  20. 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. 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
  22. 22. EDITING EXCEL USING VBA
  23. 23. ACTIVATE A WORKBOOK Name of my workbook in “ ”. Tell VBA which workbook you want to work with
  24. 24. ACTIVATE A WORKSHEET Tell VBA which worksheet you want to work with Name of my worksheet in “ ”.
  25. 25. NAMING RANGES Use .Name Assign range names to… • One cell, or Must be in “ ” because it’s text!
  26. 26. NAMING RANGES Use .Name Assign range names to… • One cell, or • A range of cells Must be in “ ” because it’s text!
  27. 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. 28. ASSIGNING VALUES Enter a number into… • One cell, or • A range of cells InputBox(“ ”) Used to get input from the user
  29. 29. ASSIGNING VALUES Enter text into… • One cell, or • A range of cells Text MUST be in “ ”
  30. 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. 31. USE EXCEL BUILT-IN FUNCTIONS FROM VBA Activate the correct worksheet
  32. 32. USE EXCEL BUILT-IN FUNCTIONS FROM VBA Name the range where the numbers are in Excel
  33. 33. USE EXCEL BUILT-IN FUNCTIONS FROM VBA Enter column headings
  34. 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. 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. 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. 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. 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. 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. 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. 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
  42. 42. THE END

×