microsoft excel 2007 - illustrated programming with excel
TRANSCRIPT
![Page 1: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/1.jpg)
Microsoft Excel 2007 -Microsoft Excel 2007 -IllustratedIllustrated
Programming with ExcelProgramming with Excel
![Page 2: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/2.jpg)
2Microsoft Office Excel 2007 - Illustrated
• View VBA codeView VBA code
• Analyze VBA codeAnalyze VBA code
• Write VBA codeWrite VBA code
• Add a conditional statementAdd a conditional statement
ObjectivesObjectives
![Page 3: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/3.jpg)
3Microsoft Office Excel 2007 - Illustrated
• Prompt the user for dataPrompt the user for data
• Debug a macroDebug a macro
• Create a main procedureCreate a main procedure
• Run a main procedureRun a main procedure
ObjectivesObjectives
![Page 4: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/4.jpg)
4Microsoft Office Excel 2007 - Illustrated
Unit IntroductionUnit Introduction
• Excel macros are written in a Excel macros are written in a programming language called Visual programming language called Visual Basic for Applications, or Basic for Applications, or VBAVBA• Create a macro with the Excel macro Create a macro with the Excel macro
recorderrecorder• The recorder writes the VBA The recorder writes the VBA
instructions for youinstructions for you• Enter VBA instructions manuallyEnter VBA instructions manually
• Sequence of VBA statements is called Sequence of VBA statements is called a a procedureprocedure
![Page 5: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/5.jpg)
5Microsoft Office Excel 2007 - Illustrated
Viewing VBA CodeViewing VBA Code
• View existing VBA code to learn the View existing VBA code to learn the languagelanguage• To view VBA code, open the Visual Basic To view VBA code, open the Visual Basic
EditorEditor• Contains a Project Explorer window, a Properties Contains a Project Explorer window, a Properties
window, and a Code windowwindow, and a Code window
• VBA code appears in the Code windowVBA code appears in the Code window• The first line of a procedure is called the The first line of a procedure is called the procedure procedure
headerheader• Items displayed in blue are Items displayed in blue are keywordskeywords• Green notes explaining the code are called Green notes explaining the code are called
commentscomments
![Page 6: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/6.jpg)
6Microsoft Office Excel 2007 - Illustrated
Viewing VBA Code (cont.)Viewing VBA Code (cont.)
Comments
Procedure header
Keyword
![Page 7: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/7.jpg)
7Microsoft Office Excel 2007 - Illustrated
Viewing VBA Code (cont.)Viewing VBA Code (cont.)
• Understanding the Visual Basic Understanding the Visual Basic EditorEditor• A A modulemodule is the Visual Basic equivalent is the Visual Basic equivalent
of a worksheetof a worksheet• Store macro proceduresStore macro procedures• A module is stored in a workbook, or A module is stored in a workbook, or
projectproject, along with worksheets, along with worksheets• View and edit modules in the Visual Basic View and edit modules in the Visual Basic
EditorEditor
![Page 8: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/8.jpg)
8Microsoft Office Excel 2007 - Illustrated
Analyzing VBA CodeAnalyzing VBA Code
• Analyzing VBA codeAnalyzing VBA code• Every element of Excel, including a Every element of Excel, including a
range, is considered an range, is considered an objectobject• A A range objectrange object represents a cell or a represents a cell or a
range of cellsrange of cells• A A propertyproperty is an attribute of an object is an attribute of an object
that defines one of the object’s that defines one of the object’s characteristics, such as sizecharacteristics, such as size
• The last line in VBA code is the The last line in VBA code is the procedure footerprocedure footer
![Page 9: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/9.jpg)
9Microsoft Office Excel 2007 - Illustrated
Analyzing VBA Code (cont.)Analyzing VBA Code (cont.)
Selects range object
cell A2
Applies bold formatting to range A3:F3
Sets width of columns B-F
to AutoFit
![Page 10: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/10.jpg)
10Microsoft Office Excel 2007 - Illustrated
Writing VBA CodeWriting VBA Code
• To write your own code, open the To write your own code, open the Visual Basic Editor and add a module Visual Basic Editor and add a module to the workbookto the workbook• You must follow the formatting rules, or You must follow the formatting rules, or
syntaxsyntax, of the VBA programming , of the VBA programming language exactlylanguage exactly
• A misspelled keyword of variable name A misspelled keyword of variable name will cause a procedure to failwill cause a procedure to fail
![Page 11: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/11.jpg)
11Microsoft Office Excel 2007 - Illustrated
Writing VBA Code (cont.)Writing VBA Code (cont.)
Comments begin with
apostrophes
Information between
quotes will be inserted in the active
cell
![Page 12: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/12.jpg)
12Microsoft Office Excel 2007 - Illustrated
Writing VBA Code (cont.)Writing VBA Code (cont.)
• Entering code using AutoCompleteEntering code using AutoComplete• To assist you in entering the VBA code, To assist you in entering the VBA code,
the Editor often displays a list of words the Editor often displays a list of words that can be used in the macro that can be used in the macro statementstatement
• Typically the list appears after you Typically the list appears after you press period [.]press period [.]
![Page 13: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/13.jpg)
13Microsoft Office Excel 2007 - Illustrated
Adding a Conditional Adding a Conditional StatementStatement
• Sometimes you may want a Sometimes you may want a procedure to take an action based on procedure to take an action based on a certain condition or set of a certain condition or set of conditionsconditions• One way to add this type of statement One way to add this type of statement
is by using an is by using an If...Then…Else statementIf...Then…Else statement• The syntax for this statement is: If The syntax for this statement is: If
conditioncondition then then statementsstatements Else [ Else [else else statementsstatements]]
![Page 14: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/14.jpg)
14Microsoft Office Excel 2007 - Illustrated
Adding a Conditional Adding a Conditional Statement (cont.)Statement (cont.)
Elements of the If…then…Else statement appear in blue
![Page 15: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/15.jpg)
15Microsoft Office Excel 2007 - Illustrated
Prompting the User for DataPrompting the User for Data
• When automating routine tasks, When automating routine tasks, sometimes you need to pause a sometimes you need to pause a macro for user inputmacro for user input• Use the VBA InputBox function to Use the VBA InputBox function to
display a dialog box that prompts the display a dialog box that prompts the user for informationuser for information
• A A functionfunction is a predefined procedure is a predefined procedure that returns a valuethat returns a value
![Page 16: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/16.jpg)
16Microsoft Office Excel 2007 - Illustrated
Prompting the User for Data Prompting the User for Data (cont.)(cont.)
This text will appear in a dialog box
Comment points out
error in next line of the procedure
![Page 17: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/17.jpg)
17Microsoft Office Excel 2007 - Illustrated
Debugging a MacroDebugging a Macro
• When a macro procedure does not When a macro procedure does not run properly, it can be due to an run properly, it can be due to an error, called a error, called a bugbug, in the code, in the code• To help you find bugs in a procedure, To help you find bugs in a procedure,
the Visual Basic Editor steps through the Visual Basic Editor steps through the procedure’s code one line at a timethe procedure’s code one line at a time
• When you locate an error, you can When you locate an error, you can debugdebug, or correct it, or correct it
![Page 18: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/18.jpg)
18Microsoft Office Excel 2007 - Illustrated
Debugging a Macro (cont.)Debugging a Macro (cont.)
Indicates that the
LeftFooter variable is
empty
![Page 19: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/19.jpg)
19Microsoft Office Excel 2007 - Illustrated
Creating a Main ProcedureCreating a Main Procedure
• Combine several macros that you Combine several macros that you routinely run together into a routinely run together into a procedureprocedure• This is a This is a main proceduremain procedure• To create a main procedure, type a Call To create a main procedure, type a Call
statement for each procedure you want statement for each procedure you want to runto run
![Page 20: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/20.jpg)
20Microsoft Office Excel 2007 - Illustrated
Creating a Main Procedure Creating a Main Procedure (cont.)(cont.)
MainProcedure calls each
procedure in the order shown
![Page 21: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/21.jpg)
21Microsoft Office Excel 2007 - Illustrated
Running a Main ProcedureRunning a Main Procedure
• Running a main procedure allows Running a main procedure allows you to run several macros in you to run several macros in sequencesequence• Run a main procedure as you would Run a main procedure as you would
any other macroany other macro
![Page 22: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/22.jpg)
22Microsoft Office Excel 2007 - Illustrated
Running a Main Procedure Running a Main Procedure (cont.)(cont.)
Current Modulebutton
Printing Macro Procedures
Current Projectbutton
![Page 23: Microsoft Excel 2007 - Illustrated Programming with Excel](https://reader035.vdocuments.site/reader035/viewer/2022062217/5697bfa21a28abf838c964df/html5/thumbnails/23.jpg)
23Microsoft Office Excel 2007 - Illustrated
SummarySummary
• Learn by viewing and analyzing VBA codeLearn by viewing and analyzing VBA code• Write VBA code using the Visual Basic Write VBA code using the Visual Basic
EditorEditor• Use If..Then..Else statements for Use If..Then..Else statements for
conditional actionsconditional actions• Prompt user for data to automate input Prompt user for data to automate input
taskstasks• Use the “Step Into” feature of the Visual Use the “Step Into” feature of the Visual
Basic Editor to debug macrosBasic Editor to debug macros• Use Main procedures to combine several Use Main procedures to combine several
macrosmacros