Microsoft Office 2013 - Illustrated Unit P: Programming with Excel
Objectives View VBA code Analyze VBA code Write VBA code Add a conditional statement Microsoft Office Excel 2013 - Illustrated
Objectives (cont’d.) Prompt the user for data Debug a macro Create a main procedure Run a main procedure Microsoft Office Excel 2013 - Illustrated
View VBA Code VBA (Visual Basic for Applications): programming language for macros Visual Basic Editor: tool within Excel Enter and edit VBA code Procedure: sequence of VBA statements Microsoft Office Excel 2013 - Illustrated
View VBA Code (cont’d.) Procedure header: defines the procedure’s type, name, and arguments Arguments: variables used by other procedures that the main procedure might run Microsoft Office Excel 2013 - Illustrated
View VBA Code (cont’d.) Comments: notes explaining code; displayed in green Keywords: recognized parts of VBA programming language; displayed in blue Sub procedure: series of VBA statements; perform an action but do not return a value Microsoft Office Excel 2013 - Illustrated
View VBA Code (cont’d.) Microsoft Office Excel 2013 - Illustrated
View VBA Code (cont’d.) Understanding the Visual Basic Editor Module: equivalent of a worksheet Stores macro procedures Stored in a workbook/project with worksheets Project: collection of all workbook procedures Microsoft Office Excel 2013 - Illustrated
View VBA Code (cont’d.) Understanding the Visual Basic Editor (cont’d.) Project Explorer: displays a list of all open projects (or workbooks) and the worksheets and modules they contain Code window: displays the selected module’s procedures Properties window: displays a list of characteristics related to the module Microsoft Office Excel 2013 - Illustrated
Analyze VBA Code Object: every element Range object: cell or a range of cells Statement: line of code Property: attribute defining an object’s characteristic Procedure footer: last line of VBA code indicating end of procedure Microsoft Office Excel 2013 - Illustrated
Analyze VBA Code (cont’d.) Microsoft Office Excel 2013 - Illustrated
Write VBA Code Open the Visual Basic Editor, add a module to the workbook, and write the code Syntax: programming language’s formatting rules applied to code writing Microsoft Office Excel 2013 - Illustrated
Write VBA Code (cont’d.) Microsoft Office Excel 2013 - Illustrated
Write VBA Code (cont’d.) Entering code using AutoComplete VBA Editor provides AutoComplete List of words: can be used in the macro statement and matches what you type Appears after you press [.] (period) Select a word in the list: double-click it or press [Tab] Microsoft Office Excel 2013 - Illustrated
Add a Conditional Statement If...Then…Else statement: code entered in a procedure to perform an action based on a certain condition or set of conditions Microsoft Office Excel 2013 - Illustrated
Add a Conditional Statement (cont’d.) Microsoft Office Excel 2013 - Illustrated
Prompt the User for Data Function: predefined procedure that returns a value InputBox function’s required elements: object.InputBox(“prompt”) Prompt: user-friendly message that appears in input box Variable: memory location for temporarily information storage Microsoft Office Excel 2013 - Illustrated
Prompt the User for Data (cont’d.) Microsoft Office Excel 2013 - Illustrated
Prompt the User for Data (cont’d.) Naming variables (in VBA) Must begin with a letter Uppercase or lowercase letters allowed Up to 255 characters long Each name must be unique Microsoft Office Excel 2013 - Illustrated
Debug a Macro Bug: error in code Debug: locate and correct error Step Into: opens Visual Basic Editor with yellow statement selector positioned on the procedure’s first statement Press [F8] to step to successive statements Microsoft Office Excel 2013 - Illustrated
Debug a Macro (cont’d.) Statement selector positioned on first procedure statement Microsoft Office Excel 2013 - Illustrated
Debug a Macro (cont’d.) Adding security to your macro projects Add a digital signature: guarantees the project hasn’t been altered since it was signed Sign macros only after testing and ready to distribute Microsoft Office Excel 2013 - Illustrated
Create a Main Procedure Main procedure: several macros combined into a single procedure Runs multiple procedures in sequence Call statement syntax: Call procedurename Place call statements in sequence in the main procedure Microsoft Office Excel 2013 - Illustrated
Create a Main Procedure (cont’d.) Microsoft Office Excel 2013 - Illustrated
Create a Main Procedure (cont’d.) Copying a macro to another workbook Open both the source and destination Excel workbooks Open the Visual Basic Editor and verify that macros are enabled In Project Explorer, drag the module that will be copied from the source workbook to the destination workbook Microsoft Office Excel 2013 - Illustrated
Create a Main Procedure (cont’d.) Writing and documenting VBA code Make code readable by indenting and adding comments Comments: begin with an apostrophe Microsoft Office Excel 2013 - Illustrated
Run a Main Procedure Click the name of the main procedure in the Macro dialog box and click Run Main procedure: runs several macros in sequence Microsoft Office Excel 2013 - Illustrated
Run a Main Procedure (cont’d.) Running a macro using a button Assign a macro to a button on a worksheet Create a button, right-click on the button, click Assign macro, and click the desired macro Label the button with descriptive text Microsoft Office Excel 2013 - Illustrated