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

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

Dec 29, 2015

Download

Documents

Sibyl Phillips
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 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 Pearson Prentice Hall. All rights reserved.

1

Microsoft Office Excel

Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1Copyright © 2008 Pearson Prentice Hall. All rights

reserved. 11Copyright © 2008 Prentice-Hall. All rights reserved.

Introduction to Excel Macros

Lab 15

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

2

Objectives

Create a macroCreate macro buttonsWork with macro security Understand the basics of VBA

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

3

Macros and VBAA 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

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

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

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

5

Work with Macro SecurityThe 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

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

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

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

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 macroTake your time and be sure the action is correctTry to ensure your macros are broad enough to

apply to a variety of situationsThe Visual Basic Editor (VBE) is used to create,

edit, execute, and debug Excel macros

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

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

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

9

Method2: Visual Basic Editor

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

10

Method2: Assign Macro to Button

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

11

Method 2: Create a Macro with Editor: Procedure

Name your Macro4 and click New to open visual basic editor

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

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

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

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

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

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.

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

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

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

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 macrosThe Code window displays the VBA macro

code associated with any item in Project Explorer

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

Method 2: Working with the Visual Basic Editor

17

Sheet Macro Modules

Comment Line

VBA Command

End of Procedure

Sub Procedure

Name

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

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

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

Method 2: Working with Sub Procedures

19

Line separate procedures

Welcome_Click

Notice the highlighted text. Code window is syntax sensitive

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

Method 2: Referring to Objects

20

VBA is an object-oriented programming language, in which tasks are performed by manipulating objects

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

Method 2: Referring to Objects

21

Objects are often grouped into collections, which are themselves objects, called collection objects

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

Referring to Objects

22

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

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

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

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 typeA variable name

Must start with letter and can’t contain spaces and special characters (such as “&”, “%”, “\”)

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

Method 2: Using Variables

Declaring VariablesFormat: Dim varibaleName AS dataTypeExamples:

Dim myText As String Dim myNum As IntegerDim myObj As Range

The default value of any numeric variable is zeroany string variable – “” (empty string)an Object variable – is nothing (still the declaration will store space for

the object!!!)

Dim myRange as RangeSet myRange = Range(“A1:A10”)

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

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

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

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

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

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

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

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

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

Method 2: Working with Conditional Statements

30

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

Method 2: Working with Conditional Statements

31

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

Method 2: Working with Conditional Statements

32

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

Saving Excel Macros

33