Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11.

Similar presentations


Presentation on theme: "Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11."— Presentation transcript:

1 Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11 Copyright © 2008 Prentice-Hall. All rights reserved. Introduction to Excel Macros Lab 15

2 2 Objectives Create a macro Create macro buttons Work with macro security Understand the basics of VBA

3 3 Macros and VBA A macro is a set of instructions that tells Excel which commands to execute Visual Basic a programming language used to create macros You do not have to be a programmer to write macros

4 Developing an Excel Application 4 If the Excel Developer tab is not on the Ribbon, click the Office Button, click the Options button, click Customize Ribbon in the Excel Options dialog box (if necessary), click the Developer tab in the Ribbon check box to insert a check mark, and then click the OK button

5 5 Work with Macro Security The proliferation of Excel macro viruses has made it a dangerous operation to open spreadsheets that contain viruses To counter this threat, when you open an Excel workbook that contains macros, Excel automatically disables the macros and displays the Security Warning Macros have been disabled message Click Options to open the Microsoft Office Security Options dialog box

6 6 Method1: Create a Macro with the Macro Recorder To use the recorder, click the Macros down arrow in the Macros group and select Record Macros From that point until you stop recording, every command you execute will be stored by the recorder

7 7 Method1: Create a Macro with the Macro Recorder The Macro Recorder has some issues: Everything you do once you begin recording a macro becomes part of the macro Take your time and be sure the action is correct Try to ensure your macros are broad enough to apply to a variety of situations The Visual Basic Editor (VBE) is used to create, edit, execute, and debug Excel macros

8 8 Method1: Create a Macro with the Macro Recorder To create a macro: 1. Click Record Macro in the Macros down arrow in the Macros group of the View tab to open the Record Macro dialog box 2. Type a name for the macro in the Macro name text box 3. Create a keyboard shortcut, if desired, for your macro in the Shortcut key box 4. Select a location to store the macro from the Store macro in drop-down arrow 5. Click OK to start recording the Macro

9 9 Method2: Visual Basic Editor

10 10 Method2: Assign Macro to Button

11 11 Method 2: Create a Macro with Editor: Procedure  Name your Macro4 and click New to open visual basic editor

12 12 Method 2: Using a User Defined Function Click in Cell you want to use the function Click on fx to open Insert Function Click on drop down menu to select User Defined User defined functions created in VB will be listed

13 13 Method 2: Create a Macro with Editor Click on the Developer tab, then click on Visual Basic to open Editor. You can being to code either a function or a sub procedure

14 14 Method 2: Inputting Function Arguments You will be prompted for the arguments required for the function. Just like Excel SUM function required a values or a cell range, your function could require values or cells as well.

15 Method 2: Working with the Visual Basic Editor 15 In the Code group on the Developer tab, click the Macros button Click the macro name in the Macro name box, if necessary, and then click the Edit button Code Code Window Project Explorer

16 Method 2: Working with the Visual Basic Editor 16 A project is a collection of macros, worksheets, data- entry forms, and other items that make up the customized application you’re trying to create An object is any element within the Excel working environment such as a worksheet, cell, workbook, or even Excel itself A module is a collection of VBA macros The Code window displays the VBA macro code associated with any item in Project Explorer

17 Method 2: Working with the Visual Basic Editor 17 Sheet Macro Modules Comment Line VBA Command End of Procedure Sub Procedure Name

18 Method 2: Working with Sub Procedures 18 Procedure – a group of ordered statements enclosed by Sub and End Sub Function – the same as a procedure, but also returns some value and is closed between Function and End Function key words Module – collection of logically related procedures grouped together Syntax refers to the set of rules that specify how you must enter certain commands so that VBA interprets them correctly A comment is a statement that describes the behavior or purpose of a procedure, but does not perform any action

19 Method 2: Working with Sub Procedures 19 Line separate procedures Welcome_Click Notice the highlighted text. Code window is syntax sensitive

20 Method 2: Referring to Objects 20 VBA is an object-oriented programming language, in which tasks are performed by manipulating objects

21 Method 2: Referring to Objects 21 Objects are often grouped into collections, which are themselves objects, called collection objects

22 Referring to Objects 22

23 Method 2: Applying Methods 23 A method is an action that can be performed on an object, such as closing a workbook or printing the contents of a worksheet

24 Method 2: Working with Variables and Values 24 A variable is a named element in a program that can be used to store and retrieve information Every variable is identified by a unique variable name Dim variable as type A variable name Must start with letter and can ’ t contain spaces and special characters (such as “ & ”, “ % ”, “ \ ” )

25 Method 2: Using Variables Declaring Variables Format: Dim varibaleName AS dataType Examples: Dim myText As String Dim myNum As Integer Dim myObj As Range The default value of any numeric variable is zero any string variable – “” (empty string) an Object variable – is nothing (still the declaration will store space for the object!!!) Dim myRange as Range Set myRange = Range( “ A1:A10 ” )

26 Method 2: Procedure & Function Examples Sub ShowTime() Range("C1") = 19 End Sub Function sumNo(x, y) sumNo = x + y End Function The procedure places the value 19 inside cell C1 The function returns sum of two input numbers, whose values are in the parameter variables x & y To assign a value to a Numeric or String type Variable, you simply use your Variable name, followed by the equals sign (=) and then the String or Numeric

27 The Variables Advantage by Example Sub NoVariable() Range("A1").Value = Range("B2").Value Range("A2").Value = Range("B2").Value * 2 Range("A3").Value = Range("B2").Value * 4 Range("B2").Value = Range("B2").Value * 5 End Sub Sub WithVariable() Dim iValue as Integer iValue =Range("B2").Value Range("A1").Value = iValue Range("A2").Value = iValue * 2 Range("A3").Value = iValue * 4 Range("B2").Value = iValue * 5 End Sub

28 Variables Assignment – cont. Sub ParseValue() Dim sWord as String Dim iNumber as Integer Dim rCell as Range Set rCell = Range("A1") sWord = Range("A1").Text iNumber = Range("A1").Value End Sub

29 Method 2: Message Box Example 29 Sub MyProcedure() ‘These are my comments about my procedure MsgBox ("This Pops Open a Message Box") Range("A3").Value = "Input Text to Cell A3" Range("A5").Value = 34 End Sub

30 Method 2: Working with Conditional Statements 30

31 Method 2: Working with Conditional Statements 31

32 Method 2: Working with Conditional Statements 32

33 Saving Excel Macros 33


Download ppt "Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 11."

Similar presentations


Ads by Google