Tutorial 8: Developing an Excel Application

advertisement
Tutorial 7:
XP
Developing an Excel Application
New Perspectives on Microsoft Excel 2013
1
Objectives:
XP
• Create an application
• Create , edit , and delete defined names for cells and
ranges
• Paste a list of defined names as documentation
• Use defined names in formulas
• Add defined name to existing formulas
• Create validation rules for data entry
• Protect the content of worksheets and workbook
• Add , edit , and delete comments
New Perspectives on Microsoft Excel 2013
2
Objectives:
•
•
•
•
•
•
XP
Macro viruses and excel security features
Add the developer tab to the ribbon
Create and run a macro
Edit a macro using the visual basic editor
Assign a macro to a keyboard shortcut button
Save and open a workbook in macro-enabled format
New Perspectives on Microsoft Excel 2013
3
Visual Overview
New Perspectives on Microsoft Excel 2013
XP
4
Excel Application and Defined NamesXP
New Perspectives on Microsoft Excel 2013
5
Planning an Excel Application
XP
• A spreadsheet written or tailored to meet
specific needs
• The interface helps others use it
• Typically includes reports and charts, a data
entry area, a custom interface, instructions,
and documentation
New Perspectives on Microsoft Excel 2013
6
Naming Cells and Ranges
XP
• Use a defined name to:
– Assign a meaningful, descriptive name to a
cell or range
– Quickly navigate within a workbook to the
cell with the defined name
– Create a more descriptive formula
New Perspectives on Microsoft Excel 2013
7
Creating Defined Names
XP
• Must begin with a letter or an underscore
• Can include letters, numbers, periods and
underscores, but not other symbols or spaces
• Cannot be a valid cell address, function name,
or reserved word
• Can include as many as 255 characters
– Short, meaningful names (5–15 characters)
are more practical
• Are not case sensitive
New Perspectives on Microsoft Excel 2013
8
Creating Defined Names
XP
• Use the Name box to create defined names
Name Box
• Select the cell or range to which you want to assign a name
• Click in the name box , type the name and then press enter (or In the
FORMULA tab , in the defined Names Group click the define name
button , type a name in the Name box and then click the OK button)
New Perspectives on Microsoft Excel 2013
9
Creating Defined Names
XP
• Use the Selection dialog box to create defined
names by selection
• Select the range with labels to which you want to assign a name
• On the FURMULA tab , click the create from Selection button
• Specify whether to create the ranges based on the top row , bottom row ,
left column or right in the list
• Click the OK button
New Perspectives on Microsoft Excel 2013
10
Editing and Deleting Defined
Names
XP
• On the FURMULA tab , click the Name Manager
New Perspectives on Microsoft Excel 2013
11
Editing and Deleting Defined
Names
XP
• The edit dialog box (you can change the name
and its reference cell or range )
New Perspectives on Microsoft Excel 2013
12
Using the Paste Names CommandXP
When a workbook contains many defined names , it can be helpful to list all
of the names and their cell address in the workbook documentation Use the
Paste Names Command to generate a list of names
To create a list of defined names in the worksheet:
• Click the formulas Tab , click the Use in Formula button
• Click paste Names , the paste name dialog box open , you can paste any
selected name or you can paste the entire list
• Click the Paste list button ( the defined names and their cell references
are pasted )
New Perspectives on Microsoft Excel 2013
13
Using Defined Names in FormulasXP
• Descriptive formulas are simpler to enter and
understand
• Ex: Range reference : = SUM(A2:A10)
•
Defined Names : = SUM (Price)
New Perspectives on Microsoft Excel 2013
14
Adding Defined Names to Existing
Formulas
XP
• Defined names are not automatically substituted for cell addresses in a formula
• Replace cell addresses in existing formulas with their defined names to make
formulas more understandable
Adding defined names to existing Formula:
• On the formula tab click the defined name button arrow then click apply Names
• In the apply names dialog box select the names you want to apply
• Click ok
Or
• Edit the formula by selecting
the cell reference and typing the
defined name.
New Perspectives on Microsoft Excel 2013
15
Visual Overview
New Perspectives on Microsoft Excel 2013
XP
16
Data Validation and Protection
New Perspectives on Microsoft Excel 2013
XP
17
Validating Data Entry
XP
• Ensures that correct data is entered and stored in a
worksheet
– Protects cells with formulas from accidental
deletion
– Reduces repetitious keystrokes and mouse clicks
• Each validation rule defines criteria for data that can
be stored in a cell or range
• Use Data Validation dialog box to specify validation
criteria, input message, and error alert for the active
cell
New Perspectives on Microsoft Excel 2013
18
Specifying Validation Criteria
XP
• When you create a validation rule, specify the
type of data allowed as well as a list or range
of acceptable values (validation criteria)
New Perspectives on Microsoft Excel 2013
19
Specifying Validation Criteria
XP
• On the DATA tab click the Data validation button
• Click the setting tab
• Click the Allow arrow , click the type of data allowed
New Perspectives on Microsoft Excel 2013
20
Creating an Input Message
•
•
•



XP
Reduces the chance of a data-entry error
Provides additional information about type of data allowed for the cell
Appears as a ScreenTip next to selected cell
On the DATA tab click the Data validation button
Click the Input message tab
Enter a title and text for the message
New Perspectives on Microsoft Excel 2013
21
Creating an Error Alert Style and
Message
XP
• An error alert determines what happens after a user attempts to make an
invalid entry in a cell that has a validation rule defined
• Tree error alert styles: Stop, Warning, and Information
 On the DATA tab click the Data validation button
 Click the Error Alert tab
 Select an alert style and then enter a title and a text for the error message
New Perspectives on Microsoft Excel 2013
22
Creating a List Validation Rule
XP
• Restricts a cell to accept only entries that are on a list you
create
• Create the list of valid entries in the Data Validation dialog
box, or use a list of valid entries in a single column or row
o On the DATA tab click the Data validation button
o Click the setting tab
o Click the Allow arrow , click the type of data allowed to be list
o In the source enter the range you want
o Then click ok
New Perspectives on Microsoft Excel 2013
23
Protecting a Worksheet and a
Workbook
XP
• Reduces data-entry errors by limiting access
to certain parts of the workbook
• Prevents users from changing cell contents,
workbook organization, or viewing formulas
New Perspectives on Microsoft Excel 2013
24
Locking and Unlocking Cells
XP
• A cell’s locked property determines whether
changes can be made to that cell
– Locked property has no impact as long as
worksheet is unprotected; after worksheet
is protected, locked property is in control
– Default: Locked property is turned on for
each cell, and worksheet protection is
turned off
• Common practice: Protect the worksheet, but
leave some cells unlocked
New Perspectives on Microsoft Excel 2013
25
Locking and Unlocking Cells
XP
To unlock the cell
o In the home tab click the format button and then click format cell
o The format cell dialog box open , the locked property in the
protection tab
New Perspectives on Microsoft Excel 2013
26
Protecting a Worksheet
XP
• Specify the actions still available to users
• A protected worksheet can be unprotected
– Require a password to turn off protection only if you are
concerned that users might make changes
–
–
–
–
On the REVIEW tab click the protect sheet button
Enter a password
Select all the actions you want to allow users to take
Click ok
New Perspectives on Microsoft Excel 2013
27
Protecting a Workbook
XP
• Keeps a worksheet from being modified
– Protecting the structure prohibits renaming, deleting, hiding, or
inserting worksheets
– Protecting the windows prohibits moving, resizing, closing, or hiding
parts of the window
• Default: Protect only the
structure of the workbook, not
the windows used to display it
– On the REVIEW tab click the protect
Workbook button
– Enter a password
– Click the check box to indicate if you
want to protect the workbook structure , windows or both
– Click ok
New Perspectives on Microsoft Excel 2013
28
Unprotecting a Worksheet and a
Workbook
XP
• A worksheet must be unprotected to edit its
contents
• A workbook must be unprotected to change
its structure
– On the REVIEW tab click the unprotect sheet button
– Enter a password of asked
– Click ok
New Perspectives on Microsoft Excel 2013
29
Inserting Comments
XP
• Use comments to:
– Explain contents of a particular cell
– Provide instructions to users
– Share ideas and notes from several
users collaborating on a project
To insert a comment:
• Select the cell to which you want to attach a
comment
• Right click the selected cell and then click insert
a comment
• Type the comment
• Click a cell to hide the comment
New Perspectives on Microsoft Excel 2013
30
Visual Overview
New Perspectives on Microsoft Excel 2013
XP
31
Working with Macros
New Perspectives on Microsoft Excel 2013
XP
32
Automating Tasks with Macros
XP
• Macros perform repetitive tasks consistently and faster than
you can
• After the macro is created and tested, tasks are done exactly
the same way each time
• Use Developer tab to create and run macros
To display the developer tab in the ribbon :
o Click the FILE tab and then click options
o In the left pane click customize ribbon
o In the right pane click the DEVELOPER check box and then
click ok
New Perspectives on Microsoft Excel 2013
33
Automating Tasks with Macros
New Perspectives on Microsoft Excel 2013
XP
34
Protecting Against Macro Viruses XP
• Virus
– Computer program designed to copy itself
into other programs with the intention of
causing mischief or harm
• Macro viruses
– Type of virus that uses a program’s own
macro programming language to distribute
the virus
• Microsoft Office 2013 provides several
options for levels of security
New Perspectives on Microsoft Excel 2013
35
Macro Security Settings
XP
• Control what Excel will do about macros when
the workbook is opened
New Perspectives on Microsoft Excel 2013
36
Macro Security Settings
XP
• Set macro security in the Trust Center
• Use Trusted Locations to define file paths for files considered trustworthy
• Use a digital signature to identify the author of a workbook that contains
macros
To set the macro security level:
• On the developer tab click macro security , click the Disable all macros
with notification
New Perspectives on Microsoft Excel 2013
37
Recording a Macro
XP
• For simple macros, use the macro recorder to record keystrokes and
mouse actions as they are performed
• For sophisticated macros, enter a series of commands in the Visual Basic
for Applications (VBA) programming language
To start the macro recording :
•
•
•
On the developer tab click the Record Macro button
Enter a name for the macro
Specify the location to store the macro then Click ok
New Perspectives on Microsoft Excel 2013
38
Running a Macro
XP
• Either use the specified shortcut key or on the
developer tab click the macros button then select
the macro in the Macro dialog box then click run
New Perspectives on Microsoft Excel 2013
39
Options for Fixing Macro Errors
XP
• Rerecord the macro using the same macro
name
• Delete the recorded macro; record it again
• Run the macro one step at a time to locate
the problem; use one of the previous methods
to correct the problem
New Perspectives on Microsoft Excel 2013
40
Working with the Visual Basic Editor XP
• Components
– Code window contains the VBA code
– Project Explorer window displays a treelike
diagram consisting of every open workbook
– Menu bar contains menus of commands
used to edit, debug, and run VBA
statements
• Accessed through Macro dialog box or Visual
Basic button in Code group on Developer tab
New Perspectives on Microsoft Excel 2013
41
Working with the Visual Basic Editor XP
On the developer tab click the Macros button , select the macro from the
macro name list and then click edit button
New Perspectives on Microsoft Excel 2013
42
Understanding the Structure of
Macros
XP
• Each macro (sub procedure) begins with Sub
followed by the name of the sub procedure
and a set of parentheses (the arguments)
• Comments about the macro follow the
statement and do not include any actions
• The body of the macro follows the comments
• End Sub statement indicates the end of the
sub procedure
New Perspectives on Microsoft Excel 2013
43
Understanding the Structure of
Macros
XP
• A Code window can contain several sub
procedures
• Each procedure is separated from the others
by SubProcedureName() statement at the
beginning, and End Sub statement at the end
• Sub procedures are organized into modules
New Perspectives on Microsoft Excel 2013
44
Editing a Macro Using the Visual BasicXP
Editor
• The Visual Basic Editor provides tools to assist
in writing error-free code
• As you type a command, the editor provides
pop-up windows and text to help you insert
the correct code
New Perspectives on Microsoft Excel 2013
45
Creating Macro Buttons
XP
• A macro can be assigned to a button placed directly in the worksheet
• Clicking a button (with a descriptive label) can be more intuitive and
simpler than trying to remember combinations of keystrokes
o
o
o
o
o
On the developer tab , in the Controls group click the insert button
In the form controls section , click the button(from control)button
Click the worksheet where you want the macro to be located
In the assign macro dialog box , select the macro you want to assign
Type a new name and click ok
New Perspectives on Microsoft Excel 2013
46
Creating Macro Buttons
New Perspectives on Microsoft Excel 2013
XP
47
Saving Workbooks with Macros
XP
• Default Excel Workbook format is a macrofree workbook (.xlsx file extension)
• To save the workbook with the macros, save
the file as a macro-enabled workbook (.xlsm
file extension)
New Perspectives on Microsoft Excel 2013
48
Saving Workbooks with Macros
XP
To save a workbook with macros:
• Click the save button , a dialog box will open click no
then the save as dialog box will open
• Click the save as type button and then click Excel
Macro-Enabled Workbook
• Click save
New Perspectives on Microsoft Excel 2013
49
Opening a Workbook with MacrosXP
• First time a workbook opens, a Security
Warning appears in Message Bar providing
the option to:
– Enable macros so they can be run, or
– Open the workbook with the macros
disabled
New Perspectives on Microsoft Excel 2013
50
Customizing the Ribbon
XP
• Minimize the Ribbon
To remove the DEVELOPER tab
• Right click any tab on the ribbon and then
click Customize the Ribbon
• In the right box , click the developer check box
to remove the check
• Click ok
New Perspectives on Microsoft Excel 2013
51
Download