MICROSOFT EXCEL VISUAL BASIC FOR APPLICATIONS INTRODUCTION

Size: px
Start display at page:

Download "MICROSOFT EXCEL VISUAL BASIC FOR APPLICATIONS INTRODUCTION"

Transcription

1 MICROSOFT EXCEL VISUAL BASIC FOR APPLICATIONS INTRODUCTION

2 Welcome! Thank you for choosing WWP as your learning and development provider. We hope that your programme today will be a stimulating, informative and rewarding experience. Our highly skilled training consultants combine practical experience, dynamic teaching styles and tried and tested delivery methods, to ensure that you will enjoy learning new skills, which you can confidently apply in your working environment. It is our goal to inspire curiosity and develop the knowledge, skills and creativity of each individual to deliver improved performance in the workplace, and we hope that the benefits you will gain from your programme today will inspire you with enthusiasm and encourage you to keep coming back to WWP. To continue your learning and development with WWP, we have three key areas of specialism, each of which has its own comprehensive brochure Professional Development, IT Skills and Project, Programme and Risk Management. If you would like to receive a copy of any of our brochures, please contact us on or by on info@wwp.co.uk. Alternatively, you can visit our website to view our full range of learning and development programmes and services. We always appreciate client feedback so that we can continue to maintain and improve our quality standards. I would welcome any comments or suggestions you would like to make regarding the service you received from WWP via my personal at susan.foran@wwp.co.uk. Susan Foran Chief Executive WWP Training Limited WWP Training Limited Page 2

3 NOTE Unless otherwise stated, screenshots of dialog boxes and screens in this book were taken using Excel 2003 running on Window XP Professional. There may, therefore, be minor differences if you are using other versions of Excel, or if you are running on Windows 2000, Windows Vista or Windows 7. Concepts, discussions, procedures and functionality, however, remain unchanged. WWP Training Limited Page 3

4 Contents NOTE... 3 LESSON 1 - RECORDING MACROS... 6 WORKING WITH VISUAL BASIC FOR APPLICATIONS... 7 DEFINING MACROS... 7 RECORDING A MACRO... 7 RUNNING A MACRO RECORDING A MACRO USING RELATIVE REFERENCES SAVING A WORKBOOK CONTAINING A MACRO OPENING A WORKBOOK CONTAINING A MACRO SAVING THE PERSONAL MACRO WORKBOOK Exercise LESSON 2 THE VISUAL BASIC EDITOR LAUNCHING THE VISUAL BASIC EDITOR NAVIGATING THE VISUAL BASIC EDITOR FINDING A MACRO IN THE VISUAL BASIC EDITOR INSERTING A MODULE INTO A WORKBOOK COPYING A MODULE BETWEEN WORKBOOKS RENAMING A MODULE SHEET DELETING A MODULE SHEET Exercise LESSON 3 - ASSIGNING MACROS ASSIGNING A SHORTCUT KEY ADDING A CUSTOM TOOLBAR BUTTON (EXCEL ONLY) ADDING A MACRO BUTTON TO THE QUICK ACCESS TOOLBAR (EXCEL 2007 ONLY) CREATING A NEW TOOLBAR (EXCEL ONLY) ASSIGNING A MACRO TO A MENU (EXCEL ONLY) CREATING A NEW MENU (EXCEL ONLY) DELETING A CUSTOM TOOLBAR BUTTON OR CUSTOM MENU ITEM (EXCEL ONLY) USING A MACRO BUTTON CREATING A MACRO BUTTON COPYING A WORKSHEET MACRO BUTTON EDITING A WORKSHEET MACRO BUTTON DELETING A MACRO BUTTON Exercise LESSON 4 WRITING PROCEDURES INTRODUCTION TO EXCEL OBJECTS METHODS AND PROPERTIES RETURNING METHODS AND PROPERTIES ADDING ARGUMENTS TO METHODS PROPERTIES THAT ARE ALSO OBJECTS SELECTION OR ACTIVECELL? WRITING VBA SUB PROCEDURES THE WITH... END WITH BLOCK THE OFFSET PROPERTY DEALING WITH A RUNTIME ERROR USING THE OBJECT BROWSER Exercise LESSON 5 - CONTROL STRUCTURES THE IF-THEN CONTROL STRUCTURE THE IF-THEN-ELSE CONTROL STRUCTURE USING ELSEIF IN A CONTROL STRUCTURE THE SELECT CASE CONTROL STRUCTURE USING A DO LOOP STATEMENT USING A WHILE - WEND STATEMENT WWP Training Limited Page 4

5 Excel Level 5 - VBA Introduction Contents Exercise LESSON 6 DATA VARIABLES DATA VARIABLES CREATING VARIABLES AND ASSIGNING VALUES VARIABLE TYPES Exercise APPENDIX I - MACRO SECURITY SECURITY LEVELS CHANGING MACRO SECURITY SETTINGS TRUSTED LOCATIONS (EXCEL 2007) APPENDIX II SOLUTIONS TO EXERCISES INDEX WWP Training Limited Page 5

6 LESSON 1 - RECORDING MACROS In this lesson, you will: Record and run a macro Understand the difference between recording a macro in the Personal Macro Workbook or This Workbook Understand the difference between recording a macro with relative and absolute references WWP Training Limited Page 6

7 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros WORKING WITH VISUAL BASIC FOR APPLICATIONS Discussion With most Microsoft Office applications (Word, Access, PowerPoint, etc), you can use the computer programming language Visual Basic for Applications (VBA) to enhance, customise and automate the tasks that you perform in the application. VBA can also be used to create import and export filters for various file formats. The word visual in Visual Basic refers to the way in which you develop the user interface for application. Buttons, dialog boxes, menus and so forth are drawn on the screen. The word basic in Visual Basic Applications refers to the programming code you write; Visual Basic is a form of the programming language BASIC (Beginner s All-Purpose Symbolic Instruction Code). VBA should not be confused with VB (Visual Basic, which at the time of writing was at version 6.0). VB allows programmers not only to create simple graphical user interface (GUI) applications, but also more complex ones. For users who do not have VB loaded on their computers, the code must be compiled into separate executable files. Code created in VBA can be run as part of the Excel application, without a separate executable file. DEFINING MACROS Discussion A macro is a small program that you can use to perform frequently used simple operations automatically, or for more complex tasks that require consistency. It contains all the commands and actions necessary to complete a task. Excel macros are written in the VBA programming language. The series of commands used in a macro are known as a procedure. Each procedure is given a name that is used to identify the macro for later execution (running) and editing. Each procedure is saved in a device called a module. In Excel, this module is attached to the worksheet in which the macro was written. Since Office 97, a module cannot be seen in the normal Excel window because it is hidden. You must use the Visual Basic Editor to select a module and view, write or edit the macro code. In order for a macro to run, the Excel file containing the VBA code must be open. RECORDING A MACRO Discussion A macro can be created using the macro recorder. You perform the steps you want to include in the macro and Excel records them into a macro procedure. The steps you perform are translated into VBA, which uses macro statements to replace your steps. When you record a macro, Excel creates a module that is attached to a worksheet. A module can contain more than one macro. WWP Training Limited Page 7

8 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros At the start of the macro recording process, you enter the following information into the Record Macro dialog box: Macro name: Shortcut key: Store macro in: Type a name for the macro. The macro name can contain letter, numbers and underscores but MUST begin with a letter. The name cannot contain spaces or punctuation marks There is a limit of 64 characters to a macro name. Type a letter that you wish to use in conjunction with the CTRL key to run the macro later. For example, if you press x you can later run the macro using CTRL x. Tip: Most of the CTRL <something> keystrokes are already used by Excel for other commands (eg. CTRL o for File Open, CTRL c for Copy). To avoid the macro keystroke overriding the standard Excel command, combine the SHIFT key with the keystroke. For example, by entering SHIFT O into the Shortcut key: box, the macro will run using CTRL SHIFT O, thus avoiding normal Excel keystrokes. Select the location where you want the macro recorder to store the VBA code for the macro. The code can be stored in the Personal Macro Workbook, a New Workbook or This Workbook. The Personal Macro Workbook is a special Excel file that Excel creates for you the first time that you record a macro in it. Excel names this file personal.xls and stores it in a folder named XLSTART. The XLSTART folder forms part of your personal settings (profile) so should always be available at any computer that you log on to and which is connected to your corporate network. Excel manages personal.xls by automatically saving it when you exit Excel, and automatically opening it when you launch Excel. As a result, any macros in the Personal Macro Workbook should always available for you to use. The Personal Macro Workbook is the ideal place, therefore, to store general purpose macros that you may want to use on ANY Excel file. Recording a macro in This Workbook is more usual in cases where the macro is specific to that workbook only. For example, a macro that carries out an analysis explicit to a table that is found in that workbook only. Storing a macro in This Workbook also makes it more available and transportable in shared situations. For example, if you send the workbook as an attachment, the macro will also be available to the recipient. If you had stored the same macro in the personal macro workbook, it would be available to you only. Recording a macro in a New Workbook is the same as This Workbook; the difference being that Excel will create a new file to store the macro in rather than storing it in the active file that WWP Training Limited Page 8

9 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros Description: you are recording the macro from. Type a comment or note(s) about the macro in general. For example, instructions on how to run the macro, an explanation what the macro is designed to do, etc. The Record Macro dialog box (Excel ) The Record Macro dialog box (Excel 2007) Studying the Visual Basic code generated by the recorder is an excellent way of getting acquainted with the VBA language. Procedures (Excel ) 1. Click the Tools menu. 2. Point at Macro 3. Select Record New Macro Type a name for the macro (no spaces) in the Macro name: box. 5. Assign a keystroke in the Shortcut key: box, if desired. 6. Specify where to store the macro in the Store macro in: box. 7. Add additional information about the macro in the Description: box: 8. Click OK. The Stop Recording toolbar appears and the word Recording appears in the status bar at the bottom left of the Excel window. 9. Perform the actions that you wish the macro to repeat later. 10. Click the Stop button when finished. NB If you do not see the Stop Recording toolbar, you can stop the macro by clicking Tools - Macro - Stop Recording. WWP Training Limited Page 9

10 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros Excel only Commands for working with macros can be accessed more quickly by displaying the Visual Basic toolbar (View - Toolbars - Visual Basic) The Visual Basic Toolbar Procedures (Excel 2007) 1. Click the View tab on the Ribbon. 2. Click the Macros button in the Macro group. 3. Select Record Macro Type a name for the macro (no spaces) in the Macro name: box. 5. Assign a keystroke in the Shortcut key: box, if desired. 6. Specify where to store the macro in the Store macro in: box. 7. Add additional information about the macro in the Description: box: 8. Click OK. A Stop button appears in the status bar at the bottom left of the Excel window. 9. Perform the actions that you wish the macro to repeat later. 10. Click the Stop button in the status bar when finished, or a. click the View tab on the Ribbon. b. click the Macros button in the Macro group. c. select Stop Recording. RUNNING A MACRO Discussion Macros are saved in modules with the workbooks in which they were written. Before you can run a macro, the workbook with the module containing the macro must be opened. When you select a macro name in the Run Macro dialog box, Excel performs the commands listed under the macro name in the module in sequence. Macros in any open workbook can be run from any other open workbook. WWP Training Limited Page 10

11 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros The Macro dialog box (Excel ) The Macro dialog box (Excel 2007) Procedures (Excel ) 1. Open a workbook and enable the macros. 2. Click the Tools menu. 3. Point to the Macros command. 4. Select Macros... in the side menu. 5. Select the macro you want to run. 6. Click Run. Excel versions only You can also run macros by clicking the Run Macro button on the Visual Basic toolbar. Procedures (Excel 2007) 1. Open a workbook and enable the macros. 2. Click the View tab on the Ribbon. 3. Click the Macros button in the Macro group. 4. Select View Macros. 5. Select the macro you want to run. 6. Click Run. RECORDING A MACRO USING RELATIVE REFERENCES Discussion By default, Excel records macros using absolute references. This means that it records the absolute or exact locations of any cells that you may select when recording your macro. For example, if you enter the text Annual Sales Report in cell A4, this will WWP Training Limited Page 11

12 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros always be typed into cell A4 of a worksheet when you run the macro, irrespective of what cell was selected when you started. If you are using relative references, Excel records cell locations relative to the cell from which you start the macro. For example, if you enter the text Annual Sales Report into a cell when recording with relative referencing, you can reproduce this text in any cell of any worksheet by activating a cell and running the macro. Relative references give the macro more flexibility. Procedures (Excel ) 1. Click Tools. 2. Point at Macro. 3. Select Record New Macro... from the side menu. 4. Type a name for the macro. 5. Click OK. 6. Click the Relative Reference button on the Stop Recording toolbar. 7. Record the steps of the macro. 8. Click the Stop Recording button on the Stop Recording toolbar when finished. Procedures (Excel 2007) 1. Click the View tab on the Ribbon. 2. Click the Macros button in the Macro group. 3. Select Use Relative References from the menu. 4. Click the Macros button again. 5. Select Record Macro Type a name for the macro. 7. Record the steps of the macro. 8. Click the Stop button in the Status Bar when finished. After recording a macro using relative references, ensure that you re-set the recorder if you wish to record your next macro using absolute references. In Excel , this is achieved by clicking the Relative References button on the Stop Recording toolbar as soon WWP Training Limited Page 12

13 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros as the recording starts but before carrying out any steps. Relative References active (background shading) Relative References not active (no background shading) In Excel 2007, this is achieved by opening the View Ribbon, clicking the Macros button and ensuring Use Relative References is correctly set before recording the new macro. Relative References active (orange shading) Relative References not active (no orange shading) SAVING A WORKBOOK CONTAINING A MACRO Discussion When you record a macro in This Workbook, the VBA code is written in a module in the workbook. When you save the workbook, therefore, you also save the module sheet and any macro(s) therein. In Excel 2007, workbooks containing macros have to be saved as a macro-enabled workbook. If you attempt to save a workbook containing macros as a standard Excel workbook, you will not be able to, and a message will appear explaining how to save it correctly. Procedures (Excel ) 1. Record your macros in the workbook. 2. Click Save. Procedures (Excel 2007) 1. Record your macros in the workbook. 2. Click the Microsoft Office Button. WWP Training Limited Page 13

14 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros 3. Point at the Save As command. 4. Select Excel Macro-Enabled Workbook in the side menu. 5. Once the workbook has been saved in this way, it can later be updated using the standard Save command. OPENING A WORKBOOK CONTAINING A MACRO Discussion While many macros are simply recordings of your keystrokes and mouse actions, VBA developers can write macros that carry out many commands on your computer. As a result, hackers can introduce harmful code onto your computer by writing macros that release a virus when run. When opening an Excel file containing macros, therefore, Excel uses various criteria to ensure that any macros contained therein are safe. The level and thoroughness at which Excel does this is determined by the security settings in force on your computer. The criteria for this varies depending on whether the workbook is being opened from a trusted location and/or if it is digitally signed. The level of security that Excel provides can be controlled. If you re creating your own macros, or the macros that you run are from a reliable source within your own workplace, you ll probably want to lower the protective security settings. If, however, the macros you use are often received from unknown sources, you may wish to increase it. For further information about these setting and how to change then, see Appendix A Macro Security on page 96. In Excel you are warned about opening a workbook that contains macros by means of message boxes. Examples of these are shown below: WWP Training Limited Page 14

15 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros In Excel 2007, you are warned about opening a workbook that contains macros by means of a Notification in the Message Bar. Excel 2007 window showing a macro security notification Upon clicking the Options... button in the message bar, you are presents with a window offering choices on how to deal with the macro(s) contained in the workbook that you are trying to open. Examples are: WWP Training Limited Page 15

16 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros A digital signature is an electronic, encryption-based, secure stamp of authentication on a macro or document. This signature confirms that the macro or document originated from the signer and has not been altered. For further information about security settings and how to change them, see Appendix I on page 96. Procedures (Excel version ) 1. Click the File menu. 2. Select Open Navigate to the drive/folder where the workbook is stored. 4. Select the file. 5. Click Open. 6. Note and respond to any message box that appears. 7. If no message box appears, you can assume that: a. the workbook contains no macro(s); b. the workbook contains macros but they are digitally signed from a trusted source; c. the workbook is being opened from a trusted location. (In Excel , the only trusted location is the XLStart folder.) WWP Training Limited Page 16

17 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros Procedures (Excel 2007) 1. Click the Microsoft Office button. 2. Select Open. 3. Navigate to the drive/folder where the workbook is stored. 4. Select the file. 5. Click Open. 6. Note any macro security notification that may appear in the message bar. 7. Click the Options... button in the message bar. 8. Note and respond to any message window that appears. 9. If no notification appears in the message bar, you can assume that: a. the workbook contains no macro(s); b. the workbook contains macros but they are digitally signed from a trusted source; c. the workbook is being opened from a trusted location. In Excel 2007 there are several, default trusted locations. See page 99 for further information on how to use trusted locations. SAVING THE PERSONAL MACRO WORKBOOK Discussion The personal macro workbook is automatically created by Excel the first time you record a macro into it. It is then stored in the XLStart folder (a trusted location in all versions of Excel and part of your personal profile) and saved when you exit Excel. It is then automatically opened when you launch Excel; macros stored in it, therefore, ar e always available for use while Excel is running. Procedures (All versions) 1. Exit Excel. 2. If macros have been recorded or edited in the personal macro workbook, the following message box appears: 3. Click Yes to save the macros or No to discard them. WWP Training Limited Page 17

18 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros EXERCISE RECORDING MACROS 1. Open a blank workbook. 2. Record a macro with absolute references as follows: Name: MyDataEntry Keystroke: Ctrl-Shift-R Store Macro in: Personal Macro Workbook Description: Sets up rep names and column labels 3. The macro should first add a new sheet to the workbook and then starting in cell A1, enter the data and formatting shown below. Bold Italic 4. Run the MyDataEntry macro using the assigned keystroke. Note how the data is entered into the same cells as when recorded. 5. Save the file as Sales Record and close. 6. Open the file Tables. 7. Select cell A1 on the Central Region sheet. 8. Record a macro with relative references as follows: Name: FormatHeading Keystroke: Ctrl-Shift-H Store Macro in: This Workbook. Description: Formats any five cell table heading 9. Start the recorder. 10. Click the appropriate Relative References button. 11. Format the currently selected cell with a bold font, a blue font colour and a top and bottom border. 12. Select the cell to the right. 13. Repeat steps 11 and 12 for each cell until you get to the end of the row. WWP Training Limited Page 18

19 Excel Level 5 : VBA Introduction Lesson 1 Recording Macros 14. Stop the recorder. 15. Open the North Region sheet of the workbook. 16. Select cell B Run the FormatHeading macro. If recorded correctly, the macro will format the top five cells of the table. 18. Repeat on the South Region sheet by selecting cell A Save and close the file. WWP Training Limited Page 19

20 LESSON 2 THE VISUAL BASIC EDITOR In this lesson, you will learn how to: Launch the Visual Basic Editor Navigate the VB Editor screen Find a macro in the VB Editor Insert a module sheet into an Excel workbook Copy a module sheet between workbooks Rename a module sheet Delete a module sheet WWP Training Limited Page 20

21 Lesson 2 The Visual Basic Editor LAUNCHING THE VISUAL BASIC EDITOR Discussion Before you can edit a macro, you need to find it. Similarly, if you wish to write a new macro from scratch, you need to access or create a module in which to store the code. All this can be done in the Visual Basic Editor (VBE). The VBE is a program that sits alongside the Office family of applications and can be launched from within most Office programs. The VBE is not unique to Excel, you can use it in most Office programs to create VBA code in order to automate and customise the application. Procedures (Excel ) 1. Open the workbook. 2. Enable the macros, if necessary. 3. Click Tools. 4. Point at Macro. 5. Select Visual Basic Editor from the side menu. Procedures (Excel 2007) 1. Open the workbook. 2. Enable the macros, if necessary. 3. Check the Ribbon to see if there is a Developer tab. If there is, proceed to step 9. If there is not, continue with step Click the Microsoft Office Button. 5. Click the Excel Options button. 6. Select Popular in the pane at the left, if necessary. 7. Click the Show Developer tab in the Ribbon check box. 8. Click OK. 9. Click the Developer tab on the Ribbon. 10. Click the Visual Basic button in the Code group. The Visual Basic Editor can also be launched by using the shortcut keys: ALT + F11 (all versions), or by clicking the Visual Basic Editor button on the Visual Basic toolbar (Excel only). WWP Training Limited Page 21

22 Lesson 2 The Visual Basic Editor NAVIGATING THE VISUAL BASIC EDITOR Discussion The Visual Basic Editor consists of four main sections: ). Menus and Toolbar(s) provide methods for giving commands to the editor. The Project Explorer pane shows all open Excel workbooks files and Add-Ins. Primarily used for locating files and accessing locations where VBA code is stored. ). The Properties pane primarily used for adding additional characteristics and settings to objects on User Forms and/or for naming module sheets. ). The Code Pane primarily a simple word-processor used for writing and editing the VBA code on a selected module sheet). Menus and Toolbar Project Explorer Code Pane Properties Pane The Visual Basic Editor If the Project Explorer pane and/or the Properties pane are not visible, click the Project Explorer button and/or the Properties button toolbar. on the VB Editor FINDING A MACRO IN THE VISUAL BASIC EDITOR Discussion Before you can edit a macro, you need to navigate to where it is stored. The Project Explorer pane in the VBE can be used for this purpose. ). WWP Training Limited Page 22

23 Lesson 2 The Visual Basic Editor The Project Explorer pane shows at the top level (in bold) all the open Excel files. They are normally listed as: VBAProject (file name). Project Explorer pane showing top-level file names Any.XLA files in the Project Explorer pane are Add-Ins that have been installed to increase Excel functionality. They are normally password protected and cannot be viewed or edited. Examples of Add-Ins are: FUNCRES.XLA, SOLVER.XLA, and EUROTOOL.XLA. There may be many more, however, depending on your Excel setup. ). Once the file containing the macro(s) has been identified, you need to open its Modules folder, and finally open the specific module sheet containing the macro VBA code that you wish to edit. ). Procedures (All versions) Project Explorer showing the contents of a workbook s Modules folder 1. Launch the Visual Basic Editor. 2. Show the Project Explorer pane, if necessary. 3. Click the button at the left of the file (VBAProject) containing the macro(s). 4. Click the button at the left of the Modules folder. WWP Training Limited Page 23

24 Lesson 2 The Visual Basic Editor 5. Double-click the module sheet containing the code. By selecting a macro in the Run Macro dialog box (see page 10) and clicking the Edit button, the Visual Basic Editor is launched and takes you directly to the module sheet containing the VBA code for the selected macro. The exception to this is the Personal Macro Workbook that has to be unhidden first (Window > Unhide) INSERTING A MODULE INTO A WORKBOOK Discussion If writing a new macro into a workbook and there is no module sheet already in it to use, you need to insert one. ). There are no hard and fast rules about how many module sheets are used in a workbook. Macros can be written on an existing module sheet where previous ones have been recorded or written, or new module sheets can be added to keep different types of macro separate in the same way as normal worksheets in Excel are used to organise and arrange your work more clearly. Procedures (All versions) 1. Open the workbook. 2. Enable the macros, if necessary. 3. Launch the Visual Basic Editor. 4. Select in the Project Explorer pane the workbook (VBAProject) that you want to add the module sheet to. 5. Click the Insert menu. 6. Select Module. A module sheet can also be inserted into a workbook by right-clicking the workbook in the Project Explorer, pointing at Insert and selecting Module from the side menu. WWP Training Limited Page 24

25 Lesson 2 The Visual Basic Editor COPYING A MODULE BETWEEN WORKBOOKS Discussion There may be times when you need to transfer macros between workbooks. For example, there may be macros in a workbook that would also be useful in another workbook, or it may just be that you recorded or wrote a macro in the wrong module and need to move it. While it is possible to copy a macro from one module and paste it into another, it may be easier and more convenient to copy the whole module. This can be achieved in the VB Editor by exporting the module, and importing it into the other workbook. Procedures (All versions) 1. Launch the VB Editor. 2. In the Project Explorer, right-click the module that you wish to copy to another workbook. 3. Select Export File Type a file name for the module. 5. Select a location to store the file. 6. Click Save. WWP Training Limited Page 25

26 Lesson 2 The Visual Basic Editor A module sheet can also be copied to another workbook by clicking and dragging it from one workbook to another in the Project Explorer pane. RENAMING A MODULE SHEET Discussion A default name of Module[n] is given to all new module sheets whether they are added by the macro recorder or by the user (see page 24). It may be necessary, therefore, to rename module sheet(s) to clarify what macros they contain. Procedures (All versions) 1. Launch the VB Editor. 2. In the Project Explorer, select the module that you wish to rename. 3. Click into the Name box of the Properties pane. 4. Type a name for the module sheet. 5. Press Enter. WWP Training Limited Page 26

27 Lesson 2 The Visual Basic Editor DELETING A MODULE SHEET Discussion Module sheets can be removed from the workbook if no longer required. When removing a module sheet, you are given the option of saving it as a text file. This stores the code so that it can be imported back into the workbook, or into another one, at a later date. Procedures (All versions) 1. Launch the VB Editor. 2. In the Project Explorer, right-click the module that you wish to delete. 3. Select Remove Module[n] Click Yes to save the module as a text file or, click NO to discard. WWP Training Limited Page 27

28 Lesson 2 The Visual Basic Editor EXERCISE USING THE VB EDITOR 1. Close any open files. 2. Open the files, Data Records and Sales Analysis. Enable macros, if prompted. 3. Launch the VB Editor. 4. Insert a module sheet into the Data Records workbook. 5. Rename the module sheet to FormattingMacros (Tip: use the Name property). 6. Open Module1 in the Sales Analysis workbook. 7. Copy the FormatTable macro. 8. Paste it into the FormattingMacros module of Data Records. 9. Return to Excel. 10. Save and close Data Records and Sales Analysis. 11. Open the file, Special Macros. Enable the macros, if prompted. 12. Launch the VB Editor. 13. Export the GeneralMacros1 module sheet as a file named GeneralMacros.bas in the My Documents folder. 14. Import the GeneralMacros1.txt file to the Personal Macro Workbook (personal.xls). 15. Remove the GeneralMacros1 module from the Special Macros workbook. Do not export it. 16. Move the GeneralMacros2 module sheet from Special Macros to the Personal Macro Worksheet by clicking and dragging it. 17. Close the VB Editor. 18. Exit Excel and save the Personal Macro Worksheet. WWP Training Limited Page 28

29 LESSON 3 - ASSIGNING MACROS In this lesson, you will learn how to: Assign a macro to a keystroke Assign a macro to a custom toolbar button Create a new toolbar Assign a macro to a menu command Create a new menu Delete a custom toolbar button Delete a macro command from a menu Create a worksheet macro button Delete a worksheet macro button WWP Training Limited Page 29

30 Lesson 3 Assigning Macros ASSIGNING A SHORTCUT KEY Discussion You can assign a shortcut key to a macro. When a macro has a shortcut key, pressing the shortcut key combination runs the macro. All macro shortcut key combinations consist of [Ctrl] followed by a letter. Shortcut keys are case sensitive. Therefore the shortcut Key [Ctrl+e] is not the same as the shortcut Key [Ctrl+Shift+E]. Shortcut Keys can be assigned when you record a macro by entering a shortcut key in the Record Macro dialog box. You should avoid assigning shortcut Keys to macros that are the same as the standard Excel shortcuts (e.g., [Ctrl+B], [Ctrl+S]) because the standard Excel shortcuts will be overridden by the macro shortcuts. Procedures (Excel ) 1. Open the workbook containing the macros. 2. Enable the macros, if necessary. 3. Click the Tools menu. 4. Point to the Macro command. 5. Select Macros... in the side menu. 6. Select the name of the macro to which you want to assign the shortcut key. 7. Click Options. 8. Type a letter. 9. Select OK. 10. Select the Close button in the Macro dialog box. Procedures (Excel 2007) 1. Open the workbook containing the macros. 2. Enable the macros, if necessary. 3. Click the View tab on the Ribbon. 4. Click Macros in the Macro group. 5. Select View Macros. 6. Select the name of the macro to which you want to assign the shortcut key. WWP Training Limited Page 30

31 Lesson 3 Assigning Macros 7. Click Options. 8. Type a letter (preferably in conjunction with the SHIFT key). 9. Click OK. 10. Click the Close button in the Macro dialog box. The Macro Options dialog box (Excel 2003 shown but almost identical in all versions) ADDING A CUSTOM TOOLBAR BUTTON (EXCEL ONLY) Discussion In addition to the toolbar buttons that execute commands, Excel provides generic buttons that you can customise by attaching a macro. These buttons are listed in the Commands list box in the Customize... dialog box. Because custom toolbar buttons are saved with the Excel application and hence always available, the are best used to run general-purpose macros that you have stored in the personal macro workbook. Procedures 1. Open the workbook containing the macro(s). If the macros are in the personal macro workbook, it will already be open. 2. Click Tools. 3. Select the Customize... command. 4. Open the Commands tab. 5. Select Macro from the Categories list box. 6. Drag the Custom Button to a convenient location on one of your existing toolbars. 7. Right-click the custom button that you have added to your toolbar and type a descriptive name for it. This name will be used as the Help Tip when you later point at the button to use it. WWP Training Limited Page 31

32 Lesson 3 Assigning Macros 8. Select Change Button Image or Edit Button Image... to modify the appearance of the button, if necessary. 9. Right-click the custom button and select Assign Macro Select the macro to assign to the custom button. 11. Click OK. 12. Click Close in the Customize dialog box. The Customize dialog box with the Commands tab open ADDING A MACRO BUTTON TO THE QUICK ACCESS TOOLBAR (EXCEL 2007 ONLY) Discussion In addition to the Ribbon buttons that execute commands, Excel 2007 provides buttons for your macros that can be added to the Quick Access Toolbar. These buttons can be found in the Customize... pane of Excel Options. Custom buttons on the Quick Access Toolbar are saved with the Excel application and hence always available. They are best used to run general-purpose macros that you have stored in the personal macro workbook. Procedures 1. Open the workbook containing the macro(s). If the macros are in the personal macro workbook, it should already be open. 2. Click the Customize Quick Access Toolbar button 3. Select More Commands Select Macros in the Choose commands from: drop down list. WWP Training Limited Page 32

33 Lesson 3 Assigning Macros 5. Select the macro that you wish to assign a custom button to in the list below. 6. Click Add >>. 7. Select the macro name in the list at the right. 8. Click the Modify... button to change the button image, if desired. 9. Click OK. 10. Click OK. The Customize pane of the Excel Options dialog box showing list of macros (Excel 2007) CREATING A NEW TOOLBAR (EXCEL ONLY) Discussion As well as adding custom buttons to existing toolbars, you can create a new toolbar. If you have many macros to create custom buttons for, it makes sense to add them to a separate toolbar rather than fill the existing toolbars to overflowing. Procedures 1. Open the workbook containing the macro(s). If the macros are in the personal macro workbook, it will already be open. 2. Click Tools. 3. Select the Customize... command. 4. Open the Toolbars tab, if necessary. 5. Click the New... button. 6. Type a descriptive name for the new toolbar. 7. Click OK. A small, empty toolbar appears in the centre of the screen. WWP Training Limited Page 33

34 Lesson 3 Assigning Macros 8. Add custom buttons to the new toolbar as described on page 31. Custom buttons that you may have already added to existing toolbars can also be moved (click and drag) to the new toolbar. The Customize... window MUST be open in order to do this, even if you are not doing anything inside it. 9. Click Close in the Customize dialog box when finished. ASSIGNING A MACRO TO A MENU (EXCEL ONLY) Discussion You can assign a macro to a menu. You can choose in which menu you want the macro to appear. The macro appears as a menu command item to which you can give a specific name. Whenever the workbook containing the macro is opened, you can execute the macro, just as you would execute any standard menu command. It may be necessary to move the Customise dialog box in order to drag Custom Menu Item from the Commands list box to the desired location on the menu. Procedures 1. Open the workbook containing the macro(s). If the macros are in the personal macro workbook, it will already be open. 2. Enable the macros, if necessary. 3. Click Tools. 4. Select the Customize... command. 5. Select the Commands tab. 6. Select Macros from the Categories list box. 7. Drag Custom Menu Item from the Commands list box to the menu that you wish to add it to (eg. Tools); keep them mouse button held down and the menu will open. 8. Click and drag the custom menu item down into the menu to the desired location. 9. Right-click the custom menu that you have added to your menu. WWP Training Limited Page 34

35 Lesson 3 Assigning Macros 10. Type a descriptive name for it in the Name: box. This name will show up as the command when you later use the menu. Tip: Type an ampersand (&) immediately before the character that you want to underline and make the shortcut key for the item. 11. Right-click the custom menu item and select Assign Macro Select the macro to assign to the menu item. 13. Click OK. 14. Click Close in the Customize dialog box. CREATING A NEW MENU (EXCEL ONLY) Discussion As well as adding custom menu items to existing menus, you can create a new menu. If you have many macros to create menu items for, it makes sense to add them to a separate menu rather than fill the existing menus to overflowing. Procedures 1. Open the workbook containing the macro(s). If the macros are in the personal macro workbook, it will already be open. 2. Click Tools. 3. Select the Customize... command. 4. Open the Commands tab. 5. Select New Menu in the Categories list box. 6. Click and drag New Menu from the commands list box at the right to a convenient position on the menu bar (eg. between Window and Help). 7. Release the mouse button. 8. Right-click the new menu and type a descriptive name for the menu in the Name: box (eg. Macros). Tip: Type an ampersand (&) immediately before the character that you want to underline and make the shortcut key for the menu. WWP Training Limited Page 35

36 Lesson 3 Assigning Macros 9. Add custom menu items to the new toolbar as described on page 34. Custom menu items that you may have already added to existing menus can also be moved (click and drag) to the new menu. The Customize... window MUST be open in order to do this, even if you are not doing anything inside it. 10. Click Close in the Customize dialog box when finished. DELETING A CUSTOM TOOLBAR BUTTON OR CUSTOM MENU ITEM (EXCEL ONLY) Discussion Once you assign a macro to a custom toolbar button or a custom menu item, the macro is available to all worksheets. If you no longer want to run the macro in this way, you can delete the commands. Macro items can also be deleted from menus and toolbars by selecting opening the Customize... dialog box, and dragging the button or menu item off the toolbar or menu. Procedures 1. Click the Tools menu. 2. Select the Customize... command. 3. Right-click the toolbar button that you want to delete. 4. Select Delete. 5. Click the menu containing the macro you want to delete. 6. Right-click the menu item that you want to delete. 7. Select Delete. 8. Click Close in the Customize... window. USING A MACRO BUTTON Discussion You can create macro buttons in a worksheet that, when clicked, runs a particular macro. Macro buttons can simplify your work, automate repetitive tasks, or help an inexperienced user perform various worksheet tasks. WWP Training Limited Page 36

37 Lesson 3 Assigning Macros You can draw macro buttons anywhere on a worksheet. Like other graphic objects, macro buttons float above the worksheet. You can create as many macro buttons as desired to perform a number of different tasks. Procedures (All versions) 1. Open the workbook. 2. Enable the macros. 3. Click the macro button. CREATING A MACRO BUTTON Discussion You create a macro button on a worksheet using the Forms toolbar. You can also size the macro button and position it anywhere on the worksheet. After you draw the button, the Assign Macro dialog box opens in which you assign a macro to the button. When the button is clicked, the macro attached to the button runs. Creating a macro button (Excel ) Procedures (Excel ) 1. Open the workbook. 2. Enable the macros, if necessary. 3. Open the worksheet that you want to create the macro button on. 4. Click the View menu. 5. Point at Toolbars. 6. Select Forms from the side menu. 7. Click the Button button on the Forms toolbar. WWP Training Limited Page 37

38 Lesson 3 Assigning Macros 8. Click and drag to create a button in a blank part of the worksheet. 9. Release the mouse button. 10. Select the macro you want to assign to the button. 11. Click OK. 12. Click and drag to select the text on the button. 13. Release the mouse button. 14. Type the new text. 15. Deselect the button by clicking a cell on the worksheet. Procedures (Excel 2007) In order to create a worksheet button in Excel 2007, an extra tab has to be added to the Ribbon - the Developer tab. The Excel 2007 Developer tab 1. Open the workbook. 2. Enable the macros, if necessary. 3. Click the Office Button. 4. Click Excel Options. 5. Select the Show Developer tab in the Ribbon check box. 6. Click OK. 7. Click the Developer tab on the Ribbon. 8. Click the Insert button in the Controls group. 9. Click the Button (Form Control) button. 10. Click and drag to create a button in a blank part of the worksheet. 11. Release the mouse button. 12. Select the macro you want to assign to the button. 13. Click OK. WWP Training Limited Page 38

39 Lesson 3 Assigning Macros 14. Click and drag to select the text on the button. 15. Release the mouse button. 16. Type the new text. 17. Deselect the button by clicking a cell on the worksheet. COPYING A WORKSHEET MACRO BUTTON Discussion You can create macro buttons quickly by copying existing ones. This option is useful if you need several similar macro buttons on a worksheet. When you copy macro buttons, you can ensure that the buttons are the same size and shape. Copying a macro button (Excel 2003 shown but techniques identical in all versions) Procedures (All versions) 1. Open the workbook. 2. Enable the macros, if necessary. 3. Right-click the button you want to copy. 4. Select Copy. 5. Right-click a blank cell where you want to make the copy. 6. Select Paste. 7. Right-click the edge of the copied button to display the shortcut menu. 8. Select the Assign Macro... command. 9. Select the macro you want to assign to the button. 10. Click OK. WWP Training Limited Page 39

40 Lesson 3 Assigning Macros 11. Click and drag to select the text on the button. 12. Release the mouse button. 13. Type the new text. 14. Deselect the button by clicking a cell on the worksheet. EDITING A WORKSHEET MACRO BUTTON Discussion You can move, size or format a macro button the same way that you move, size or format other graphic objects on a worksheet. In order edit a macro button, you must first select it. Moving a macro button (Excel 2003 shown but techniques identical in all versions) Sizing a macro button (Excel 2003 shown but techniques identical in all versions) To select a macro button for moving or sizing, you hold the [Ctrl] Key, click the desired macro button, and release the [Ctrl] Key. If you do not select the button first (if selection handles do not appear around the button), the macro attached to the button runs when you click it. WWP Training Limited Page 40

41 Lesson 3 Assigning Macros Procedures (All versions) 1. Open the workbook. 2. Enable the macros. 3. To move the macro button, hold Ctrl and click the button to select it. 4. Release the Ctrl key. 5. Point at the shaded edge of the button that you want to move. 6. Click and drag the button to a new position on the worksheet. 7. Release the mouse button. 8. To size the macro button, hold Ctrl and click the button to select it. 9. Release the Ctrl key. 10. Point to one of he sizing handles on the button that you want to size. 11. Drag the handle to size the button. 12. Release the mouse button. 13. To format a macro button, right-click it to show its shortcut menu. 14. Select Format Control Make changes as necessary. 16. Click OK. 17. Click any cell on the worksheet to deselect. The Format Control dialog box (Excel ) The Format Control dialog box (Excel 2007) WWP Training Limited Page 41

42 Lesson 3 Assigning Macros DELETING A MACRO BUTTON Discussion You can delete any macro buttons you no longer need. This option prevents you from accidentally using macros that no longer work correctly. When you delete a macro button, only the button itself is deleted. The attached macro is not deleted. Procedures (all versions) 1. Open the workbook. 2. Enable the macros, if necessary. 3. Hold Ctrl and click the macro button to select it. 4. Release the Ctrl key. 5. Press the Delete key. WWP Training Limited Page 42

43 Lesson 3 Assigning Macros EXERCISE ASSIGNING MACROS 1. Open the file Run Macros. Enable the macros, if prompted. This workbook contains several macros that apply different number styles. 2. On the Qtr 1 sheet of the workbook, assign all the macros stored in the workbook to worksheet buttons (use the Forms toolbar). The picture below gives you a guide to what is required. 3. Select the range C4:F9 and run the SterlingStyle macro using the worksheet button. 4. Select the range C14:F19 and run the PlainStyle macro using the worksheet button. 5. On the same range, run the NumberStyle macro using the worksheet button. 6. Select the ranges C3:E3 and C13:E13 and run the DateStyle macro using the worksheet button. 7. Assign the macros in this workbook to keystrokes as follows: SterlingStyle: Ctrl Shift Y PlainStyle: Ctrl Shift L NumberStyle: Ctrl Shift N DateStyle: Ctrl Shift - T 8. Repeat the instruction from 3 to 6 above on the Qtr 2 sheet using the appropriate keystrokes. Ensure the macros run correctly. 9. Save and close the file. 10. Open the file, Run More Macros. 11. Excel users only: Create a new toolbar named My Macros. Excel 2007 users: Go to next step 12. Excel users: Add custom buttons to this toolbar to run the WrapAndCentre and ToggleGrid macros that are stored in the Personal Macro Workbook. Excel 2007 users: add custom buttons to the Quick Access Toolbar to run the WrapAndCentre and ToggleGrid macros that are stored in the Personal Macro WWP Training Limited Page 43

44 Lesson 3 Assigning Macros Workbook. 13. Run the ToggleGrid macro using the custom button. 14. Select the range A4:A10 and run the WrapAndCentre macro with the custom button. 15. Excel users: Delete the My Macros toolbar. Excel 2007 users: Remove the custom macro button from the Quick Access Toolbar. 16. Save and close the file. WWP Training Limited Page 44

45 LESSON 4 WRITING PROCEDURES In this lesson, you will learn how to: Understand the difference between Objects, Methods and Properties Write basic VBA statements in a procedure Deal with a Run-Time error Work with the Object Browser WWP Training Limited Page 45

46 Lesson 4 Writing Procedures INTRODUCTION TO EXCEL OBJECTS Discussion An object is something that can be controlled by VBA, for example a worksheet or a range of cells. An object is controlled by using methods and properties. Putting it simply, an object is an Excel thing that you want to work with. A method is an action that you want to perform on the object and a property is something about the object that you want to change. You can also get objects to tell you things about themselves by returning their properties and in certain cases, their methods. An analogy would be: Object = Dog Properties = Colour of fur; weight; height; length of tail! etc. Methods = Bark; walk; eat; beg! etc. Most VBA statement (lines of code) that carry out an action always start with the object that you want to do something to or with. This is then followed by a dot (full stop), and that is then followed by an appropriate method or property. Hence: Dog.Bark (make the dog bark) Dog.Eat (make the dog eat) Dog.ColourOfFur = Brown (make the colour of the dog s fur brown) The Excel Object Model is a hierarchy of objects that you can work with in Excel. In Excel 2003 there are approximately 200 objects, although about 60 of them are available only for backward compatibility with previous versions. Every new version of Excel introduces new ones, and in Excel 2007 there are approximately 250. A useful link to follow (current at May 2010) if you wish to learn more about the Excel Object Model is: It is not necessary to know all the Excel objects; very effective procedures can be written for most everyday purposes using only a few. Some of the more common, user-interface objects are: Object Application Workbooks Refers to The entire Microsoft Excel application. A collection of all the Workbooks that are currently open in the Excel application. WWP Training Limited Page 46

47 Lesson 4 Writing Procedures Worksheets Range A collection of all the Worksheets in the specified or active workbook. Worksheets are specifically sheets consisting of columns and rows and used for entering and working with data. Represents a cell, a row, a column, a selection of cells containing one or more contiguous blocks of cells, or a 3-D range. Worksheets refers specifically to a standard Excel worksheet, viz. one containing columns and rows. If the workbook that you are referring to contains chart sheets or special module and dialog sheets, the Sheets object should be used. Examples of using the above objects are: Application.Quit (exit Excel) Range( A1 ).Value = Excel VBA (write Excel VBA in cell A1) Range( A1:A10 ).Copy (copy onto the clipboard the contents of the range A1:A10) Discussion Collections You will notice that a couple of the objects in the above list are plural (viz. Workbooks and Worksheets). This is because they are collections and when used, apply whichever method that follows to ALL the open objects that they refer to, for example: Workbooks.Close (close ALL open workbooks) Worksheets.PrintOut (print ALL the worksheets in the workbook) Returning Objects from Collections You may have several workbooks open, but you want your macro to identify and carry out tasks on just one of them. You may have a workbook open with many worksheets in it, and you want your macro to carry out tasks on just a couple of them. In all these case, you have to refer to the collection (Workbooks or Worksheets) and then add in brackets, the name or index number of the specific one that you want the macro to refer to. If you are using the name of the object, it must be written in quotes ( ). If you are referring to it by its index number, the number does NOT go inside quotes. For example: Referring by name WorkBooks( Sales Analysis ).Activate WWP Training Limited Page 47

48 Lesson 4 Writing Procedures (activate the open workbook named Sales Analysis) WorkSheets( Sheet2 ).Activate (activate the worksheet in the current workbook named Sheet2) Referring by index number The index number of a workbook is automatically applied by Excel in the order that it is opened or created. The first workbook will always have an index number of 1, irrespective of its saved name. In Excel , the index number of a workbook can be identified by clicking the Window menu. Workbook index numbers (Excel ) In Excel 2007 the index number of a workbook can be identified by clicking the View tab on the Ribbon and clicking the Switch Windows button Workbook index numbers (Excel 2007) Eg: WorkBooks(1).Activate or WorkBooks.Item(1).Activate (activate the open workbook with an index number of 1) The index number of a worksheet is as it appears from left to right. The worksheet at the left of the workbook will always have an index number of 1, irrespective of its name. Eg: Worksheets(2).Activate or Worksheets.Item(2).Activate (activate the second worksheet from the left of the current workbook) WWP Training Limited Page 48

49 Lesson 4 Writing Procedures The object - Range - is also a collection (a collection of cells) although it is written in the singular. To return a single range object, you refer to it by its absolute reference, eg: Range( C10 ).Select (select the cell C10) You cannot refer to a cell by its index number using range. To refer to a cell by its index number, use Cells, eg: Cells(1).Select or Cells.Item(1).Select (select the cell A1) Cells(257).Select or Cells.Item(257).Select (select the cell A2 (Excel ) or IW1 in Excel 2007) Cells are indexed starting in the top left corner of the worksheet (A1) and going across the rows and down the columns, hence A1 = Cells(1); B1 = Cells(2), C1 = Cells(3)... etc. Cells can more easily be used by referencing their row number and column number, eg: Cells(1,1).Value = Excel VBA (enter Excel VBA in cell A1 -row 1 columm1 ) Cells(2,1).Font.Bold = true (make bold the font in cell A2 - row 2 column 1) METHODS AND PROPERTIES Discussion A method is an action that can be performed by the object. For example, a worksheet has an Insert method to insert rows or columns. A property is a characteristic of the object that can be set in a particular way. For example a Font has many properties, such as, size, colour, bold, italic, etc. a range of cells has a height property. There may be dozens of objects in Excel, but there are hundreds of methods and properties. The actions that they perform will also vary depending on the object that they are being used with. The Add method, for example, can be used in Excel 2007 with about 44 different objects! It is not necessary to know all the different methods and properties associated with each object in order to write useful and effective code. Listed below are some of the more popular methods and properties used in conjunction with the objects given on page 46. Object Application Methods(M) and Properties (P) Quit (M) ScreenUpdating Sample Code Application.Quit (exit Excel) Application.ScreenUpdating = False WWP Training Limited Page 49

50 Lesson 4 Writing Procedures Object Workbooks Methods(M) and Properties (P) (P) DisplayAlerts (P) Add (M) PrintOut (M) Save (M) Close (M) Sample Code (screen updating is turned off to speed up your macro code. You won't be able to see what the macro is doing, but it will run faster) Application.DisplayAlerts = False (disables Excel from displaying certain alerts and messages when a macro is running. If a response is required at any time, Microsoft Excel chooses the default response. The exception to this rule is Save As where the default is No but Excel picks Yes to overwrite the existing file) Workbooks.Add (adds a new workbook to the Excel application) Workbooks.PrintOut (prints all open workbooks completely) Workbooks( Sales Analysis ).PrintOut (prints the workbook named Sales Analysis) Workbooks( Sales Analysis ).Save (saves (updates) the workbook named Sales Analysis) Workbooks.Close (closes all the open workbooks) Workbooks( Sales Analysis ).Close (closes the workbook named Sales Analysis) Worksheets Range Add (M) Activate (M) Delete (M) Name (P) PrintOut (M) Select (M) Clear (M) ClearContents Worksheets.Add (adds a new worksheet to the left of the current worksheet in the current workbook) Worksheets(1).Activate (makes the first sheet at the left of the workbook, the active worksheet) Worksheets( London Data ).Delete (delete the sheet in the current workbook named London Data) Worksheets( Sheet1 ).Name = 2009 Sales (renames the sheet named Sheet1 to 2009 Sales) Worksheets( Summary ).PrintOut (print the sheet in the current workbook named Summary) Range( A1:C10 ).Select (select the range A1 to C10) Range( B20 ).Clear (clear cell B20 of both contents and formatting) Range( B20 ).ClearContents WWP Training Limited Page 50

51 Lesson 4 Writing Procedures Object Methods(M) and Properties (P) (M) Sample Code (clear cell B20 of just contents) ClearFormatting (M) Range( B20 ).ClearFormatting (clear cell B20 of just formatting) Value (P) Formula (P) FormulaR1C1 (P) Font (P) Interior PrintOut (M) Range( A1 ).Value = Summary Sheet (write the text Summary Sheet in cell A1) Range( A6 ).Formula = =sum(a2:a5) (enter the formula SUM(A2:A5) in cell A6) Range( D10 ).FormulaR1C1 = =R[-2]C R[-1]C (enter a formula in cell D10 that subtracts the cell one row up in the same column from the cell that is two cells up in the same column. This is equivalent to =D8-D9) The Font property cannot be used on its own and needs to be elaborated with additional properties. Eg. Range( A1 ).Font.Bold = True (makes the font in cell A1 Bold) Range( A1 ).Font.Italic = True (makes the font in cell A1 Italic) Range( A1 ).Font.Underline = True (makes the font in cell A1 underlined) Range( A1 ).Font.Size = 18 (makes the font in cell A1 18pts) Range( A1 ).Font.Name = Arial Black (makes the font in cell A1 Arial Black) Range( A1 ).Font.ColorIndex = 15 (makes the font in cell A1 mid grey) Range( A1 ).Font.Color = vbblue (makes the font in cell A1 blue) Range( A1:A8 ).Interior.Color = vbyellow (makes the fill colour in cells A1 to A8 yellow) Range( A1:D20 ).PrintOut (prints just the range A1 to D20 on the current sheet) WWP Training Limited Page 51

52 Lesson 4 Writing Procedures RETURNING METHODS AND PROPERTIES Discussion The previous section of this lesson dealt primarily with applying methods and setting properties to objects. There are cases, however, where a method or more commonly, a property, needs to feed back to the macro (return), information about an object. This is useful in decision-making control structures (see Lesson 5 Control Structures, on page 72), where the flow of the code needs to change based on conditions or tests found in the workbook. Returning is also useful for counting objects in collections so that code can be run an appropriate number of times on the appropriate number of items. The current value of a method or property is often saved as a variable (see Lesson 6 Data Variables on page 87). The examples below gives code for returning: a) whether the font in a cell is bold b) if the gridlines on a worksheet are turned on or off c) the number of sheets in a workbook, and d) the number of cells in a selected range. a) var1 = ActiveCell.Font.Bold The value of var1 will be either TRUE or FALSE based on whether the font in the active cell is bold. b) var2 = ActiveWindow.Gridlines The value of var2 will be either TRUE or FALSE based on whether the worksheet gridlines are turned on or off. c) var3 = ActiveWorkbook.Sheets.Count (can be shortened to Sheets.Count) The value of var3 will be however many sheets (of all types) there are in the active workbook. d) var4 = Range( A1 ).Value The value of var4 will the contents of cell A1 (ie. usually text, a number or empty). ADDING ARGUMENTS TO METHODS Discussion While many methods can be used without additional information (arguments), they will provide just a standard, default functionality. Adding additional arguments can WWP Training Limited Page 52

53 Lesson 4 Writing Procedures make a Method more powerful. Some methods MUST have additional arguments added, and others need no additional arguments added. Worksheets.Add will insert a default new worksheet to the left of the current one in the same way as if you use Insert > Worksheet in Excel , or use the Home tab, Insert button, Insert Sheet in Excel By adding additional arguments to the Add method, it can do more. The easiest way of finding out what additional arguments can be used with a method is to type a space after it. By typing Worksheets.Add followed by a space into a module, the following Auto Quick Info Tip appears. The additional parameters are as follows: Name Before After Count Type Description An object that specifies the sheet before which the new sheet is added. An object that specifies the sheet after which the new sheet is added. The number of sheets to be added. The default value is one. Specifies the sheet type. Can be one of the following: xlworksheet, xlchart, xlexcel4macrosheet, or xlexcel4intlmacrosheet. If you are inserting a sheet based on an existing template, specify the path to the template. The default value is xlworksheet. All the additional arguments in this case are optional. If, however, you wanted to add three new sheets before the first one at the left, the syntax would be as follows: Space After argument (2 nd ) (here left blank) Sheets.Add Sheets(1),, 3 Object Method Before argument (1 st ) Count argument (3 rd ) There must be a space before the word Add and the first argument. WWP Training Limited Page 53

54 Lesson 4 Writing Procedures The first argument describes the object before which you want to add the new sheet (Sheets(1)). The second argument is not required and left blank by typing commas with nothing between them. The third argument is the number of sheets to insert. The final argument, if not used, should be omitted. Do not add any further commas. An alternative syntax that makes method arguments easier to use and understand uses named arguments. In the above example, the syntax would be as follows: Sheets.Add Before:=Sheets( Sheet1 ), Count:= 3 A space still needs to be added after the word Add, but the additional arguments that you want to use are typed exactly as they appear in the Auto Quick Info Tip immediately followed by a colon and an equal symbol (:=) and then their value. This not only makes it clearer to read and understand, but by only entering the arguments that you need, keeps the statement more concise. Another example of a method with an optional argument is Copy (or Cut): Range( A1:A10 ).Copy Destination:= Sheets(2).Range( B1 ) That is certainly easier, quicker and more understandable than typing the following: Range( A1:A10 ).Copy Sheets(2).Activate Range( B1 ).Select Activesheet.Paste An example of a method that needs a mandatory argument is Open when it refers to the Workbooks object. Without the Filename argument, Excel would not know which file you wanted to open! Eg: Workbooks.Open Filename:= s:\shareddata\excel\sales.xls A full list of arguments for the Open method (Excel 2003) is: WWP Training Limited Page 54

55 Lesson 4 Writing Procedures PROPERTIES THAT ARE ALSO OBJECTS Discussion As briefly described on page 46, the Excel Object Model is a hierarchy of objects that you can manipulate in VBA. At the top of the hierarchy is the Application object, which has many methods and properties that it can manipulate. Some of its properties are also objects in their own right (think of them as sub-objects) and some of these have properties that are objects in their own right too (think of them as sub-sub-objects). For example, to enter a formula into cell B10, you could write: Application.ActiveWorkbook.ActiveSheet.Range( B10 ).Formula = =B9 * B8 Entering such a long statement would be very time consuming and make for unwieldy, unnecessarily complicated code that could be difficult to understand and prone to errors. It is, therefore, usually abbreviated to the object at the lowest level, viz. Range. Hence, a more concise statement for carrying out the same action would be: Range( B10 ).Formula = =B9 * B8 This assumes that you want to enter the formula in cell B10 of the sheet that the macro is being run from (the active sheet). If, however, you wish to enter the formula into cell B10 of Sheet2 and you are running the macro from Sheet1, then you have to go up the hierarchy to make the statement more specific, eg: Sheets( Sheet2 ).Range( B10 ).Formula = =B9 * B8 This assumes that you want to enter the formula in cell B10 of the workbook that the macro is being run from (the active workbook). If, however, you wish to enter the formula into cell B10 of Sheet2 of another open work book named, Stock Report, then you have to go up the hierarchy further to make the statement even more specific, eg Workbooks( Stock Report.xls ).Sheets( Sheet2 ).Range( B10 ).Formula = =B9 * B8 Think of the Excel Object Model as the organisation chart of your own company or organisation. At the top of the tree is the Managing Director, CEO or Chairperson, etc. If you are very senior, you may work directly for that person (ie. you can be directly controlled by that person) but in other cases, you will be further down the hierarchy controlled by a manger at a lower level. WWP Training Limited Page 55

56 Lesson 4 Writing Procedures If someone asks you who you work for, you could say that you work for the MD, CEO, or Chair (because ultimately, you do!), but you would normally quote your immediate group or departmental manager. Objects are, therefore, like levels of management, always refer to the one at the lowest possible level. Think of it as always using the lowest common denominator. Examples of common properties that can also be referred to as objects are: Property(Object) Refers to ActiveWorkbook ActiveSheet ActiveCell The workbook from which the macro is being run. The sheet from which the macro is being run The cell that is currently selected Examples ActiveWorkbook.Save ActiveWorkBook.PrintOut ActiveWorkbook.Close variable = ActiveWorkbook.FullName ActiveSheet.PrintOut ActiveSheet.Delete ActiveSheet.PageSetup.Orientation = xllandscape variable = ActiveSheet.Item ActiveCell.HorizontalAlignment = xlcenter ActiveCell.RowHeight = 20 ActiveCell.EntireColumn.Autofit variable = ActiveCell.Address SELECTION OR ACTIVECELL? Discussion The macro recorder is very fond of using Selection when ActiveCell would be more appropriate, for example: Range("D5").Select Selection.Font.Bold = True In the above case, using Selection has no detrimental effect but it is important to understand the difference between ActiveCell and Selection. ActiveCell refers to one specific cell that has been selected or activated. Selection is more appropriate when several cells have been selected. For example: Range("D5").Select ActiveCell.Font.Bold = True WWP Training Limited Page 56

57 Lesson 4 Writing Procedures but Range("D5:E10").Select Selection.Font.Bold = True If the above piece of code had stated: Range("D5:E10").Select ActiveCell.Font.Bold = True... only the cell D5 (the one in the top left of the selected area) would be made bold. Selection can refer to many groups (classes) of object, not just cells (range). In the code below, all the charts on a sheet are selected and then deleted. ActiveSheet.ChartObjects.Select Selection.Delete The following example counts the number of drawn objects (shapes) on the sheet, and stores them in a variable named counter. ActiveSheet.Shapes.SelectAll counter = Selection.Count The following example selects all the cells on the sheet and removes all the formatting, leaving just the plain cell contents. Cells.Select Selection.ClearFormats WRITING VBA SUB PROCEDURES Discussion Once you have an idea about the basic structure of VBA (concepts of objects, methods and properties) and you learned and become familiar with some commonly used VBA words (by using the macro recorder, looking at examples or reading this manual), you can start to put it all together in a sub procedure of your own. WWP Training Limited Page 57

58 Lesson 4 Writing Procedures The definition of a sub procedures is a set of instructions that can be performed as a unit. (Those of you who remember DOS, can think of it as a Batch-file, ie. a list of commands to be executed after one another). Creating a sub procedure is like planning a project. You have an objective and you list the necessary sequential steps to achieve it. Once you have identified the file to store the macro in (see page 22) and inserted or found a suitable module sheet onto which to write the code (see page 24), you can start. a) Sub End Sub The first word of a sub procedure is Sub. Sub stands for sub procedure and is followed by a name. The name must contain no spaces and punctuation marks are also best avoided, although the underscore may be used to represent spaces. The name should be reasonably descriptive of the purpose of the sub procedure, eg:. Sub SetUpNewReport or Sub Set_up_new_report Capitalisation is unimportant, but if the sub procedure name consists of more than one word (as in the first example above), capitalising the first letter of each word makes it clearer to read and understand what the macro does. Upon pressing enter, the VB Editor will add brackets at the end of the Sub statement, and a new statement a couple of lines below - End Sub. Eg: Sub SetUpNewReport () End Sub The brackets are used for passing arguments to a sub procedure from a called sub procedure, a topic that is outside the scope in this course. In most cases the brackets are left empty. End Sub indicates where the sub procedure ends and must, obviously, be kept as the final line of the sub procedure. The VB Editor formats the words, Sub and End Sub with a blue font. This is to highlight them as Key Words - main instructions of the VBA language - rather than an object, method or property. b) Statements In between Sub and End Sub, come statements. Statements are the sentences that make up the bulk of each sub procedure. Most (but not all) statements carry out an action on an object by applying methods to it or by altering its properties. WWP Training Limited Page 58

59 Lesson 4 Writing Procedures The number of statements (and other code) that can be written between Sub and End Sub are too numerous to give here, but examples and principles given earlier in this book should point you in the right direction for writing simple but effective and useful sub procedures. For example, the following add a new sheet to a workbook, moves it to the left of the workbook and then enters and formats a title in cell A1: Sub SetUpNewReport () Sheets.Add Before:= Sheets(1) Range( A1 ).Value = Stock Report Range( A1 ).Font.Bold = True Range( A1 ).Font.Size = 20 Range( A1 ).Font.Name = Arial Black Range( A1 ).Font.Color = vbblue End Sub The following selects all the cells on the active sheet (the one that the macro is being run from) and alters the column widths, row heights and font: Sub FormatAllSheet() Cells.Select Selection.ColumnWidth = 20 Selection.RowHeight = 15 Selection.Font.Name = Calibri Selection.Font.Size = 11 Range( A1 ).Select End Sub c) Capitalisation VBA words always have the first character capitalised. Some VBA words are made up of two or more words (eg. ActiveWorkbook; ActiveCell; ColumnWidth; CommandBarButton) and in these cases, the first character of the second or subsequent word(s) is also capitalised. In practice, it is not necessary to capitalise your VBA words as you type. In fact, it is recommended that you always write in lower case because when you press Enter at the end of a statement or you move off the line, the VB Editor will capitalise for you. This is a good way of ensuring that the words you have written are valid, VBA words. Any word(s) not capitalised are probably typos or variables (see page 88). This does not guarantee, however, that your VBA words will have the desired effect in the context that you have used them! WWP Training Limited Page 59

60 Lesson 4 Writing Procedures d) White Space and Indentations Some developers believe that although formatting code might make it look pretty, it is not really worth the time and effort. Properly formatted code, however, has nothing to do with appearance and everything to do with how easy your code is to understand and maintain. Formatting also helps detail the code by showing the logic and flow of a procedure and by grouping logically related sections of code. Use blank lines to separate logically related blocks of code, introductory (header) comments from the first variable declaration, and the last declared variable from the code itself. Indent code and comments within a procedure by using a two- to four-space tab stop. (The Visual Basic Editor uses a four-space tab stop by default.) As with white space, indents are used to organize code logically and make it visually appealing. Concern is often expressed over whether one should add spacing between statements or indent them. To put it simply, you don t have to add any spacing or indentations at all. They have no effect on the way that the code is executed and are added purely for neatness and clarity. Indenting statements will be elaborated further in some subsequent lessons and topics. e) Comments It is always a good idea to add notes to a sub procedure, not only to describe and/or remind yourself what is happening or why, but also for the benefit of other people who may have to edit your code in the future. Comments are added simply by preceding them with an apostrophe ( ). A comment can be added on a line of its own, eg: The following code formats the range with a currency style Range( B16:F16 ).Style = Currency Or, it can be added at the end of a statement, eg: Range( B16 ).Formula = =SUM(B3:B15) Adds up the column B figures The VB Editor formats comments in green to make them stand out more clearly. Precede all comments with a blank line f) Line Breaks Some statements can be very long, especially where many arguments are being used with a method, or your code is entering a long text string into a cell. This results in part of the code extending off the right edge of the screen and making it difficult to read without scrolling. Unlike normal word-processing where a line break can normally be created by pressing Enter, it is not so straightforward in the VB Editor. Before you press Enter to break a statement, you must type a line-continuation character an underscore (_) preceded by a space. WWP Training Limited Page 60

61 Lesson 4 Writing Procedures The line-continuation character cannot be added just anywhere, it has to be added where there is a natural gap in the statement, ie. not in between a single word (eg. Range, xldescending, False) or inside special characters (eg. :=) Consider the following long statement that sorts a table of data (range A1:F12) by column A in a descending order. Range("A1:F12").Sort Key1:=Columns("A"), Order1:=xlDescending, Header:=xlGuess, MatchCase:=False Line breaks could be added as follows: Range("A1:F12").Sort Key1:=Columns("A"), Order1:=xlDescending, _ Header:=xlGuess, MatchCase:=False or: Range("A1:F12").Sort Key1:=Columns("A"), _ Order1:=xlDescending, Header:=xlGuess, _ MatchCase:=False But NOT: Range("A1:F12").Sort Key1:=Colu _ mns("a"), Order1:=xlDescending, Header:=xl _ Guess, MatchCase:=False In fact, if you attempted to write the above example, you would receive the following message. In the case of long text strings, line breaks need to have added an ampersand concatenation character (&) placed at the beginning of the continuation line as follows:. Range( B2 ).Value = Fourscore and seven years ago our fathers " _ & "brought forth, on this continent, a new nation, " _ WWP Training Limited Page 61

62 Lesson 4 Writing Procedures & "conceived in liberty, and dedicated to the " _ & "proposition that all men are created equal." THE WITH... END WITH BLOCK Discussion There are several types of statement in VBA that come in blocks. Block statements are characterised by having sub-statements written within them that the block carries out special actions on. Examples of statements that come in blocks are the If statement and loops. These will be covered in more detail later in this book (see pages 88, 81 & Error! Bookmark not defined.). A useful general-purpose block is the With... End With block. This allows you to perform a series of statements on a specified object (and/or associated property) without having to retype it repeatedly. Not can this improve performance, but also reduces the risk of typos. Think of a With block a bit like using ditto in normal written communications. Consider the following code that enters and formats a title into cell A1: Sub AddReportTitle () Range( A1 ).Value = Stock Report Range( A1 ).Font.Bold = True Range( A1 ).Font.Size = 20 Range( A1 ).Font.Name = Arial Black Range( A1 ).Font.Color = vbblue End Sub Note how by using a With... End With block, repetitive typing of Range( A1 ) is avoided:. Sub SetUpNewReport () With Range( A1 ).Value = Stock Report.Font.Bold = True.Font.Size = 20.Font.Name = Arial Black.Font.Color = vbblue End With WWP Training Limited Page 62

63 Lesson 4 Writing Procedures End Sub By using a nested With... End With block inside the first one, repetitive typing of.font can also be avoided: Sub SetUpNewReport () With Range( A1 ).Value = Stock Report With.Font.Bold = True.Size = 20.Name = Arial Black.Color = vbblue End With End With End Sub THE OFFSET PROPERTY Discussion VBA offers several different ways to refer to ranges: Referring to ranges directly (eg. Range( A1 ), Range( B10:F10 ), etc.) Referring to ranges by using the Cells method (eg. Cells(1), Cells (2, 2), etc.) Referring to ranges by using the Offset property The first two methods have already been described in previous lessons (see page 49). The best way of describing using the Offset property is to think of it as a way of referring to a cell that is away from another cell by a certain number of rows and columns. Although Offset is a property and properties are normally equal to something, Offset takes an argument in brackets as follows: Offset(rowoffset, columnoffset) RowOffset is the number of rows away from the object that you want to refer. Positive values are offset downward, and negative values are offset upward. ColumnOffset is the number of columns away from the object that you want to refer. Positive values are offset to the right, and negative values are offset to the left. WWP Training Limited Page 63

64 Lesson 4 Writing Procedures Offset(0, -1) Same row, left one column Offset(-1, 0) Up one row, same column Cell being referred to Offset(1, 0) Down one row, same column Offset(0, 1) Same row, right one column The following example puts a value of 100 to the cell that is one row below cell C2 and two cells to the right of C2 (ie. cell E3):. Range("C2").Offset(1, 2).Value = 100 An advantage of the Offset function in VBA is that you can do things to a cell away from the one that is currently active without having to select it. Not only does this create more concise code, but it can significantly speed up its execution. The following code enters random numbers between 1 and 50 into the active cell and all the cells surrounding it without actually selecting them. WWP Training Limited Page 64

65 Lesson 4 Writing Procedures Sub EnterRandomNumbers With ActiveCell.Value = Rnd * 50.Offset(-1, 0).Value = Rnd * 50.Offset(0, 1).Value = Rnd * 50.Offset(1, 0).Value = Rnd * 50.Offset(0, -1).Value = Rnd * 50 End With End Sub DEALING WITH A RUNTIME ERROR Discussion In many cases, the VB Editor is able to alert you to certain errors in your statements as you write them. When you press enter, the VB Editor checks your code and if there is something it s not happy with, it will show a Compile error message and highlight the faulty code in red. Some of these are shown below and they can often be rather vague as to their meaning. Some errors, however, cannot be identified until you actually run the sub procedure. These usually (but not always) show up as Run-time errors and are characterised by the following message window: A Run-time error message The window is essentially telling you that the sub procedure had to stop at a certain point, because it encountered an unexpected condition. The Run-time error number WWP Training Limited Page 65

66 Lesson 4 Writing Procedures can, under certain circumstances, help you identify the problem and is an essential tool is writing error trapping code into your sub procedure. This, however, is outside the scope of this particular course. In some cases, the Run-time error occurs at the very start of the sub procedure and no statements are executed. In other cases where the Run-time error has occurred in the middle or near the end of the sub procedure, some actions will have been carried out before it came to a stop without completing through got the end. You have several options on how to deal with a Run-time error message: a) Click Help to read about the problem b) Click End to terminate the sub procedure c) Click Debug to take you to the VB Editor and to the statement where the error occurred. The statement will be highlighted in yellow (called Step Mode). If you understand what the error is and can fix it, you should then click the Continue button on the toolbar of the VB Editor in order for the sub procedure to attempt the statement again and, hopefully, complete through to the end. If you are unable to deal with the error, you should click the Reset button on the toolbar of the VB Editor in order to exit the sub procedure. You should then consult the person who wrote the macro and seek their advice and co-operation is resolving the problem. In the case of a run-time error, you must click either the Continue of the Reset button. If you do not you will be unable to run any other macros. Clicking the End button in the Run-time error window is equivalent to resetting the macro. VB Editor showing Step Mode following a Run-time error WWP Training Limited Page 66

67 Lesson 4 Writing Procedures Procedures 1. Click the Debug button in the Run-time error window. 2. Edit the highlighted code, if possible. 3. Click the Continue button if you believe that the error has been successfully edited. Click the Reset button if the error has not been successfully edited. 4. Close the VB Editor or switch back to the Excel window. USING THE OBJECT BROWSER Discussion The VB Editor provides you with a tool for identifying Excel objects and seeing which methods and properties can be associated with them. This is called the Object Browser and can be displayed over the code window; it makes a useful learning tool and point of reference for both VBA novices and seasoned developers. Procedures 1. Launch the VB Editor. The VB Editor with the Object Browser displayed 2. Click the Object Browser button on the toolbar. WWP Training Limited Page 67

68 Lesson 4 Writing Procedures 3. Click the drop down list in the top left corner of the Object Browser Select Excel. 6. View the left-hand pane to identify Excel Objects (Classes) 7. Right-click an object and select Help from the menu to learn more about it. 8. Select an object in the left-hand pane. 9. View its associated methods and properties in the right-hand pane. 10. Right-click a method or property and select Help to learn more about it. 11. Double click a module or Excel object in the Project Explorer pane to close the Object Browser. WWP Training Limited Page 68

69 Lesson 4 Writing Procedures EXERCISE WRITING SUB PROCEDURES 1. Launch the VB Editor. 2. Insert a module into the Personal Macro Workbook (personal.xls). 3. Create a new sub procedure named, OpenAndFormat. 4. Enter into the sub procedure a comment stating: This macro formats table headings. 5. The next statement must select cell A1 on the active sheet. 6. The next group of statements must format the active cell with: A font size of 18 A font name of Arial Black A font colour of Blue 7. The next statement must change the column width of the active cell to The next statement must select cell A3. WWP Training Limited Page 69

70 Lesson 4 Writing Procedures 9. The next statement must insert an entire row where the active cell is. 10. The next statement must select the range A4 to H The next statement must format the selected range with: A bold font A cyan interior colour 12. The final statement of the sub procedure must select cell A1 13. The completed sub procedure should be: WWP Training Limited Page 70

71 Lesson 4 Writing Procedures 14. The sub procedure should produce the following result: 15. Save the Regional Sales workbook then exit Excel and save the Personal Macro Workbook, if prompted. WWP Training Limited Page 71

72 LESSON 5 - CONTROL STRUCTURES In this lesson, you will learn how to: Create decision-making code using the If control structure Create decision-making code using the Select Case control structure Repeat code using Do loops WWP Training Limited Page 72

73 Lesson 5 Control Structures THE IF-THEN CONTROL STRUCTURE Discussion The If-Then control structure enables you to carry out a test and change the actions that a macro performs, based on whether the test is true or is false. The test in an If-Then control structure usually contains at least one conditional statement. Conditional statements involve the use of comparison operators. A list of these is given below: Symbol Operator = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to Examples of simple If-Then control structures are as follows: 1. Single Line form. If <test> Then <action if true> If the test is false, nothing specific will happen and any following code is NOT dependant on the test. For example: If Range( A3 ).Value >= 100 Then Range( A3 ).Font.Bold = True In the single line form, multiple actions can be performed but the code becomes long and complex to read. A colon (:) must be used to separate the actions, the syntax is as follows: If <test> Then <action1 if true> : <action2 if true> : etc Block Form This syntax is more flexible and clearer to read when there are multiple actions to perform if the test is true. If the test is false, nothing happens. Note the use of End If to close off the control structure. Any code following the End If is NOT dependant on the test being true. WWP Training Limited Page 73

74 Lesson 5 Control Structures If <test> Then <actions if true> End If For example: If Range( A3 ).Value > 100 Then Range( A3 ).Font.Bold = True Range( A3 ).Font.Size = 14 Range( A3 ).Font.Color = vbblue End If Indentations to the actions if true code is added purely for clarity and to give structure to the block. Although, you can use the single-line form for short, simple tests. However, the block form provides more structure and flexibility than the single-line form. Using block form makes it easier to read, maintain, and test. Other examples of tests that might be used as tests in an If-Then control structure are: If ActiveCell.Value = Bonus Then <action if true> (If the active cell equals the word bonus, then...) If Range( C10 ).Value >= 100 Then <action if true> (If cell C10 contains a value greater than or equal to 100, then...) If ActiveCell <> Then <action if true> (If the active cell is not empty (contains no text), then...) If ActiveCell.Offset(0, 1).Value > 100 Then <action if true> (If the cell on the same row and one column to the right of the active cell is greater than 100, then...) If Range( A6 ).Font.Bold = TRUE Then <action if true> (If cell A6 contains bold font, then... A test can be elaborated further by adding And and/or Or conditions to it. The syntax is as follows: WWP Training Limited Page 74

75 Lesson 5 Control Structures If <test1> And <test2> And <test3> Then End If <actions if true> In the above example, test1, test2 and test3 must ALL be true for the actions if true to occur. If even one of the tests is false, the If ends without carrying out any actions. In the following example only ONE of the test needs to be true in order for the actions if true to occur. If <test1> Or <test2> Or <test3> Then End If <actions if true> And and Or can also be mixed together, for example: If <test1> And <test2> Or <test3> Then <actions if true> End If Procedures 1. Position the cursor in the macro where you want to place the statement. 2. Type If. 3. Type a space. 4. Type a valid test. 5. Type a space. 6. Type Then. 7. Press Enter. 8. Type the statements that you want the macro to perform if the test is true. 9. Press Enter. 10. Type End If. 11. Add additional code as necessary. WWP Training Limited Page 75

76 Lesson 5 Control Structures THE IF-THEN-ELSE CONTROL STRUCTURE Discussion As discussed in the previous topic, the If-Then control structure carries out actions only if the test(s) is/are true. In cases where there is a need for separate actions to be performed if the test is false, the If-Then-Else control structure can be used. The single-line form of the If - Then - Else control structure is: If <test> Then <actions if true> Else <actions if false> Any code following this will be executed irrespective of whether the test is true or false. Because the single-line form can become very long and difficult to read, the block form is preferable in almost all cases: If <test> Then <actions if true> Else <actions if false> End If Any code following the End If will be executed irrespective of whether the test is true or false. Procedures 1. Position the cursor in the sub procedure where you want to place the statement. 2. Type If. 3. Type a space. 4. Type a valid test. 5. Type a space. 6. Type Then. 7. Press Enter. 8. Type the statements that you want the sub procedure to perform if the test is true. 9. Press Enter. WWP Training Limited Page 76

77 Lesson 5 Control Structures 10. Type Else. 11. Type the statements that you want the sub procedure to perform if the test is false. 12. Press Enter. 13. Type End If. 14. Add additional code as necessary. USING ELSEIF IN A CONTROL STRUCTURE Discussion As discussed in the previous topic, the If - Then - Else control structure carries out one set of actions if the test is true, and another if the test is false. By using And and/or Or, you can make the test carry out multiple simultaneous tests. What, however, if you want to carry out multiple sequential tests? In this case ElseIf needs to be incorporated into the If control structure. The syntax is as follows: If <test1> Then <actions if true> ElseIf <test2> Then <actions if true> Else <actions if false> End If In the example below, the font in the active cell is made blue if it contains a value of Status A. If, however, the active cell does not contain Status A, control is passed to the ElseIf, which carries out a second test to establish if it contains the value Status B. If this is true then the font in the active cell is made green. If the active cell does not contain Status B either, then control passes to Else and under all other circumstances makes the font red. WWP Training Limited Page 77

78 Lesson 5 Control Structures If ActiveCell.Value = Status A Then ActiveCell.Font.Color = vbblue ElseIf ActiveCell.Value = Status B Then ActiveCell.Font.Color = vbgreen Else ActiveCell.Font.Color = vbred End If ElseIf cannot be used in the single-line form. There is no limit to how many ElseIfs are added to the block. Rules for If - Then - ElseIf - Then - Else control structures The first test in the block must always be preceded by If; Subsequent tests must be preceded by ElseIf; Else always comes last in the block to cover any possibilities that have not been eliminated by previous tests. All test whether the initial If or subsequent ElseIfs must be followed by Then. Procedures 1. Position the cursor in the sub procedure code where you want to place the statement. 2. Type If. 3. Type a space. 4. Type a valid test. 5. Type a space. 6. Type Then. 7. Press Enter. 8. Type the statements that you want the sub procedure to perform if the test is true. 9. Press Enter. 10. Type ElseIf. 11. Type a space. 12. Type a valid test. 13. Type a space. 14. Type Then. 15. Press Enter. WWP Training Limited Page 78

79 Lesson 5 Control Structures 16. Type the statements that you want the sub procedure to perform if the test is true. 17. Press Enter. 18. Type Else. 19. Type the statements that you want the sub procedure to perform if the test is false. 20. Press Enter. 21. Type End If. 22. Add additional code as necessary. THE SELECT CASE CONTROL STRUCTURE Discussion Select Case can be used as an alternative to If - Then - ElseIf - Else where several conditions may need to be evaluated. This often seen as a more efficient, clearer to read and more concise way of coding where multiple conditions need to be evaluated. It does, however, lack the flexibility and power of If - Then - ElseIf - Else where each condition can involve different variables. The syntax of the Select Case control structure is: Select Case <expression> Case Is = <value1> <actions if true> Case Is = <value2> <actions if true> Case Else <actions under all other circumstances> End Select A single expression is evaluated at the top of the Case structure. Each case is then checked for this value, and the appropriate statements executed. Control is then passed to the statement after the End Select statement. If no Case statement holds the evaluated value, then the statements after the Case Else are executed. Case Else is optional. In the example below, the font in the active cell is made blue if it contains a value of Status A WWP Training Limited Page 79

80 Lesson 5 Control Structures If, however, the active cell does not contain Status A, control passes to the second Case Is and if the active cell contains the value Status B, the font in the active cell is made green. If the active cell does not contain Status B either, then control passes to the Case Else and under all other circumstances makes the font red. Select Case ActiveCell.Value Case Is = Status A Activecell.Font.Color = vbblue Case Is = Status B Activecell.Font.Color = vbgreen Case Else Activecell.Font.Color = vbred End Select The usual comparison operators can be used after the Case Is, such as: Case Is > <value> Case Is >= <value> Case Is < <value> Case Is <= <value> Case Is <> <value> Procedures 1. Position the cursor in the sub procedure code where you want to place the statement. 2. Type Select Case. 3. Type a space. 4. Type the object.property that you want to carry out tests on. Eg. ActiveCell.Value 5. Press Enter. 6. Type Case Is. 7. Type a valid comparison operator, eg. = or > or < or >= or <= or <>. 8. Type the value that you want to compare the object/property against. 9. Press Enter. 10. Type the statements that you want the sub procedure to perform if the test is true. WWP Training Limited Page 80

81 Lesson 5 Control Structures 11. Press Enter. 12. Add further Case Is <comparison operator> <value> statements, if necessary. 13. At the end of the Case Is statements, type Case Else if there is a final set of actions that you want the sub procedure to perform if none of the previous tests are true. 14. Press Enter. 15. Type End Select. 16. Add additional code as necessary. USING A DO LOOP STATEMENT Discussion Loops are used to run the same statements a certain number of times. Loops can be constructed in many ways to suit different circumstances and often, the same result can be obtained by using different loops. There are two main types of loops DO loops and FOR loops. DO loops keep repeating statements while a condition exists or until a condition is met (eg. do this until you get to row 100, or do this while there is data in a cell). FOR loops repeat statements a fixed number of times (eg. do this 10 times), or as a variable (eg. do this for as many times as there are cells in a selected range). The basic structure of all loops is the same, ie. 1. Start of Loop (tells the sub procedure how often to repeat the statement(s) that follow(s)) 2. Statements (the actions to perform a certain number of times) 3. End of Loop (tells the sub procedure to return to the start of the loop and check whether it has carried out the statements a sufficient number of times) In this lesson, the DO loop is examined. FOR loops are considered a more advanced type of loop and hence, discussed and demonstrated separately on the Excel Level 6 training course. A Do Loop is used to run a block of statements until a condition is True or while a condition becomes True. It comes in two forms Do Until and Do While. In the example below, cells in a column (starting at the top of the column) are formatted bold until an empty one is reached. WWP Training Limited Page 81

82 Lesson 5 Control Structures Do Until ActiveCell.Value = (Do the following statements until the active cell is empty) ActiveCell.Font.Bold = True (Make bold the font of the active cell) ActiveCell.Offset(1,0).Select (From the active cell, move down 1 row, same column) Loop (Return to Do Until and check if the active cell is empty. If TRUE, repeat the statement; if FALSE continue the sub procedure from after Loop.) In the same example, a Do While loop would look like this: Do While ActiveCell.Value <> (Do the following statements while the active cell is not empty) ActiveCell.Font.Bold = True (Make bold the font of the active cell) ActiveCell.Offset(1,0).Select (From the active cell, move down 1 row, same column) Loop (Return to Do While and check if the active cell is not empty. If TRUE, repeat the statement; if FALSE continue the sub procedure from after Loop.) It is important to check the condition before entering a loop. If the condition is FALSE the contents of the loop will never execute. In cases where this is an issue, use the following to ensure that the statements are executed at least once: Do (Do the following statements) ActiveCell.Font.Bold = True (Make bold the font of the active cell) ActiveCell.Offset(1,0).Select (From the active cell, move down 1 row, same column) Loop Until ActiveCell = (Check if the active cell is empty. If TRUE, return to Do and repeat the statements; if FALSE continue the sub procedure from after Loop Until...) Or... WWP Training Limited Page 82

83 Lesson 5 Control Structures Do (Do the following statements) ActiveCell.Font.Bold = True (Make bold the font of the active cell) ActiveCell.Offset(1,0).Select (From the active cell, move down 1 row, same column) Loop While ActiveCell <> (Check if the active cell is not empty. If TRUE, return to Do and repeat the statement; if FALSE continue the sub procedure below Loop While...) The Not operator can be used in a Do loop to reverse the logic of the condition. The syntax would be, for example: Do Until Not ActiveCell.Value = Statement(s) Loop This would be the same as: Do Until ActiveCell <> Statement(s) Loop To stop an endless loop press Esc or Ctrl + Break. More Do loop examples: 1. Move down a column of data until an empty cell is found: Do While ActiveCell.Value <> ActiveCell.Offset(1,0).Select Loop WWP Training Limited Page 83

84 Lesson 5 Control Structures 2. Move across a row of data until the cell is found containing today s date: Do Until ActiveCell.Value = Date Loop ActiveCell.Offset(0,1).Select (Date is a VBA function that returns the current date from the system clock. Equivalent to the =TODAY() Excel worksheet function) 3. Move down a column of data until you get to a cell containing bold font. Add a blue font colour to cells with a value higher than 100. Do Until ActiveCell.Font.Bold = TRUE Loop If ActiveCell.Value > 100 then ActiveCell.Font.Color = vbblue End If ActiveCell.Offset(1,0).Select Procedures 1. Position the cursor in the sub procedure code where you want to place the statement. 2. Type Do Until or Do While. 3. Type a space. 4. Type the condition for the Until or While, 5. eg. ActiveCell.Value = or ActiveCell.Offset(0,1) <> 6. Press Enter. 7. Type the statements that you want the sub procedure to perform until or while the condition is true. 8. Press Enter. 9. Type Loop. 10. Add additional code as necessary. USING A WHILE - WEND STATEMENT Discussion The While Wend loop is included in this lesson for completeness and for guidance in cases where you may encounter it in inherited or sample pieces of code. WWP Training Limited Page 84

85 Lesson 5 Control Structures The While Wend loop is very similar to the Do While loop. It makes use of a condition and continues looping until the condition becomes FALSE. In the example below, cells in a column (starting at the top of the column) are formatted bold until an empty one is reached. While ActiveCell.Value <> (Do the following statements while the active cell is not empty) ActiveCell.Font.Bold = True (Make bold the font of the active cell) ActiveCell.Offset(1, 0).Select (From the active cell, move down 1 row, same column) Wend The Do loop provides are more structured and flexible way to perform conditional looping. WWP Training Limited Page 85

86 Lesson 5 Control Structures EXERCISE DECISIONS-MAKING CODE AND DO LOOPS 1. Open the file Practice Decision. 2. Insert a module sheet to the workbook and write a sub procedure named, CheckStock which uses an IF statement block to check the value of an active cell and if the value is less than 300 enters Order now into the cell to the right. 3. Place a DO loop around the IF statement so that it runs down a column until it comes to an empty cell. 4. On the Inventory sheet, select cell C5 and run the CheckStock sub procedure. 5. Edit the sub procedure so that in cases where the value is not less than 300, the text, "Hold Order" is entered into the cell to the right. 6. Delete the text in cells D5:D10 and run the CheckStock procedure again to test your code. 7. Finally, edit your procedure again so that if the cell being tested is equal to 0, the words, "Out of stock" is entered into the cell to the right and formatted to RED font. 8. Run to test the procedure again. 9. Save and close the file. 10. Open the file Personnel Record. 11. Write a procedure named BonusAward and use a CASE SELECT statement block and a DO statement block to carry out the following actions on the table: Test Enter in Bonus column length of service is greater than 5 years 2% length of service is greater than 10 years 3% length of service is greater than 20 years 5% 12. Test the procedure on the values in column D. 13. Save and close the Workbook. WWP Training Limited Page 86

87 LESSON 6 DATA VARIABLES In this lesson, you will learn how to: Create data variables and learn how to assign values to them Declare variables WWP Training Limited Page 87

88 Lesson 6 Data Variables DATA VARIABLES Discussion Data variables are used to hold information temporarily whilst a sub procedure is running; information that can be used later in the sub procedure to carry out a calculation or make a decision. In some cases, the information stored in the variable can be returned to an Excel workbook to achieve a result or carry out an action. One way of thinking of a variable is to look at it as being a container into which you can put something. This something can be many things, eg: A text string A number A date An object (although this is not covered on this course) Variables can also help make your sub procedures more interactive. Consider the following statement that opens an Excel file: WorkBooks.Open ( Sales Data.xls ) (Open the Excel file named Sales Data) Now consider the following where cell A1 contains the text, Sales Data.xls: filetouse = Range( A1 ).Value (This creates a variable named filetouse and stores in it the text, Sales Data) WorkBooks.Open (filetouse) (This uses the text stored in the variable, filetoopen, to open the workbook) The second example provides more flexibility because it is easier to change the contents in cells A1 with the name of the file that you want to open, than accessing the module and changing the code in the sub procedure. CREATING VARIABLES AND ASSIGNING VALUES Discussion To create a variable, you invent a word to use as a name for the variable and then make it equal to something. Rules for creating variable names include:. Single characters (eg. a, b, c, x, y, z) should be avoided unless the variable if being used for simple counting purposes. Using this type of variable name can WWP Training Limited Page 88

89 Lesson 6 Data Variables cause ambiguity and it is better to use descriptive, but concise, names for your variables, eg: newname, salesfig, startdate, numcells. A variable name must start with a letter and not a number. Numbers can be included within the name, but not as the first character, eg. salesfig1 but not 1salesfig. The first character of a variable name should be left lowercase. Because VBA words always start with an uppercase character (eg. ActiveCell, WorkBook, Cells), keeping the first character lowercase helps make your variables stand out more clearly in the sub procedure. Avoid using names that conflict with VBA words such as: activecell, sheets, font, cells, etc. Spaces cannot be used in variable names. You can separate words by either capitalisation, eg. newname, or by using the underscore character, eg. new_name. Most punctuation marks (eg.,. : ;? -) and special characters (eg. $, %, ^, &, #, }) cannot be used. A variable name can be no longer than 250 characters. Assigning a value to a data variable can be done by: 1. Assigning a value from a cell on a worksheet. celval = Range( A1 ).Value mynum = Cells(3,4).Value Assigns the value in cell A1 of the active sheet to the variable celval. celval would, therefore, be a piece of text (a string) or a number. If cell A1 were empty, then celval would store an empty string (viz. nothing!). Assigns the value in cell that is at the intersection of row 3 and column 4 of the active sheet(ie. D4) to the variable mynum. mynum would, therefore, be a piece of text (a string) or a number. If cell D4 was empty, then mynum would store an empty string. 2. Giving the variable a value, either within the sub procedure or via user interaction (eg. by means of an input box - covered on the Excel Level 6 training course). firstnum = 10 secnum = 20 thirdnum = firstnum * secnum MsgBox thirdnum. Stores the number 10 in the variable firstnum; stores the number 20 in the variable secnum and stores their product in the variable thirdnum. Displays the variable thirdnum in a message box. filetouse = InputBox( Enter the Prompts the user to type a file name and WWP Training Limited Page 89

90 Lesson 6 Data Variables name of the file to open ) WorkBooks.Open (filetouse) then opens it. 3. Assigning a value by means of a test carried out on an object property. isitbold = ActiveCell.Font.Bold. numcels = Selection.Cells.Count celaddr = Cells(3, 4).Address Stores in the variable isitbold a TRUE or a FALSE based on whether the font in the active cell is bold or not. Stores in the variable numcels a number denoting the number of cells in a selected range. Stores in the variable celaddr the absolute reference of the cell that is at the intersection of row 3 and column 4 (ie. $D$4). celaddr = Cells(3, 4).Address(False, False) Ditto but stores the relative reference (ie. D4). Variables can change their values during the execution of a sub procedure. The following example uses the value of the variable tempval in a calculation, and then changes it to a different value for a second calculation. tempval = 0.15 calc1 = ActiveCell.Value * tempval tempval = 0.22 calc2 = ActiveCell.Value * tempval Another example of changing the value of a variable is for counting the number of times that a condition is encountered, or where there is a need to count the number of times that a loop has executed. The following example uses a variable, numbold to count the number of cells in a column containing bold font. It then returns the value of counter into cell A1. WWP Training Limited Page 90

91 Lesson 6 Data Variables counter = 0 (Create a variable named counter and store in it a zero) Do Until ActiveCell.Value = (Do the following statements until the active cell is empty) If ActiveCell.Font.Bold = True Then (If the active cell contains bold font then...) counter = counter + 1 (Increase the value of counter by 1 (ie. make the new value of counter equal to its old value + 1)) End if ActiveCell.Offset(1,0).Select (From the active cell, move down 1 row, same column) Loop (Return to Do Until and check if the active cell is empty. If FALSE, repeat the statement; if TRUE continue the sub procedure from after Loop.) Range( A1 ).Value = counter (Return to cell A1 the value of counter) Procedures 1. Position the cursor in the sub procedure code where you want to create the variable. 2. Type a name for the data variable keeping in mind the naming rules. 3. Type =. 4. Type a value for the variable; this can be a number, a string, a boolean expression (True or False) or an object/property that returns one of the above values, 5. eg. ActiveWorkBook.Name or ActiveCell.Value 6. Press Enter. 7. Add additional code as necessary. The following example, uses two counters. One to count the number of cells containing bold font (counter1) and the second to count the number of times that the loop has executed (counter2). Counter2, therefore, counts the number of cells that have been checked in the column. WWP Training Limited Page 91

92 Lesson 6 Data Variables counter1 = 0 counter2 = 0 (Create variables named counter1 and counter2 and store in them zero) Do Until ActiveCell.Value = (Do the following statements until the active cell is empty) If ActiveCell.Font.Bold = True Then (If the active cell contains bold font then...) Counter1 = counter1 + 1 (Increase the value of counter1 by 1 (ie. make the new value of counter its old value + 1)) End if ActiveCell.Offset(1,0).Select (From the active cell, move down 1 row, same column) counter2 = counter2 + 1 (Increase the value of counter2 by 1. Because this statement will occur each time the loop is repeated irrespective of whether the IF is true or false, it will count the total number if times that the loop has executed) Loop (Return to Do Until and check if the active cell is empty. If FALSE, repeat the statement; if TRUE continue the sub procedure from after Loop.) Range( A1 ).Value = counter1 Range( A2 ).Value = counter2 (Return to cell A1 the value of counter1 the number of cells containing bold font and return to cell A2 the value of counter2 the number of cells checked) VARIABLE TYPES Discussion It was mentioned previously that variables can store many things (see page 88). It is, therefore, good practice (although not always necessary in VBA) to tell the sub procedure in advance, what variables are going to be used and the type of data that is going to be stored in them. By doing this, you are not only helping to prevent potential errors and ambiguity in the sub procedure, but also improving the efficiency and performance of the sub procedure by assigning an appropriate amount of computer memory for storing the variable. Although this will have relatively little impact on a small sub procedure that uses just a few variable, it could slow down or crash the computer in cases where there are hundreds of lines of code using dozens of variables. The process of doing this is called declaring a variable,.and is usually carried out at the beginning of a sub procedure by using a Dim statement as follows:. Dim <variable name> As <type> WWP Training Limited Page 92

93 Lesson 6 Data Variables Unless otherwise specified, undeclared variables are assigned the Variant data type. If a data type is not specified, eg. Dim <variable name>... the variable will default to a variant data type. This data type makes it easy to write sub procedures, but it is not always the most efficient data type to use. Because the sub procedure does not know what you are going to put into a variant variable, it may create an unnecessarily large space in the computer s memory with a resulting loss of performance. You should consider using appropriate data types if: Your program is very large and uses many variables. Your program must run as quickly as possible. You write data directly to random-access files. The following provides a list of the more commonly used variables types for storing data. Data type Memory size Storage capability Byte 1 byte 0 to 255 Boolean 2 bytes True or False Integer 2 bytes -32,768 to 32,767 Long 4 bytes -2,147,483,648 to 2,147,483,647 Single 4 bytes E38 to E-45 for negative values; E-45 to E38 for positive values Double 8 bytes E308 to E-324 for negative values; E-324 to E308 for positive values Currency 8 bytes -922,337,203,685, to 922,337,203,685, Decimal 14 bytes +/-79,228,162,514,264,337,593,543,950,335 with no decimal point; +/ with 28 places to the right of the decimal; smallest non-zero number is +/ Date 8 bytes January 1, 100 to December 31, 9999 String (variable-length) 10 bytes + string length 0 to approximately 2 billion During a sub procedure, a variable may be assigned different values and data types. The last declaration will determine what the variable is allowed to store. WWP Training Limited Page 93

94 Lesson 6 Data Variables In the following example, the variable myvar is used firstly to store a number, and then to store a date. Dim myvar as Single myvar = Dim myvar as Date myvar = #01/05/2009# If a specific data type is assigned to a variable, the variable will only be able to hold that type of data. If storing a different data type is attempted, an error message will be displayed when the sub procedure is run. Procedures 1. Position the cursor on the line below the sub procedure name. 2. Type Dim. 3. Type a space. 4. Type a name for the first variable that you intend using in the sub procedure. 5. Type a space. 6. Type As. 7. Type a space. 8. Type (or select from the members list) the type of data that the variable will store. 9. Press Enter. 10. Add additional code as necessary. WWP Training Limited Page 94

95 Lesson 6 Data Variables EXERCISE TO ASSIGN VALUES TO VARIABLES. 1. Open the file, Practice Variables. 2. Create a sub procedure named, VatCalc that: stores the current rate of VAT in a variable named vatrate stores the contents of an active cell in a variable named celamount returns in the cell to the right of the active cell the amount plus VAT 3. Run the macro on cell B5. If working correctly, a value of should be returned into cell C5. 4. Repeat with cells B6 and B7, the results in cells C6 and C7 should be and respectively. 5. Edit the sub procedure to declare the variables. 6. On Sheet2 on the workbook, use declared variables a necessary to create a sub procedure named TransferNames that will reverse the position of Bill and Ben. 7. Test the macro. If correct, Bill should be moved to cell B1 and Ben moved to cell A1. 8. Repeat the sub procedure. The names should move back to their original positions. 9. Save and close the file. 10. Open the file, First Orders. 11. Write a procedure using variables to count and display in cell H1, the number of orders that have a red font colour in the Sent column. 12. Save and close the file. WWP Training Limited Page 95

96 APPENDIX I - MACRO SECURITY SECURITY LEVELS Discussion While many macros are simply recordings of your keystrokes and mouse actions, VBA developers can write macros that carry out many commands on your computer. As a result, hackers can introduce harmful code onto your computer by writing macros that release a virus when run. When opening an Excel file containing macros, therefore, Excel uses various criteria to ensure any macros contained therein are safe. The level and thoroughness at which Excel does this is determined by the security settings in force on your computer. The criteria for this varies depending on whether the workbook is being opened from a trusted location and/or if it is digitally signed. A digital signature.is an electronic, encryption-based, secure stamp of authentication on a macro or document. This signature confirms that the macro or document originated from the signer and has not been altered. Very High (Excel 2003 only) Security Levels - Excel 2003, 2002(XP) & Only macros installed in trusted locations will be allowed to run. All other signed and unsigned macros are disabled. When attempting to open a workbook containing macros, the following message will appear: High Medium Unsigned macros are automatically disabled and the file is opened. With signed macros, the source of the macro and the status of the signature determine how signed macros are handled. When attempting to open a workbook containing macros, no message will appear. With unsigned macros, the user is prompted to enable or disable the macros. When attempting to open a workbook containing unsigned macros, the following message will appear. WWP Training Limited Page 96

97 Appendix I - Security Low With signed macros, the source of the macro and the status of the signature determine how signed macros are handled. When security it set to low, all macros are treated equally regardless of origin or certificate status. With low security, you receive no prompt or signature validation and macros are automatically enabled. Use this setting only if you are certain that all macros in your files are from trusted sources. In Excel 2007, macro security has changed significantly. Although the security levels are comparable with earlier versions of Excel, the way in which Excel 2007 warns you about potential risks has changed. Instead of a message box appearing on the screen, a Message Bar appears above the work area at the left and below the Ribbon. These are referred to as notifications. Excel 2007 macro security notification Disable all macros without notification Security Levels (Excel 2007). Click this option if you don't trust the macros. All macros and security alerts about macros are disabled. If there are documents that contain unsigned macros that you do trust, you can put those documents into a trusted location (see page 99). Documents in trusted locations are allowed to run without being checked by the Trust Center security system. Disable all macros with notification (default) With unsigned macros, the user is prompted to enable or disable the macros. On clicking the Options... button, the following dialog box appears: WWP Training Limited Page 97

98 Appendix I - Security Disable all macros except digitally signed macros With signed macros, the source of the macro and the status of the signature determine how signed macros are handled. Enable all macros When security it set to low, all macros are treated equally regardless of origin or certificate status. With low security, you receive no prompt or signature validation and macros are automatically enabled. Use this setting only if you are certain that all macros in your files are from trusted sources. In Microsoft Office 2003 or later, a component checks all XML files that have references to XSL files for script that could be unsafe. If macro security is set to High, running this script is disabled. If macro security is set to Medium, the user is asked whether or not to run script in XSL files. If macro security is set to Low, the script is run. CHANGING MACRO SECURITY SETTINGS Discussion If you re creating your own macros, or the macros that you run are from a reliable source within your own workplace, you ll probably want to lower the protective security settings. If, however, the macros you use are often received from unknown sources, you may wish to increase it. Procedures (Excel Versions ) 11. Click the Tools menu. 12. Point at Macro. 13. Select Security Select the required security level option. WWP Training Limited Page 98

MICROSOFT EXCEL VISUAL BASIC FOR APPLICATIONS INTRODUCTION

MICROSOFT EXCEL VISUAL BASIC FOR APPLICATIONS INTRODUCTION MICROSOFT EXCEL VISUAL BASIC FOR APPLICATIONS INTRODUCTION NOTE Unless otherwise stated, screenshots of dialog boxes and screens in this book were taken using Excel 2003 running on Window XP Professional.

More information

MICROSOFT EXCEL 2000 LEVEL 5 VBA PROGRAMMING INTRODUCTION

MICROSOFT EXCEL 2000 LEVEL 5 VBA PROGRAMMING INTRODUCTION MICROSOFT EXCEL 2000 LEVEL 5 VBA PROGRAMMING INTRODUCTION Lesson 1 - Recording Macros Excel 2000: Level 5 (VBA Programming) Student Edition LESSON 1 - RECORDING MACROS... 4 Working with Visual Basic Applications...

More information

MICROSOFT EXCEL. VERSIONS 2007 & 2010 Level 2

MICROSOFT EXCEL. VERSIONS 2007 & 2010 Level 2 MICROSOFT EXCEL VERSIONS 2007 & 2010 Level 2 Welcome! Thank you for choosing WWP as your learning and development provider. We hope that your programme today will be a stimulating, informative and rewarding

More information

As you probably know, Microsoft Excel is an

As you probably know, Microsoft Excel is an Introducing Excel Programming As you probably know, Microsoft Excel is an electronic worksheet you can use for a variety of purposes, including the following: maintain lists; perform mathematical, financial,

More information

Macros enable you to automate almost any task that you can undertake

Macros enable you to automate almost any task that you can undertake Chapter 1: Building and Running Macros In This Chapter Understanding how macros do what they do Recording macros for instant playback Using the relative option when recording macros Running the macros

More information

Macros enable you to automate almost any task that you can undertake

Macros enable you to automate almost any task that you can undertake Chapter 1: Building and Running Macros In This Chapter Understanding how macros do what they do Recording macros for instant playback Using the relative option when recording macros Running the macros

More information

Using macros enables you to repeat tasks much

Using macros enables you to repeat tasks much An Introduction to Macros Using macros enables you to repeat tasks much more efficiently than tediously performing each step over and over. A macro is a set of instructions that you use to automate a task.

More information

Getting started 7. Writing macros 23

Getting started 7. Writing macros 23 Contents 1 2 3 Getting started 7 Introducing Excel VBA 8 Recording a macro 10 Viewing macro code 12 Testing a macro 14 Editing macro code 15 Referencing relatives 16 Saving macros 18 Trusting macros 20

More information

Introduction to macros

Introduction to macros L E S S O N 7 Introduction to macros Suggested teaching time 30-40 minutes Lesson objectives To understand the basics of creating Visual Basic for Applications modules in Excel, you will: a b c Run existing

More information

VBA Excel 2013/2016. VBA Visual Basic for Applications. Learner Guide

VBA Excel 2013/2016. VBA Visual Basic for Applications. Learner Guide VBA Visual Basic for Applications Learner Guide 1 Table of Contents SECTION 1 WORKING WITH MACROS...5 WORKING WITH MACROS...6 About Excel macros...6 Opening Excel (using Windows 7 or 10)...6 Recognizing

More information

Lesson 2. Using the Macro Recorder

Lesson 2. Using the Macro Recorder Lesson 2. Using the Macro Recorder When the recorder is activated, everything that you do will be recorded as a Macro. When the Macro is run, everything that you recorded will be played back exactly as

More information

You can record macros to automate tedious

You can record macros to automate tedious Introduction to Macros You can record macros to automate tedious and repetitive tasks in Excel without writing programming code directly. Macros are efficiency tools that enable you to perform repetitive

More information

4) Study the section of a worksheet in the image below. What is the cell address of the cell containing the word "Qtr3"?

4) Study the section of a worksheet in the image below. What is the cell address of the cell containing the word Qtr3? Choose The Correct Answer: 1) Study the highlighted cells in the image below and identify which of the following represents the correct cell address for these cells: a) The cell reference for the selected

More information

MICROSOFT EXCEL. VERSIONS 2007 & 2010 Level 1

MICROSOFT EXCEL. VERSIONS 2007 & 2010 Level 1 MICROSOFT EXCEL VERSIONS 2007 & 2010 Level 1 Welcome! Thank you for choosing WWP as your learning and development provider. We hope that your programme today will be a stimulating, informative and rewarding

More information

Macros in Excel: Recording, Running, and Editing

Macros in Excel: Recording, Running, and Editing Macros in Excel: Recording, Running, and Editing This document provides instructions for creating, using, and revising macros in Microsoft Excel. Simple, powerful, and easy to customize, Excel macros can

More information

Microsoft Excel 2007 Macros and VBA

Microsoft Excel 2007 Macros and VBA Microsoft Excel 2007 Macros and VBA With the introduction of Excel 2007 Microsoft made a number of changes to the way macros and VBA are approached. This document outlines these special features of Excel

More information

EVALUATION COPY. Unauthorized Reproduction or Distribution Prohibited

EVALUATION COPY. Unauthorized Reproduction or Distribution Prohibited INTRODUCTION TO MICROSOFT EXCEL 2016 Introduction to Microsoft Excel 2016 (EXC2016.1 version 1.0.1) Copyright Information Copyright 2016 Webucator. All rights reserved. The Authors Dave Dunn Dave Dunn

More information

MODULE VI: MORE FUNCTIONS

MODULE VI: MORE FUNCTIONS MODULE VI: MORE FUNCTIONS Copyright 2012, National Seminars Training More Functions Using the VLOOKUP and HLOOKUP Functions Lookup functions look up values in a table and return a result based on those

More information

This book is about using Microsoft Excel to

This book is about using Microsoft Excel to Introducing Data Analysis with Excel This book is about using Microsoft Excel to analyze your data. Microsoft Excel is an electronic worksheet you can use to perform mathematical, financial, and statistical

More information

Excel Select a template category in the Office.com Templates section. 5. Click the Download button.

Excel Select a template category in the Office.com Templates section. 5. Click the Download button. Microsoft QUICK Excel 2010 Source Getting Started The Excel Window u v w z Creating a New Blank Workbook 2. Select New in the left pane. 3. Select the Blank workbook template in the Available Templates

More information

Advanced Excel Macros : Data Validation/Analysis : OneDrive

Advanced Excel Macros : Data Validation/Analysis : OneDrive Advanced Excel Macros : Data Validation/Analysis : OneDrive Macros Macros in Excel are in short, a recording of keystrokes. Beyond simple recording, you can use macros to automate tasks that you will use

More information

Excel Pivot Tables & Macros

Excel Pivot Tables & Macros Excel 2007 Pivot Tables & Macros WORKSHOP DESCRIPTION...1 Overview 1 Prerequisites 1 Objectives 1 WHAT IS A PIVOT TABLE...2 Sample Example 2 PivotTable Terminology 3 Creating a PivotTable 4 Layout of

More information

Microsoft Excel 2007

Microsoft Excel 2007 Learning computers is Show ezy Microsoft Excel 2007 301 Excel screen, toolbars, views, sheets, and uses for Excel 2005-8 Steve Slisar 2005-8 COPYRIGHT: The copyright for this publication is owned by Steve

More information

Chapter11 practice file folder. For more information, see Download the practice files in this book s Introduction.

Chapter11 practice file folder. For more information, see Download the practice files in this book s Introduction. Make databases user friendly 11 IN THIS CHAPTER, YOU WILL LEARN HOW TO Design navigation forms. Create custom categories. Control which features are available. A Microsoft Access 2013 database can be a

More information

Excel 2013 Intermediate

Excel 2013 Intermediate Excel 2013 Intermediate Quick Access Toolbar... 1 Customizing Excel... 2 Keyboard Shortcuts... 2 Navigating the Spreadsheet... 2 Status Bar... 3 Worksheets... 3 Group Column/Row Adjusments... 4 Hiding

More information

Excel Main Screen. Fundamental Concepts. General Keyboard Shortcuts Open a workbook Create New Save Preview and Print Close a Workbook

Excel Main Screen. Fundamental Concepts. General Keyboard Shortcuts Open a workbook Create New Save Preview and Print Close a Workbook Excel 2016 Main Screen Fundamental Concepts General Keyboard Shortcuts Open a workbook Create New Save Preview and Print Close a Ctrl + O Ctrl + N Ctrl + S Ctrl + P Ctrl + W Help Run Spell Check Calculate

More information

MICROSOFT OFFICE. Courseware: Exam: Sample Only EXCEL 2016 CORE. Certification Guide

MICROSOFT OFFICE. Courseware: Exam: Sample Only EXCEL 2016 CORE. Certification Guide MICROSOFT OFFICE Courseware: 3263 2 Exam: 77 727 EXCEL 2016 CORE Certification Guide Microsoft Office Specialist 2016 Series Microsoft Excel 2016 Core Certification Guide Lesson 1: Introducing Excel Lesson

More information

COPYRIGHTED MATERIAL. Making Excel More Efficient

COPYRIGHTED MATERIAL. Making Excel More Efficient Making Excel More Efficient If you find yourself spending a major part of your day working with Excel, you can make those chores go faster and so make your overall work life more productive by making Excel

More information

Using Microsoft Excel

Using Microsoft Excel About Excel Using Microsoft Excel What is a Spreadsheet? Microsoft Excel is a program that s used for creating spreadsheets. So what is a spreadsheet? Before personal computers were common, spreadsheet

More information

DOWNLOAD PDF MICROSOFT OFFICE POWERPOINT 2003, STEP BY STEP

DOWNLOAD PDF MICROSOFT OFFICE POWERPOINT 2003, STEP BY STEP Chapter 1 : Microsoft Office Excel Step by Step - PDF Free Download Microsoft Office PowerPoint Step by Step This is a good book for an 76 year old man like me. It was a great help in teaching me to do

More information

Microsoft Excel 2010 Part 2: Intermediate Excel

Microsoft Excel 2010 Part 2: Intermediate Excel CALIFORNIA STATE UNIVERSITY, LOS ANGELES INFORMATION TECHNOLOGY SERVICES Microsoft Excel 2010 Part 2: Intermediate Excel Spring 2014, Version 1.0 Table of Contents Introduction...3 Working with Rows and

More information

12 BASICS OF MS-EXCEL

12 BASICS OF MS-EXCEL 12 BASICS OF MS-EXCEL 12.1 INTRODUCTION MS-Excel 2000 is a Windows based application package. It is quite useful in entering, editing, analysis and storing of data. Arithmetic operations with numerical

More information

Microsoft Excel 2010 Level 1

Microsoft Excel 2010 Level 1 Microsoft Excel 2010 Level 1 One Day Course Course Description You have basic computer skills such as using a mouse, navigating through windows, and surfing the Internet. You have also used paper-based

More information

Excel 2010 Foundation. Excel 2010 Foundation SAMPLE

Excel 2010 Foundation. Excel 2010 Foundation SAMPLE Excel 2010 Foundation Excel 2010 Foundation Excel 2010 Foundation Page 2 2010 Cheltenham Courseware Pty. Ltd. All trademarks acknowledged. E&OE. No part of this document may be copied without written permission

More information

ACCESS 2007 ADVANCED

ACCESS 2007 ADVANCED ACCESS 2007 ADVANCED Welcome! Thank you for choosing WWP as your learning and development provider. We hope that your programme today will be a stimulating, informative and rewarding experience. Our highly

More information

EXCEL 2003 DISCLAIMER:

EXCEL 2003 DISCLAIMER: EXCEL 2003 DISCLAIMER: This reference guide is meant for experienced Microsoft Excel users. It provides a list of quick tips and shortcuts for familiar features. This guide does NOT replace training or

More information

Section 1 Microsoft Excel Overview

Section 1 Microsoft Excel Overview Course Topics: I. MS Excel Overview II. Review of Pasting and Editing Formulas III. Formatting Worksheets and Cells IV. Creating Templates V. Moving and Navigating Worksheets VI. Protecting Sheets VII.

More information

Microsoft Excel - Macros Explained

Microsoft Excel - Macros Explained Microsoft Excel - Macros Explained Macros Explained Macros or macroinstructions allow you to automate procedures or calculations in Excel. Macros are usually recorded using the Macro recorder and then

More information

Table of Contents. Word. Using the mouse wheel 39 Moving the insertion point using the keyboard 40 Resume reading 41

Table of Contents. Word. Using the mouse wheel 39 Moving the insertion point using the keyboard 40 Resume reading 41 Table of Contents iii Table of Contents Word Starting Word What is word processing? 2 Starting Word 2 Exploring the Start screen 4 Creating a blank document 4 Exploring the Word document window 5 Exploring

More information

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan DATA 301 Introduction to Data Analytics Microsoft Excel VBA Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca DATA 301: Data Analytics (2) Why Microsoft Excel Visual Basic

More information

PART 7. Getting Started with Excel

PART 7. Getting Started with Excel PART 7 Getting ed with Excel When you start the application, Excel displays a blank workbook. A workbook is a file in which you store your data, similar to a three-ring binder. Within a workbook are worksheets,

More information

Excel has a powerful automation feature that lets you automate processes that you need to do repeatedly.

Excel has a powerful automation feature that lets you automate processes that you need to do repeatedly. Professor Shoemaker There are times in Excel when you have a process that requires several or many steps and that you need to do repeatedly. Excel has a powerful automation feature that lets you automate

More information

Excel Part 3 Textbook Addendum

Excel Part 3 Textbook Addendum Excel Part 3 Textbook Addendum 1. Lesson 1 Activity 1-1 Creating Links Data Alert and Alternatives After completing Activity 1-1, you will have created links in individual cells that point to data on other

More information

Excel 2010: Getting Started with Excel

Excel 2010: Getting Started with Excel Excel 2010: Getting Started with Excel Excel 2010 Getting Started with Excel Introduction Page 1 Excel is a spreadsheet program that allows you to store, organize, and analyze information. In this lesson,

More information

What is a spreadsheet?

What is a spreadsheet? Microsoft Excel is a spreadsheet developed by Microsoft. It is a software program included in the Microsoft Office suite (Others include MS Word, MS PowerPoint, MS Access etc.). Microsoft Excel is used

More information

This chapter is intended to take you through the basic steps of using the Visual Basic

This chapter is intended to take you through the basic steps of using the Visual Basic CHAPTER 1 The Basics This chapter is intended to take you through the basic steps of using the Visual Basic Editor window and writing a simple piece of VBA code. It will show you how to use the Visual

More information

Using Microsoft Excel

Using Microsoft Excel Using Microsoft Excel Formatting a spreadsheet means changing the way it looks to make it neater and more attractive. Formatting changes can include modifying number styles, text size and colours. Many

More information

Excel VBA. Microsoft Excel is an extremely powerful tool that you can use to manipulate, analyze, and present data.

Excel VBA. Microsoft Excel is an extremely powerful tool that you can use to manipulate, analyze, and present data. Excel VBA WHAT IS VBA AND WHY WE USE IT Microsoft Excel is an extremely powerful tool that you can use to manipulate, analyze, and present data. Sometimes though, despite the rich set of features in the

More information

Project Advanced. Microsoft Application Series. Best STL

Project Advanced.  Microsoft Application Series. Best STL Microsoft Application Series Project Advanced Best STL Courses never cancelled: guaranteed Last minute rescheduling 24 months access to Microsoft trainers 12+ months schedule UK wide delivery www.microsofttraining.net

More information

Using SymPrint to Make Overlays, Templates & More...

Using SymPrint to Make Overlays, Templates & More... Welcome to SymPrint SymPrint is an easy-to-use tool for creating communication overlays, worksheets, classroom activities and more using a modern toolbar and common-sense interface modeled after the programs

More information

Make Your Documents Accessible Worksheet (Microsoft Word 2010)

Make Your Documents Accessible Worksheet (Microsoft Word 2010) Make Your Documents Accessible Worksheet (Microsoft Word 2010) This exercise is intended for staff attending the Make your documents accessible course, although other staff will also find this resource

More information

Table of Contents Lesson 1: Introduction to the New Interface... 2 Lesson 2: Prepare to Work with Office

Table of Contents Lesson 1: Introduction to the New Interface... 2 Lesson 2: Prepare to Work with Office Table of Contents Lesson 1: Introduction to the New Interface... 2 Exercise 1: The New Elements... 3 Exercise 2: Use the Office Button and Quick Access Toolbar... 4 The Office Button... 4 The Quick Access

More information

SAMPLE ICDL 5.0. International Computer Driving Licence. Module 4 - Spreadsheets Using Microsoft Excel 2010

SAMPLE ICDL 5.0. International Computer Driving Licence. Module 4 - Spreadsheets Using Microsoft Excel 2010 ICDL 5.0 International Computer Driving Licence Module 4 - Spreadsheets Using Microsoft Excel 2010 This training, which has been approved by ECDL Foundation, includes exercise items intended to assist

More information

EXCEL 2002 (XP) FOCUS ON: DESIGNING SPREADSHEETS AND WORKBOOKS

EXCEL 2002 (XP) FOCUS ON: DESIGNING SPREADSHEETS AND WORKBOOKS EXCEL 2002 (XP) FOCUS ON: DESIGNING SPREADSHEETS AND WORKBOOKS ABOUT GLOBAL KNOWLEDGE, INC. Global Knowledge, Inc., the world s largest independent provider of integrated IT education solutions, is dedicated

More information

Using Microsoft Excel

Using Microsoft Excel Using Microsoft Excel Files in Microsoft Excel are referred to as Workbooks. This is because they can contain more than one sheet. The number of sheets a workbook can contain is only limited by your computer

More information

download instant at

download instant at CHAPTER 1 - LAB SESSION INTRODUCTION TO EXCEL INTRODUCTION: This lab session is designed to introduce you to the statistical aspects of Microsoft Excel. During this session you will learn how to enter

More information

EXCEL BASICS: MICROSOFT OFFICE 2007

EXCEL BASICS: MICROSOFT OFFICE 2007 EXCEL BASICS: MICROSOFT OFFICE 2007 GETTING STARTED PAGE 02 Prerequisites What You Will Learn USING MICROSOFT EXCEL PAGE 03 Opening Microsoft Excel Microsoft Excel Features Keyboard Review Pointer Shapes

More information

239 Excel Keyboard Shortcuts

239 Excel Keyboard Shortcuts 239 Excel Keyboard Shortcuts WORK FASTER AND MORE EFFICIENTLY WITH THESE CLEARLY ILLUSTRATED EXCEL SHORTCUTS. My Online Training Hub https://www.myonlinetraininghub.com/ Below is a huge list of Excel keyboard

More information

Status Bar: Right click on the Status Bar to add or remove features.

Status Bar: Right click on the Status Bar to add or remove features. Excel 2013 Quick Start Guide The Excel Window File Tab: Click to access actions like Print, Save As, etc. Also to set Excel options. Ribbon: Logically organizes actions onto Tabs, Groups, and Buttons to

More information

From workbook ExcelPart2.xlsx, select FlashFillExample worksheet.

From workbook ExcelPart2.xlsx, select FlashFillExample worksheet. Microsoft Excel 2013: Part 2 More on Cells: Modifying Columns, Rows, & Formatting Cells Find and Replace This feature helps you save time to locate specific information when working with a lot of data

More information

Lecture- 5. Introduction to Microsoft Excel

Lecture- 5. Introduction to Microsoft Excel Lecture- 5 Introduction to Microsoft Excel The Microsoft Excel Window Microsoft Excel is an electronic spreadsheet. You can use it to organize your data into rows and columns. You can also use it to perform

More information

WEEK NO. 12 MICROSOFT EXCEL 2007

WEEK NO. 12 MICROSOFT EXCEL 2007 WEEK NO. 12 MICROSOFT EXCEL 2007 LESSONS OVERVIEW: GOODBYE CALCULATORS, HELLO SPREADSHEET! 1. The Excel Environment 2. Starting A Workbook 3. Modifying Columns, Rows, & Cells 4. Working with Worksheets

More information

Using Microsoft Excel

Using Microsoft Excel Using Microsoft Excel Table of Contents The Excel Window... 2 The Formula Bar... 3 Workbook View Buttons... 3 Moving in a Spreadsheet... 3 Entering Data... 3 Creating and Renaming Worksheets... 4 Opening

More information

Examples: To speed up routine editing and formatting Page X of Y. If you type teh plus a space, AutoCorrect replaces it with "the.

Examples: To speed up routine editing and formatting Page X of Y. If you type teh plus a space, AutoCorrect replaces it with the. Ways to Automate Repetitive Tasks: AutoText The AutoText feature lets you specify a short name for a body of text or graphics and then lets you insert that body of material just by typing the short name.

More information

When you pass Exam : Access 2010, you complete the requirements for the Microsoft Office Specialist (MOS) - Access 2010 certification.

When you pass Exam : Access 2010, you complete the requirements for the Microsoft Office Specialist (MOS) - Access 2010 certification. Appendix 1 Microsoft Office Specialist: Access Certification Introduction The candidates for Microsoft Office Specialist certification should have core-level knowledge of Microsoft Office Access 2010.

More information

Word 2016 Advanced. North American Edition SAMPLE

Word 2016 Advanced. North American Edition SAMPLE Word 2016 Advanced Word 2016 Advanced North American Edition WORD 2016 ADVANCED Page 2 2015 Cheltenham Group Pty. Ltd. All trademarks acknowledged. E&OE. No part of this document may be copied without

More information

PowerPoint 2010 Level 1 Computer Training Solutions Student Guide Version Revision Date Course Length

PowerPoint 2010 Level 1 Computer Training Solutions Student Guide Version Revision Date Course Length Level 1 Computer Training Solutions Version 1.2 Revision Date Course Length 2012-Feb-16 6 hours Table of Contents Quick Reference... 3 Frequently Used Commands... 3 Manitoba ehealth Learning Management

More information

Microsoft Excel > Shortcut Keys > Shortcuts

Microsoft Excel > Shortcut Keys > Shortcuts Microsoft Excel > Shortcut Keys > Shortcuts Function Keys F1 Displays the Office Assistant or (Help > Microsoft Excel Help) F2 Edits the active cell, putting the cursor at the end* F3 Displays the (Insert

More information

Excel. module. Lesson 1 Create a Worksheet Lesson 2 Create and Revise. Lesson 3 Edit and Format

Excel. module. Lesson 1 Create a Worksheet Lesson 2 Create and Revise. Lesson 3 Edit and Format module 2 Excel Lesson 1 Create a Worksheet Lesson 2 Create and Revise Formulas Lesson 3 Edit and Format Worksheets Lesson 4 Print Worksheets Lesson 5 Modify Workbooks Lesson 6 Create and Modify Charts

More information

INTRODUCTION... 1 UNDERSTANDING CELLS... 2 CELL CONTENT... 4

INTRODUCTION... 1 UNDERSTANDING CELLS... 2 CELL CONTENT... 4 Introduction to Microsoft Excel 2016 INTRODUCTION... 1 The Excel 2016 Environment... 1 Worksheet Views... 2 UNDERSTANDING CELLS... 2 Select a Cell Range... 3 CELL CONTENT... 4 Enter and Edit Data... 4

More information

EXCEL BASICS: MICROSOFT OFFICE 2010

EXCEL BASICS: MICROSOFT OFFICE 2010 EXCEL BASICS: MICROSOFT OFFICE 2010 GETTING STARTED PAGE 02 Prerequisites What You Will Learn USING MICROSOFT EXCEL PAGE 03 Opening Microsoft Excel Microsoft Excel Features Keyboard Review Pointer Shapes

More information

Laboratory 1. Part 1: Introduction to Spreadsheets

Laboratory 1. Part 1: Introduction to Spreadsheets Laboratory 1 Part 1: Introduction to Spreadsheets By the end of this laboratory session you should be familiar with: Navigating around a worksheet. Naming sheets and cells. Formatting. The use of formulae.

More information

6. Essential Spreadsheet Operations

6. Essential Spreadsheet Operations 6. Essential Spreadsheet Operations 6.1 Working with Worksheets When you open a new workbook in Excel, the workbook has a designated number of worksheets in it. You can specify how many sheets each new

More information

MAS 90/200 Intelligence Tips and Tricks Booklet Vol. 1

MAS 90/200 Intelligence Tips and Tricks Booklet Vol. 1 MAS 90/200 Intelligence Tips and Tricks Booklet Vol. 1 1 Contents Accessing the Sage MAS Intelligence Reports... 3 Copying, Pasting and Renaming Reports... 4 To create a new report from an existing report...

More information

Chapter 10 Linking Calc Data

Chapter 10 Linking Calc Data Calc Guide Chapter 10 Linking Calc Data Sharing data in and out of Calc This PDF is designed to be read onscreen, two pages at a time. If you want to print a copy, your PDF viewer should have an option

More information

Microsoft Excel 2013 Unit 1: Spreadsheet Basics & Navigation Student Packet

Microsoft Excel 2013 Unit 1: Spreadsheet Basics & Navigation Student Packet Microsoft Excel 2013 Unit 1: Spreadsheet Basics & Navigation Student Packet Signing your name below means the work you are turning in is your own work and you haven t given your work to anyone else. Name

More information

ABOUT THIS COURSE... 3 ABOUT THIS MANUAL... 4 LESSON 1: MANAGING LISTS... 5

ABOUT THIS COURSE... 3 ABOUT THIS MANUAL... 4 LESSON 1: MANAGING LISTS... 5 Table of Contents ABOUT THIS COURSE... 3 ABOUT THIS MANUAL... 4 LESSON 1: MANAGING LISTS... 5 TOPIC 1A: SORT A LIST... 6 Sort a list in A-Z or Z-A Order... 6 TOPIC 1B: RENUMBER A LIST... 7 Renumber a List

More information

Inserting Information into PowerPoint

Inserting Information into PowerPoint LESSON 6 6.1 Inserting Information into PowerPoint After completing this lesson, you will be able to: Change the layout of a slide. Insert a clip art image. Scale an image. Insert and format a table. Insert

More information

Using Microsoft Word. Table of Contents

Using Microsoft Word. Table of Contents Using Microsoft Word Table of Contents The Word Screen... 2 Document View Buttons... 2 Selecting Text... 3 Using the Arrow Keys... 3 Using the Mouse... 3 Line Spacing... 4 Paragraph Alignment... 4 Show/Hide

More information

Study Guide. PCIC 3 B2 GS3- Key Applications-Excel. Copyright 2010 Teknimedia Corporation

Study Guide. PCIC 3 B2 GS3- Key Applications-Excel. Copyright 2010 Teknimedia Corporation Study Guide PCIC 3 B2 GS3- Key Applications-Excel Copyright 2010 Teknimedia Corporation Teknimedia grants permission to any licensed owner of PCIC 3 B GS3 Key Applications-Excel to duplicate the contents

More information

Basic Microsoft Excel 2007

Basic Microsoft Excel 2007 Basic Microsoft Excel 2007 Contents Starting Excel... 2 Excel Window Properties... 2 The Ribbon... 3 Tabs... 3 Contextual Tabs... 3 Dialog Box Launchers... 4 Galleries... 5 Minimizing the Ribbon... 5 The

More information

Excel 2013 Essentials Syllabus

Excel 2013 Essentials Syllabus Excel 2013 Essentials Syllabus Lesson 1 Managing Workbooks & Worksheets 1.1 Introduction Lesson content; What is a spreadsheet? The course folders; The course player; Before you start. 1.2 The Excel 2013

More information

A Guided Tour of Doc-To-Help

A Guided Tour of Doc-To-Help A Guided Tour of Doc-To-Help ii Table of Contents Table of Contents...ii A Guided Tour of Doc-To-Help... 1 Converting Projects to Doc-To-Help 2005... 1 Using Microsoft Word... 10 Using HTML Source Documents...

More information

Microsoft Office Excel 2007: Basic. Course Overview. Course Length: 1 Day. Course Overview

Microsoft Office Excel 2007: Basic. Course Overview. Course Length: 1 Day. Course Overview Microsoft Office Excel 2007: Basic Course Length: 1 Day Course Overview This course teaches the basic functions and features of Excel 2007. After an introduction to spreadsheet terminology and Excel's

More information

With ClaroIdeas you can quickly and easily create idea maps using a combination of words, symbols and pictures.

With ClaroIdeas you can quickly and easily create idea maps using a combination of words, symbols and pictures. Welcome to ClaroIdeas ClaroIdeas is a fresh tool to support the creation and editing of concept maps or idea maps using visual and audio components. It has been specifically developed to support people

More information

Excel Advanced

Excel Advanced Excel 2016 - Advanced LINDA MUCHOW Alexandria Technical & Community College 320-762-4539 lindac@alextech.edu Table of Contents Macros... 2 Adding the Developer Tab in Excel 2016... 2 Excel Macro Recorder...

More information

Introduction to Microsoft Word 2010

Introduction to Microsoft Word 2010 Introduction to Microsoft Word 2010 Microsoft Word is a word processing program you can use to write letters, resumes, reports, and more. Anything you can create with a typewriter, you can create with

More information

Microsoft Excel 2002 M O D U L E 2

Microsoft Excel 2002 M O D U L E 2 THE COMPLETE Excel 2002 M O D U L E 2 CompleteVISUAL TM Step-by-step Series Computer Training Manual www.computertrainingmanual.com Copyright Notice Copyright 2002 EBook Publishing. All rights reserved.

More information

IP4 - Running reports

IP4 - Running reports To assist with tracking and monitoring HRIS recruitment and personnel, reports can be run from Discoverer Plus. This guide covers the following process steps: Logging in... 2 What s changed? Changed reference

More information

EVALUATION ONLY. In this lesson, you will learn about Excel. Using LOOKUP Functions, PivotTables, and Macros EXCEL 2013 LESSON OUTLINE

EVALUATION ONLY. In this lesson, you will learn about Excel. Using LOOKUP Functions, PivotTables, and Macros EXCEL 2013 LESSON OUTLINE 12 EXCEL 2013 LESSON OUTLINE Using LOOKUP Functions, PivotTables, and Macros Introducing Lookup Functions Creating PivotTables Creating PivotCharts Changing Macro Security Recording Macros Running Macros

More information

Excel Macros, Links and Other Good Stuff

Excel Macros, Links and Other Good Stuff Excel Macros, Links and Other Good Stuff COPYRIGHT Copyright 2001 by EZ-REF Courseware, Laguna Beach, CA http://www.ezref.com/ All rights reserved. This publication, including the student manual, instructor's

More information

Microsoft How to Series

Microsoft How to Series Microsoft How to Series Getting Started with EXCEL 2007 A B C D E F Tabs Introduction to the Excel 2007 Interface The Excel 2007 Interface is comprised of several elements, with four main parts: Office

More information

Read Me First (Excel 2007)

Read Me First (Excel 2007) Read Me First (Excel 2007) Concrete Mix Evaluator Before installing the CME program please go through these steps to configure your Excel 2007. Open a NEW BLANK Workbook and click on the "Developer " (A)

More information

Word 2016 Tips. Rylander Consulting

Word 2016 Tips. Rylander Consulting Word 2016 Tips Rylander Consulting www.rylanderconsulting.com sandy@rylanderconsulting.com 425.445.0064 Word 2016 i Table of Contents Screen Display Tips... 1 Create a Shortcut to a Recently Opened Document

More information

Lesson 2 Quick Tour and Features

Lesson 2 Quick Tour and Features Lesson 2 Quick Tour and Features Objectives Students will format a document page. Students will use a spell-checker. Students will copy, cut, and paste text. Students will adjust paragraph indentations.

More information

Introduction to Microsoft Word 2010

Introduction to Microsoft Word 2010 Introduction to Microsoft Word 2010 Microsoft Word is a word processing program you can use to write letters, resumes, reports, and more. Anything you can create with a typewriter, you can create with

More information

Die Dettmer-Seminarunterlagen für den Umstieg auf Office 2016 im Word-Format können Ihre Kursteilnehmer ausgedruckt oder als PDF-Datei nutzen

Die Dettmer-Seminarunterlagen für den Umstieg auf Office 2016 im Word-Format können Ihre Kursteilnehmer ausgedruckt oder als PDF-Datei nutzen Die Dettmer-Seminarunterlagen für den Umstieg auf Office 2016 im Word-Format können Ihre Kursteilnehmer ausgedruckt oder als PDF-Datei nutzen 10 OneDrive and Microsoft Office 2016 Online OneDrive (previously

More information

NiceForm User Guide. English Edition. Rev Euro Plus d.o.o. & Niceware International LLC All rights reserved.

NiceForm User Guide. English Edition. Rev Euro Plus d.o.o. & Niceware International LLC All rights reserved. www.nicelabel.com, info@nicelabel.com English Edition Rev-0910 2009 Euro Plus d.o.o. & Niceware International LLC All rights reserved. www.nicelabel.com Head Office Euro Plus d.o.o. Ulica Lojzeta Hrovata

More information

1: Getting Started with Microsoft Excel

1: Getting Started with Microsoft Excel 1: Getting Started with Microsoft Excel The Workspace 1 Menu commands 2 Toolbars 3 Cell References 4 Cell Entries 4 Formatting 5 Saving and Opening Workbook Files 7 The Workspace Figure 1 shows the Microsoft

More information

Section 4 Working with Text

Section 4 Working with Text ECDL Section 4 Working with Text Section 4 Working with Text By the end of this section you should be able to: Start and close the WordPad program Recognise common program features Create text-based documents

More information