Excel and VBA

41 downloads 405 Views 635KB Size Report
John Walkenbach, Excel 2007 VBA Programming For. Dummies. Wiley Publishing, Inc. 2007. Greg Harvey, Microsoft Excel 2010 All-In-One for. Dummies.
23 noviembre 2011

Excel and VBA Unit 5

1

Office Automation 2011/2012

2

23 noviembre 2011

Recording Macros › 

With Excel’s macro recorder, you can create many of the utility-type macros that help you to perform the repetitive tasks necessary for creating and editing your worksheets and charts

› 

When you turn on the macro recorder, the macro recorder records all your actions in the active worksheet or chart sheet as you make them

› 

Note that the macro recorder doesn’t record the keystrokes or mouse actions that you take to accomplish an action — only the VBA code required to perform the action itself

› 

The macros that you create with the macro recorder can be stored either as part of the current workbook, in a new workbook, or in a special, globally available Personal Macro Workbook named PERSONAL.XLSB Office Automation 2011/2012

3

23 noviembre 2011

Recording Macros Developer Tab › 

The Ribbon’s View tab contains a Macros command button to which a drop- down menu containing the following three options is attached: › 

View Macros: Opens the Macro dialog box where you can select a macro to run or edit.

› 

Record Macro: Opens the Record Macro dialog box where you define the settings for your new macro and then start the macro recorder; this is the same as clicking the Record Macro button on the Status bar.

› 

Use Relative References: Uses relative cell addresses when recording a macro, making the macro more versatile by enabling you to run it in areas of a worksheet other than the ones originally used in the macro’s recording.

Office Automation 2011/2012

4

23 noviembre 2011

Recording Macros Our first example › 

Open the Excel workbook that contains the worksheet data or chart you want your macro to work with

› 

Click the Record Macro option on the Macros command button on the View tab

› 

Replace the Macro1 temporary macro name by entering your name for the macro in the Macro Name text box.

› 

(Optional) Click the Shortcut Key text box and then enter the letter of the alphabet that you want to assign to the macro

› 

Click the Personal Macro Workbook, New Workbook

› 

(Optional) Click the Description list box and then insert a brief description

› 

(Optional) Click the Use Relative References option if you want to be able to play back the macro anywhere in the worksheet

› 

Select the cells, enter the data, and choose the Excel commands required to perform the tasks that you want recorded Office Automation 2011/2012

5

23 noviembre 2011

Recording Macros Macro Security › 

Excel 2010 uses a system called Microsoft Authenticode that enables developers to authenticate their macro projects or add-ins created with Visual Basic for Applications by a process referred to as digital signing

› 

When you run a macro in your worksheet that’s not saved in the trusted locations on your computer, such as the Templates and XLSTART folder in your user area on the computer, Excel checks to see if the macro is digitally signed and that the signature is both valid and current

› 

If the program cannot verify a macro’s digital signature (perhaps because it doesn’t have one) or the trustworthiness of its macro publisher, the pro- gram then displays a security alert on the message bar underneath the Excel Ribbon Office Automation 2011/2012

6

23 noviembre 2011

Recording Macros Macro Security › 

The Macro Settings tab of the Trust Center dialog box also contains these other option buttons you can select: › 

Disable All Macros without Notification to disable all macros not saved in one of your computer’s trusted locations and all security alerts so that you and the other users of the worksheet have no way to ignore the alert and run the macro.

› 

Disable All Macros with Notification (the default) to control the disabling of macros not saved in one of your computer’s trusted locations and security alerts

› 

Disable All Macros except Digitally Signed Macros to automatically enable digitally signed macros and to disable all macros that are not digitally signed without notification

› 

Enable All Macros (Not Recommended; Potentially Dangerous Code Can Run) Office Automation 2011/2012

7

23 noviembre 2011

Recording Macros Macro Security › 

To change the trusted locations on your computer, you need to click the Trusted Locations tab in the Trust Center dialog box: › 

Add New Location: Use this command button to open the Microsoft Office Trusted Location dialog box where you select a new folder on your computer as a trusted location either by entering its directory path name in the Path text box or selecting it with the Browse button

› 

Allow Trusted Locations on My Network (Not Recommended): Select this check box so that you can designate folders to which you have access on your local network as trusted locations using the Add New Location command button

› 

Disable All Trusted Locations: Select this check box to immediately dis- able all the folders currently designated as trusted locations and allow only macros from publishers designated as trustworthy to run in Excel. Office Automation 2011/2012

8

23 noviembre 2011

Recording Macros Adding your macros to a custom tab › 

Click File➪Options and then click the Custom Ribbon button in the Excel Options dialog box

› 

Click Macros in the Choose Commands From drop-down list box on the left.

› 

Click the name of the custom group on the custom tab to which you want to add the macro in the Main Tabs list box on the right

› 

In the Choose Commands From list box on the left, click the name of the macro you want to add to the custom group now selected in the Main Tabs list box on the right.

› 

Click the Add button to add the selected macro to the selected custom group on your custom tab and then click the OK button to close the Excel options dialog box. Office Automation 2011/2012

9

23 noviembre 2011

Recording Macros Adding your macros to custom buttons on the Quick Access toolbar › 

Click the Customize Quick Access Toolbar button at the end of the Quick Access toolbar and then click More Commands on the drop- down menu to open the Quick Access Toolbar tab of the Excel Options dialog box.

› 

Click Macros in the Choose Commands From drop-down list box. Excel lists the names of the all the macros created in the current workbook and saved in the PERSONAL.XLSB workbook in the Choose Commands From list box.

› 

Click the name of the macro to add to a custom button on the Quick Access toolbar in the Choose Commands From list box and then click the Add button.

› 

Click OK to close the Excel Options dialog box. Office Automation 2011/2012

10

23 noviembre 2011

Recording Macros Examples › 

A macro to include table headers using absolute and relative references

› 

A macro to help sort data out

› 

A macro to copy, paste and sort data

Let’s GO! Office Automation 2011/2012

11

23 noviembre 2011

Visual Basic for Excel First view › 

You perform actions in VBA by writing (or recording) code in a VBA module

› 

A VBA module consists of Sub procedures Sub AddEmUp() Sum = 1 + 1 MsgBox “The answer is “ & Sum End Sub

› 

A VBA module can also have Function procedures Function AddTwo(arg1, arg2) AddTwo = arg1 + arg2 End Function

› 

VBA manipulates objects

› 

Objects are arranged in a hierarch Office Automation 2011/2012

12

23 noviembre 2011

Visual Basic for Excel First view › 

Objects of the same type form a collection

› 

You refer to an object by specifying its position in the object hierarchy, using a dot (that is, a period) as a separator Application.Workbooks(“Book1.xlsx”) Application.Workbooks(“B1.xlsx”).Worksheets(“S1”) Application.Workbooks(“Book1.xlsx”).Worksheets _ (“Sheet1”).Range(“A1”)

› 

If you omit specific references, Excel uses the active objects Worksheets(“Sheet1”).Range(“A1”)

› 

Objects have properties. You can think of a property as a setting for an object. For example, a Range object has such properties as Value and Address

Office Automation 2011/2012

13

23 noviembre 2011

Visual Basic for Excel First view › 

You refer to a property of an object by combining the object name with the property name, separated by a dot Worksheets(“Sheet1”).Range(“A1”).Value

› 

You can assign values to variables Interest = Worksheets(“Sheet1”).Range(“A1”).Value

› 

Objects have methods. A method is an action Excel performs with an object. For example, one of the methods for a Range object is ClearContents

› 

You specify a method by combining the object with the method, separated by a dot Worksheets(“Sheet1”).Range(“A1”).ClearContents

› 

VBA includes all the constructs of modern programming languages, including arrays and looping Office Automation 2011/2012

14

23 noviembre 2011

Visual Basic for Excel First view

Office Automation 2011/2012

15

23 noviembre 2011

Visual Basic for Excel Excel’s Object Model › 

Object Hierarchy › 

Application: Addin, CommandBar, Window, Workbook, WorksheetFuntions

› 

Workbook: Chart, Name, VBProject, Window, Worksheet

› 

Worksheet: Comment, Hyperlink, Name, Outline, PageSetup, PivotTable, Range

Office Automation 2011/2012

16

23 noviembre 2011

Visual Basic for Excel Excel’s Object Model › 

Collections › 

Collections are another key concept in VBA programming. A collection is a group of objects of the same type. And to add to the confusion, a collection is itself an object. ! Worksheets: A collection of all Worksheet objects contained in a particular Workbook object ! Charts: A collection of all Chart objects (chart sheets) contained in a particular Workbook object ! Sheets: A collection of all sheets (regardless of their type) contained in a particular Workbook object

› 

EXAMPLE: Change the color index of each Worksheet Office Automation 2011/2012

17

23 noviembre 2011

Visual Basic for Excel Excel’s Object Model › 

Object Methods › 

In addition to properties, objects have methods. A method is an action you perform with an object. Sub CalcCell() Worksheets(“Sheet1”).Range(“A1”).Calculate End Sub

› 

Most methods also take one or more arguments. You place the arguments for a method after the method, separated by a space. Multiple arguments are separated by a comma. Sub CopyOne() Worksheets(“Sheet1”).Activate Range(“A1”).Copy Range(“B1”) End Sub Office Automation 2011/2012

18

23 noviembre 2011

Visual Basic for Excel Excel’s Object Model › 

Procedures and Functions › 

Executing the procedure from another procedure Sub NewSub() Call CubeRoot End Sub

› 

Executing Function procedures Function CubeRoot(number) CubeRoot = number ^ (1/3) End Function

› 

Calling the function from a Sub procedure Sub CallerSub() Ans = CubeRoot(125) MsgBox Ans End Sub

› 

Calling a function from a worksheet formula =CubeRoot(1728) Office Automation 2011/2012

19

23 noviembre 2011

Visual Basic for Excel Excel’s Object Model › 

Examples ›  Static

variables to track of the number of times a procedure is executed

›  Count

all values in a range

›  Change ›  Display

the color of particular ranges

the integer part of a number

›  Use

a for-next loop to enter a random number into 100 cells

Office Automation 2011/2012

20

23 noviembre 2011

Bibliography John Walkenbach, Excel 2007 VBA Programming For Dummies. Wiley Publishing, Inc. 2007 Greg Harvey, Microsoft Excel 2010 All-In-One for Dummies. . Wiley Publishing, Inc. 2010 Paul Lomax. VB & VBA in a nutshell: the language. O’Reilly. 1998. John Paul Mueller. VBA For Dummies. 2007 Office Automation 2011/2012