0% found this document useful (0 votes)
43 views23 pages

ExcelCore2016lesson09 2E MACROS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views23 pages

ExcelCore2016lesson09 2E MACROS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Microsoft Excel 2016

Lesson 9
Working with Macros
VBA

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 1
Developer tab
• Macros and VBA tools can be found on the Developer tab,
which is hidden by default, so the first step is to enable it.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 2
Record a macro
• In the Code group on the Developer tab, click Record Macro.
• Optionally, enter a name for the macro in the Macro
name box, enter a shortcut key in the Shortcut key box, and
a description in the Description box, and then click OK to
start recording.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 3
Stop recording
• Perform the actions you want to automate, such as entering
boilerplate text or filling down a column of data.
• On the Developer tab, click Stop Recording.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 4
Saving Work with Macros
• A basic macro is a recording of a sequence of commands and
typed entries that you can then replay elsewhere in the
worksheet.
• Macros can be replayed on whatever cell is the active cell.
• A recorded macro is a series of steps that can be repeated
and that you might want to repeat frequently to save time.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 5
Step by Step: Record a Basic Macro (1 of 5)
GET READY. OPEN the 09 4Strong Tour Revenues workbook for this
lesson.
1. Click the File tab and then click Options. If the Developer tab already
displays on the ribbon, skip to Step 4. Otherwise, continue with Step 2.
2. In the Excel Options dialog box, click Customize Ribbon.
3. In the Main Tabs list on the right, check the Developer box. This adds the
Developer tab to the Excel ribbon, enabling you to more easily record
macros. Click OK.
4. The macro that you record creates a custom subtotal row at the place you
define, rather than at some place Excel determines. The rule you follow is
that the user (you) must select the cell where you want the subtotal to
appear and then run the macro. So to prepare for recording, click cell D21.
5. Click the Developer tab, and then in the Code group, find Use Relative
References. If it is not highlighted, click to select it. You want relative
references for this macro.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 6
Step by Step: Record a Basic Macro (2 of 5)
6. In the Code group, click Record Macro.
7. In the Record Macro dialog box, click the Macro name box
and then type CustomSubtotals.
8. In the Shortcut key box beside Ctrl +, type the capital S. This
changes the shortcut key to Ctrl+Shift+S. Leave Store macro
in set to This Workbook.
The Record Macro dialog box
should now appear as shown
here.
9. Click OK. You are now
recording a macro.
10. Press Shift+Down Arrow.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 7
Step by Step: Record a Basic Macro (3 of 5)
11. Click the Home tab, and then in the Cells group, click the
Insert arrow. In the menu, click Insert Sheet Rows.
12. Press Shift+Up Arrow.
13. In the Editing group, click AutoSum. Do not press Enter.
14. In the Clipboard group, click Copy.
15. Press Tab.
16. Type the partial formula =max(
17. In the Clipboard group, click Paste.
18. Type ) (end parenthesis) and press Tab.
19. Press Left Arrow.
20. Click $ (Accounting Number Format) in the Number group.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 8
Step by Step: Record a Basic Macro (4 of 5)
21. Click the Developer tab, and
then in the Code group, click
Stop Recording.
22. Now that you’re not recording,
adjust the width of column E to
fit its contents. As shown here,
the macro generates a total for
the bottom of the arbitrary
cluster of records, and also
tabulates the highest value in
that cluster in the cell adjacent
to the subtotal.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 9
Step by Step: Record a Basic Macro (5 of 5)
23. Click the File tab and then click Save As.
24. In Backstage, locate and select the Excel Lesson 9 folder.
25. In the Save as type list, choose Excel Macro-Enabled Workbook
(*.xlsm). Save the workbook as 09 4Strong Tour Revenues
Solution.xlsm. Click OK if you see a message box displayed after
you save the file.
PAUSE. LEAVE the workbook open for the next exercise.
• For security reasons, Excel no longer saves macros in its regular .XLS
and .XLSX file formats. The only way to save a macro-enabled
workbook is to give it the special .XLSM file type. This way,
companies that want to avoid any possibility of spreading malware
can enforce policies preventing .XLSM files from being attached to
or received within e-mails.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 10
Managing Macro Security
• Because of the proliferation of malicious software, you cannot
execute macros from a file you open until you read the
notification and then click Enable Content.
• If you never plan to run macros or if you’re skeptical about
your office colleagues, you can turn off macros completely.
• You can also turn off the notifications and enable all macros, if
the following situations apply:
• You work in an office where macros are in use constantly.
• You trust the source of the Excel workbooks.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 11
Step by Step: Manage Macro Security
GET READY. USE the workbook from the previous exercise.
1. On the Developer tab, in the Code group, click Macro
Security.
2. In the Trust Center dialog box, click Disable all macros with
notification to have Excel warn you whenever an opened
workbook contains macros, enabling you to turn those
macros on or off based on your decision.
3. Click OK.
CLOSE the workbook. CLOSE Excel.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 12
Data types

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 13
Data types

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 14
Data types

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 15
VBA
VBA stands for Visual Basic for Applications, an event-driven
programming language from Microsoft. It is now predominantly
used with Microsoft Office applications such as MS-Excel, MS-
Word and MS-Access.

• Modules

Modules is the area where the code is written. This is a new


Workbook, hence there aren't any Modules.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 16
Procedure
• Procedure

Procedures are a group of statements executed as a whole,


which instructs Excel how to perform a specific task. The task
performed can be a very simple or a very complicated task.
However, it is a good practice to break down complicated
procedures into smaller ones.

The two main types of Procedures are Sub and Function.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 17
Function
• A function is a group of reusable code, which can be called
anywhere in your program. This eliminates the need of writing
the same code over and over again.
• This helps the programmers to divide a big program into a
number of small and manageable functions. Apart from inbuilt
Functions, VBA allows to write user-defined functions as well
and statements are written between Function and End
Function.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 18
Function

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 19
Example

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 20
Sub-Procedures
Sub-procedures work similar to functions.
• While sub procedures DO NOT Return a value, functions may
or may not return a value. Sub procedures CAN be called
without call keyword. Sub procedures are always enclosed
within Sub and End Sub statements.

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 21
Sub-Procedures

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 22
Sub-Procedures

© 2018, John Wiley & Sons, Inc. Microsoft Official Academic Course, Microsoft Excel 2016 23

You might also like