Lec4 ExcelPart2
Lec4 ExcelPart2
Introduction to Excel
Macros
Lab 15
1 Copyright © 2008Copyright
Pearson © 2008 Pearson
Prentice Hall. AllPrentice
rig Hall. All rights
hts reserved. Copyright
Copyright©© 2008
2008 Prentice-Hall.
reserved.
Pearson All All
Prentice Hall. rights reserved.
rights reserved. 1
Objectives
Create a macro
Create macro buttons
Work with macro security
Understand the basics of VBA
2
Macros and VBA
A macro is a set of instructions that tells
Excel which commands to execute
3
Developing an Excel
Application
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
4
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
5
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
6
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
7
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
8
Method2: Visual Basic Editor
9
Method2: Assign Macro to Button
10
Method 2: Create a Macro with Editor:
Procedure
11
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
12
functions
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
13
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.
14
Method 2: Working with the Visual
Basic Editor
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
Project Code
Explorer
Code
15 Window
Method 2: Working with the Visual
Basic Editor
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
16
Method 2: Working with the Visual
Basic Editor
ed ure
P roc
Sub Name
Comment
Line
VBA
Command
End of
Procedure
Sheet Macro
Modules
17
Method 2: Working with Sub
Procedures
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
18
Method 2: Working with Sub
Procedures
Welcome_Click
Notice the
highlighted
text. Code
window is
syntax
sensitive Line separate
procedures
19
Method 2: Referring to Objects
VBA is an object-oriented programming
language, in which tasks are performed by
manipulating objects
20
Method 2: Referring to Objects
Objects are often grouped into collections,
which are themselves objects, called
collection objects
21
Referring to Objects
22
Method 2: Applying Methods
A method is an action that can be
performed on an object, such as closing a
workbook or printing the contents of a
worksheet
23
Method 2: Working with Variables
and Values
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 “&”,
“%”, “\”)
24
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!!!)
Function sumNo(x, y)
sumNo = x + y The function returns sum
of two input numbers,
End Function 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
The Variables Advantage by
Example
Sub ParseValue()
Dim sWord as String
Dim iNumber as Integer
Dim rCell as Range
End Sub
29
Method 2: Working with
Conditional Statements
30
Method 2: Working with Conditional
Statements
31
Method 2: Working with
Conditional Statements
32
Saving Excel Macros
33