excel 2007 unit p

23
Microsoft Excel 2007 - Microsoft Excel 2007 - Illustrated Illustrated Programming with Programming with Excel Excel

Upload: raja-waseem-akhtar

Post on 30-Oct-2014

1.938 views

Category:

Technology


4 download

DESCRIPTION

These Slides are shared for Education purposes only,

TRANSCRIPT

Page 1: Excel 2007 Unit P

Microsoft Excel 2007 -Microsoft Excel 2007 -IllustratedIllustrated

Programming with ExcelProgramming with Excel

Page 2: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

6Microsoft Office Excel 2007 - Illustrated

Viewing VBA Code (cont.)Viewing VBA Code (cont.)

Comments

Procedure header

Keyword

Page 7: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

18Microsoft Office Excel 2007 - Illustrated

Debugging a Macro (cont.)Debugging a Macro (cont.)

Indicates that the

LeftFooter variable is

empty

Page 19: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

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: Excel 2007 Unit P

22Microsoft Office Excel 2007 - Illustrated

Running a Main Procedure Running a Main Procedure (cont.)(cont.)

Current Modulebutton

Printing Macro Procedures

Current Projectbutton

Page 23: Excel 2007 Unit P

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