Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Using macros in microsoft excel part 1

990 views

Published on

Using macros in microsoft excel part 1

Published in: Education
  • Login to see the comments

Using macros in microsoft excel part 1

  1. 1. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.1 © NCC Education LimitedV1.0 Uses of Macros in Microsoft Excel • Macros can be used in Excel to automate tasks, such as: – Data entry and validation – Data selection – Interactive user forms – Formatting features and functions – Data output – Navigation
  2. 2. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.2 © NCC Education LimitedV1.0 Creating a Macro • Plan your macro - you should plan the steps that you want a macro to perform before you record or write it (remember the importance of planning was discussed in Topic 2, Slide 18). • A macro that is used often can be assigned to shortcut keys, buttons, pictures or icons, without having to open the Macro dialogue box. • A macro can be created by either recording it using the Macro recorder or writing VBA code in the VB Editor. Each development method will be used in this Topic and in Topic 9.
  3. 3. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.3 © NCC Education LimitedV1.0 Overview of Recording a Macro • The macro recorder is turned on. • A name for the macro is entered, it is assigned to a short cut key (optional), it is saved and a description is given to it (optional). • The recorder is started. • Steps for the macro are entered. • The recorder is stopped.
  4. 4. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.4 © NCC Education LimitedV1.0 Turning on the Macro Recorder 1. Select the Macro option. 2. The Record Macro dialogue box is displayed and here you can: - name your macro - assign your macro to short cut keys - save your macro - describe your macro
  5. 5. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.5 © NCC Education LimitedV1.0 Naming a Macro • The name must begin with a letter. • It must not contain spaces. • The underscore must be used to separate words.
  6. 6. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.6 © NCC Education LimitedV1.0 Saving a Macro • When a macro is created, it can be saved in one of the following places: - This Workbook (the macro will only be available in the spreadsheet in which it was created. - Personal Macro Book (the macro will be available to all spreadsheets) - New Workbook (the macro will only be available in that Spreadsheet which is newer than previous)
  7. 7. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.7 © NCC Education LimitedV1.0 Starting to Record a Macro 1. Once you have began to record your macro, perform each action that you want to include in the macro. 2. Note that the mouse can be used to click on commands and option, but the macro recorder cannot record mouse actions. To move the insertion point or select, copy, or move text, you must use keyboard commands. 3. To stop recording your macro at any click on the stop macro button on the bottom left corner of the screen.
  8. 8. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.8 © NCC Education LimitedV1.0 Running a Macro • If the macro has been assigned to a key combination, that key combination can be pressed to run the macro. • Or, select the macro from the macro list and then select Run.
  9. 9. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.9 © NCC Education LimitedV1.0 Formatting Titles and Formulas - 1 • How to prepare a spreadsheet each month with the same titles, formatting and formulas: – When you want to create a new spreadsheet, you usually open the previous month’s spreadsheet, delete the figures and save it as the current month’s spreadsheet. – If you create a template spreadsheet that contains a macro that automates the titles, formatting and formulas, you can open a new spreadsheet, run the macro and then all you will have to do is enter the current month’s figures.
  10. 10. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.10 © NCC Education LimitedV1.0 Formatting Titles and Formulas - 2 1. Open a new spreadsheet. 2. Open the macro dialogue box. 3. Enter MonthlyFinancialReport for the name of the macro. 4. Save it in the Personal Macro Workbook. 5. Enter a description. 6. Click OK.
  11. 11. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.11 © NCC Education LimitedV1.0 Formatting Titles and Formulas - 3 6. The following data is entered into the spreadsheet: Each heading is in bold. 7. In cell B5, the formula =SUM(A3-A4) is entered. 8. The contents of cell B5 are copied to cells C5 and D5.
  12. 12. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.12 © NCC Education LimitedV1.0 Formatting Titles and Formulas - 4 9. In cell F3 the formula =SUM(B3:E3) is entered. 10. In cell F4 the formula =SUM(B4:E4) is entered. 11. In cell F5 the formula =SUM(B5:E5) is entered. 12. The macro recorder is turned off. 13. Data is then entered into the worksheet to test that the formulas work. 14. The macro is tested by opening a new spreadsheet (or worksheet) and running the macro to display the template.
  13. 13. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.13 © NCC Education LimitedV1.0 Format a Table • How to format a table (formatting color, font and border styles): 1. Open a spreadsheet that either contains a table or create one. 2. Open the macro dialogue box and enter the required data. 3. Click OK. 4. Format the table. 5. Stop the recording the macro. 6. Test the macro.
  14. 14. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.14 © NCC Education LimitedV1.0 Dates and Times - 1 1. Click in the cell in which you want the date to appear. 2. Open the Record Macro dialogue box. 3. Enter the macro name, etc., Click OK 4. In the cell enter =TODAY() 5. Right click and choose format cells. 6. Select Month date year as the format, click OK. 7. Stop recording.
  15. 15. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.15 © NCC Education LimitedV1.0 Dates and Times - 2 • If you do not want the current date to be displayed on the spreadsheets, but want it to remain static: 1. Create the macro up until the step, ‘Select Month date year as the format, click OK’. 2. Select Copy > Paste > Paste Values (the value in the Formula bar changes from TODAY() to the actual date) 3. Press ESC. 4. Stop recording. 5. Test the macro.
  16. 16. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.16 © NCC Education LimitedV1.0 Data Entry - 1 • In Topic 6 you wrote macros in the VB Editor that displayed simple message boxes conveying information to a user, allowing them to click on OK but not enabling interaction. We will now create simple interactive macros. • The following is a macro that displays an input box which requests a user to enter their name; when a user enters their name, it is displayed on the spreadsheet. • VBA will be used to write the macro in the VB Editor.
  17. 17. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.17 © NCC Education LimitedV1.0 Data Entry - 2 Sub EnterName() Dim strName As String strName = InputBox("Please enter your name", "Enter Name") Range("A1").Select ActiveCell.FormulaR1C1 = strName End Sub
  18. 18. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.18 © NCC Education LimitedV1.0 Message Boxes and Feedback • The message box below provides feedback to a user depending on which button is selected. • The macro has been written so that if YES is selected, another message box is then displayed.
  19. 19. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.19 © NCC Education LimitedV1.0 Message Box Code • The code for the previous message box is as follows: Sub Message() x = MsgBox("Do you want to continue?", 3) If x = 7 Then Exit Sub MsgBox ("OK, we'll continue using this macro") End Sub
  20. 20. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.20 © NCC Education LimitedV1.0 Explanation of Message Box Code • These two lines of code need explaining: x = MsgBox("Do you want to continue?", 3) If x = 7 Then Exit Sub • The 3 refers to a display of optional buttons that can be displayed in a message box. There are five combinations of buttons that can be displayed, as can be seen on the next slide:
  21. 21. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.21 © NCC Education LimitedV1.0 Message Box Buttons Name Value Description vbOKCancel 1 Displays OK and Cancel buttons vbAbortRetryIgnore 2 Displays Abort, Retry and Ignore buttons vbYesNoCancel 3 Displays Yes, No and Cancel buttons vbYesNo 4 Displays Yes and No buttons vbRetryCancel 5 Displays Retry and Cancel buttons
  22. 22. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.22 © NCC Education LimitedV1.0 Further Explanation • The following line of code needs to be explained: If x = 7 Then Exit Sub • When the user clicks on a button, an action occurs depending on what value has been assigned to the button • x contains the value 7, which means that when the No button is clicked, the message box will no longer be displayed – this value can be seen in with other values on the following slide:
  23. 23. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.23 © NCC Education LimitedV1.0 Values Assigned to Buttons Name Value Description vbOK 1 Ok VbCancel 2 Cancel vbAbort 3 Abort vbRetry 4 Retry vbIgnore 5 Ignore vbYes 6 Yes vbNo 7 No
  24. 24. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.24 © NCC Education LimitedV1.0 Macro on Excel Create a Macro: With Excel VBA you can automate tasks in Excel by writing so called macros. MsgBox: The MsgBox is a dialog box in Excel VBA you can use to inform the users of your program. Range Object: The Range object, which is the representation of a cell (or cells) on your worksheet, is the most important object of Excel VBA.
  25. 25. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.25 © NCC Education LimitedV1.0 Variables:How to declare, initialize and display a variable in Excel VBA. If Then Statement: Use the If Then statement in Excel VBA to execute code lines if a specific condition is met. Loop: Looping is one of the most powerful programming techniques. A loop in Excel VBA enables you to loop through a range of cells with just a few codes lines.
  26. 26. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.26 © NCC Education LimitedV1.0 String Manipulation: In this chapter you find the most important functions to manipulate strings in Excel VBA Date and Time: Learn how to work with dates and times in Excel VBA. Events: Events are actions performed by users which trigger Excel VBA to execute code.
  27. 27. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.27 © NCC Education LimitedV1.0 Array: An array is a group of variables. In Excel VBA, you can refer to a specific variable (element) of an array by using the array name and the index number. Function and Sub: In Excel VBA, a function can return a value while a sub cannot. Userform: This chapter teaches you how to create an Excel VBA Userform
  28. 28. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.28 © NCC Education LimitedV1.0 Range The Range object, which is the representation of a cell (or cells) on your worksheet, is the most important object of Excel VBA Range("B3").Value = 2 Range(Cells(1, 1), Cells(4, 1)) = 5 Dim example As Range Set example = Range("A1:C4") example.Value = 8
  29. 29. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.29 © NCC Education LimitedV1.0 RANGE Dim example As Range Set example = Range("A1:C4") example.Value = 8 Dim example As Range Set example = Range("A1:C4") example.Select
  30. 30. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.30 © NCC Education LimitedV1.0 SELECT Dim example As Range Set example = Range("A1:C4") example.Columns(2).Select
  31. 31. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.31 © NCC Education LimitedV1.0 COPY AND PASTE Range("A1:A2").Select Selection.Copy Range("C3").Select ActiveSheet.Paste Dim example As Range Set example = Range("A1:C4") example.Select
  32. 32. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.32 © NCC Education LimitedV1.0 CLEAR Range("A1").ClearContents Range("A1").Value = ""
  33. 33. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.33 © NCC Education LimitedV1.0 COUNT Dim example As Range Set example = Range("A1:C4") MsgBox example.Count MsgBox example.Rows.Count Then??
  34. 34. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.34 © NCC Education LimitedV1.0 VARIABLES Variable Declaration Dim x As Integer Dim book As String Dim x As Double
  35. 35. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.35 © NCC Education LimitedV1.0 VARIABLES Dim continue As Boolean continue = True If continue = True Then MsgBox "Boolean variables are cool“
  36. 36. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.36 © NCC Education LimitedV1.0 If Then Statement Dim score As Integer, result As String score = Range("A1").Value If score >= 60 Then result = "pass" Range("B1").Value = result
  37. 37. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.37 © NCC Education LimitedV1.0 For Loop Dim i As Integer For i = 1 To 6 Cells(i, 1).Value = 100 Next I
  38. 38. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.38 © NCC Education LimitedV1.0 For Loop Dim i As Integer, j As Integer For i = 1 To 6 For j = 1 To 2 Cells(i, j).Value = 100 Next j Next i
  39. 39. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.39 © NCC Education LimitedV1.0 While Loop Dim i As Integer i = 1 While i < 3 Cells(i, 2).Value = 20 i = i + 1 Wend
  40. 40. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.40 © NCC Education LimitedV1.0 Do While Loop Dim i As Integer i = 1 Do While i < 6 Cells(i, 1).Value = 20 i = i + 1 Loop
  41. 41. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.41 © NCC Education LimitedV1.0 String Manipulation Join Strings Dim text1 As String, text2 As String text1 = "Hi" text2 = "Tim" MsgBox text1 & " " & text2
  42. 42. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.42 © NCC Education LimitedV1.0 Dim text As String text = "example text" MsgBox Left(text, 4) MsgBox Right(text, 4) MsgBox Len("example text") MsgBox Instr("example text", "am")
  43. 43. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.43 © NCC Education LimitedV1.0 Date and Time Dim exampleDate As Date exampleDate = DateValue("Jun 19, 2010") MsgBox Year(exampleDate) Dim firstDate As Date, secondDate As Date firstDate = DateValue("Jun 19, 2010") secondDate = DateAdd("d", 3, firstDate) MsgBox secondDate
  44. 44. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.44 © NCC Education LimitedV1.0 Date and Time
  45. 45. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.45 © NCC Education LimitedV1.0 Workbook Open Event
  46. 46. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.46 © NCC Education LimitedV1.0 One-dimensional Array Dim Films(1 To 5) As String Films(1) = "Lord of the Rings" Films(2) = "Speed" Films(3) = "Star Wars" Films(4) = "The Godfather" Films(5) = "Pulp Fiction" MsgBox Films(4)
  47. 47. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.47 © NCC Education LimitedV1.0 Two-dimensional Array Dim Films(1 To 5, 1 To 2) As String Dim i As Integer, j As Integer For i = 1 To 5 For j = 1 To 2 Films(i, j) = Cells(i, j).Value Next j Next i MsgBox Films(4, 2)
  48. 48. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.48 © NCC Education LimitedV1.0 Two-dimensional Array
  49. 49. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.49 © NCC Education LimitedV1.0 Function and Sub Procedure The difference between a function and a sub in Excel VBA is that a function can return a value while a sub cannot. Functions and subs become very useful as program size increases.
  50. 50. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.50 © NCC Education LimitedV1.0 Function Function Area(x As Double, y As Double) As Double Area = x * y End Function Dim z As Double z = Area(3, 5) + 2 MsgBox z
  51. 51. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.51 © NCC Education LimitedV1.0 Sub Procedure Sub Area(x As Double, y As Double) MsgBox x * y End Sub Area 3, 5
  52. 52. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.52 © NCC Education LimitedV1.0 User form Form Controls are objects which you can place onto an Excel Worksheet or User Forms, which give you the functionality to interact with your data. For example, drop-down boxes, list boxes, spinners, and scroll bars are useful for selecting items from a list Option Buttons and Check Boxes allow selection of various options. Buttons allow execution of VBA code.
  53. 53. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.53 © NCC Education LimitedV1.0 User form
  54. 54. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.54 © NCC Education LimitedV1.0 User form By adding form controls to user forms, we can tell Excel how the value entered in that should be treated. This is done a special type of macros called as Events. By adding a control to a worksheet and linking it to a cell, you can return a numeric value for the current position of the control. You can use that numeric value in conjunction with the Offset, Index or other worksheet functions to return values from lists.
  55. 55. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.55 © NCC Education LimitedV1.0 Current Position Dim emptyRow As Long 'Make Sheet1 active Sheet1.Activate 'Determine emptyRow emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
  56. 56. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.56 © NCC Education LimitedV1.0 Inserting on Current Position Sub insert_Date() Cells(emptyRow,1).value=tebox1.value Cells(emptyRow,2).value=tebox2.value Cells(emptyRow,3).value=combo1.value Cells(emptyRow,4).value=listbox1.value End sub
  57. 57. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.57 © NCC Education LimitedV1.0 Current Position
  58. 58. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.58 © NCC Education LimitedV1.0 Add the Controls To add the controls to the Userform, execute the following steps. 1. Open the Visual Basic Editor. 2. Click Insert, Userform. If the Toolbox does not appear automatically, click View, Toolbox. Your screen should be set up as below.
  59. 59. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.59 © NCC Education LimitedV1.0 Add the Controls
  60. 60. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.60 © NCC Education LimitedV1.0 Show the Userform Private Sub CommandButton1_Click() DinnerPlannerUserForm.Show End Sub
  61. 61. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.61 © NCC Education LimitedV1.0 References - 1 • Gonzalez, J., Meister, C., Ozgur, S., Dilworth, B., Troy, A. and Brandt, T. (2006). Office VBA Macros You Can Use Today: Over 100 Amazing Ways to Automate Word, Excel, PowerPoint, Outlook & Access, Holy Macroi Press. • Microsoft Office: Macros and Security. (2011). http://www.msoffice-tutorial.com/macro-security.php
  62. 62. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.62 © NCC Education LimitedV1.0 References - 2 • Visual Basic/Getting Started. (2010). [Available Online] http://en.wikibooks.org/wiki/Visual_Basic/Getting_Started • VBScript Glossary. (1997). [Available Online] http://tinyurl.com/69kxbe5
  63. 63. Using Macros in Microsoft Excel – Part 1 Topic 8 - 8.63 © NCC Education LimitedV1.0 Topic 8 – Using Macros in Microsoft Excel Part 1 Any Questions?

×