0% found this document useful (0 votes)
47 views72 pages

Excel 2021 and Office 365

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

Excel 2021 and Office 365

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

Microsoft Excel VBA and

Macros (Office 2021 and


Microsoft 365)

Bill Jelen
Tracy Syrstad

9780137521524_print.indb 1 01/02/22 8:43 PM


Microsoft Excel VBA and Macros (Office 2021 and Microsoft 365) EDITOR-IN-CHIEF
Brett Bartow
Published with the authorization of Microsoft Corporation by:
Pearson Education, Inc. EXECUTIVE EDITOR
Copyright © 2022 by Pearson Education, Inc. Loretta Yates

SPONSORING EDITOR
All rights reserved. This publication is protected by copyright, and permission must be
Charvi Arora
obtained from the publisher prior to any prohibited reproduction, storage in a retrieval
system, or transmission in any form or by any means, electronic, mechanical, photo- DEVELOPMENT EDITOR
copying, recording, or likewise. For information regarding permissions, request forms, Songlin Qiu
and the appropriate contacts within the Pearson Education Global Rights & Permissions
Department, please visit www.pearson.com/permissions. MANAGING EDITOR
No patent liability is assumed with respect to the use of the information contained Sandra Schroeder
herein. Although every precaution has been taken in the preparation of this book, the
publisher and author assume no responsibility for errors or omissions. Nor is any liabil- SENIOR PROJECT EDITOR
ity assumed for damages resulting from the use of the information contained herein. Tracey Croom

COPY EDITOR
ISBN-13: 978-0-13-752152-4
Sarah Kearns
ISBN-10: 0-13-752152-9
INDEXER

Library of Congress Control Number: 2022930486 Timothy Wright

ScoutAutomatedPrintCode PROOFREADER
Donna E. Mulder
Trademarks
TECHNICAL EDITOR
Microsoft and the trademarks listed at http://www.microsoft.com on the “Trademarks” Bob Umlas
webpage are trademarks of the Microsoft group of companies. All other marks are
property of their respective owners. Figures 18-1, and 18-2 are © 2022 Spotify AB. COVER DESIGNER
Twist Creative, Seattle

Warning and Disclaimer COMPOSITOR


Every effort has been made to make this book as complete and as accurate as possible, codeMantra
but no warranty or fitness is implied. The information provided is on an “as is” basis. The
author, the publisher, and Microsoft Corporation shall have neither liability nor respon-
sibility to any person or entity with respect to any loss or damages arising from the
information contained in this book or from the use of the programs accompanying it.

Special Sales

For information about buying this title in bulk quantities, or for special sales
opportunities (which may include electronic versions; custom cover designs; and
content particular to your business, training goals, marketing focus, or branding
interests), please contact our corporate sales department at [email protected]
or (800) 382-3419.
For government sales inquiries, please contact [email protected].
For questions about sales outside the U.S., please contact [email protected].

9780137521524_print.indb 2 01/02/22 8:43 PM


Pearson’s Commitment to
Diversity, Equity, and Inclusion
Pearson is dedicated to creating bias-free content that reflects the diversity of all learners.
We embrace the many dimensions of diversity, including but not limited to race, ethnicity,
gender, socioeconomic status, ability, age, sexual orientation, and religious or political
beliefs.

Education is a powerful force for equity and change in our world. It has the potential to
deliver opportunities that improve lives and enable economic mobility. As we work with
authors to create content for every product and service, we acknowledge our responsibility
to demonstrate inclusivity and incorporate diverse scholarship so that everyone can achieve
their potential through learning. As the world’s leading learning company, we have a duty
to help drive change and live up to our purpose to help more people create a better life for
themselves and to create a better world.

Our ambition is to purposefully contribute to a world where:

■■ Everyone has an equitable and lifelong opportunity to succeed through learning.

■■ Our educational products and services are inclusive and represent the rich diversity
of learners.

■■ Our educational content accurately reflects the histories and experiences of the
learners we serve.

■■ Our educational content prompts deeper discussions with learners and motivates
them to expand their own learning (and worldview).

While we work hard to present unbiased content, we want to hear from you about any
concerns or needs with this Pearson product so that we can investigate and address them.

■■ Please contact us with concerns about any potential bias at


https://www.pearson.com/report-bias.html.

iii

9780137521524_print.indb 3 01/02/22 8:43 PM


This page intentionally left blank

9780137521524_print.indb 4 01/02/22 8:43 PM


Dedication
For Skipper Geanangel, Patricia Garick, Jim Lantz, Robert Mucci,
Bill & Jean Esposito. Thanks for launching a writing career.
—Bill Jelen

To John. Giraffe.
—Tracy Syrstad

9780137521524_print.indb 5 01/02/22 8:43 PM


Contents at a Glance

Acknowledgments xxv
About the Authors xxvii
Introduction xxix
CHAPTER 1 Unleashing the power of Excel with VBA 1
CHAPTER 2 This sounds like BASIC, so why doesn’t it look familiar? 27
CHAPTER 3 Referring to ranges 53
CHAPTER 4 Looping and flow control 69
CHAPTER 5 R1C1-style formulas 87
CHAPTER 6 Creating and manipulating names in VBA 97
CHAPTER 7 Event programming 111
CHAPTER 8 Arrays 125
CHAPTER 9 Creating classes and collections 133
CHAPTER 10 Userforms: An introduction 153
CHAPTER 11 Data mining with Advanced Filter 175
CHAPTER 12 Using VBA to create pivot tables 211
CHAPTER 13 Excel power 257
CHAPTER 14 Sample user-defined functions 285
CHAPTER 15 Creating charts 313
CHAPTER 16 Data visualizations and conditional formatting 339
CHAPTER 17 Dashboarding with sparklines in Excel 363
CHAPTER 18 Reading from the web using M and VBA 385
CHAPTER 19 Text file processing 413
CHAPTER 20 Automating Word 427
CHAPTER 21 
Using Access as a back end to enhance multiuser
access to data 447
CHAPTER 22 Advanced userform techniques 465
CHAPTER 23 The Windows Application Programming Interface (API) 491
CHAPTER 24 Handling errors 501
CHAPTER 25 Customizing the ribbon to run macros 517
CHAPTER 26 Creating Excel add-ins 539
CHAPTER 27 An introduction to creating Office add-ins 549
CHAPTER 28 What’s new in Excel 365 and what’s changed 571
Index 579
vi

9780137521524_print.indb 6 01/02/22 8:43 PM


Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxix

Chapter 1 Unleashing the power of Excel with VBA 1


Barriers to entry. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The macro recorder doesn’t work!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
No one person on the Excel team is focused on the
macro recorder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Visual Basic is not like BASIC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Good news: Climbing the learning curve is easy. . . . . . . . . . . . . . . . . . 3
Great news: Excel with VBA is worth the effort. . . . . . . . . . . . . . . . . . . . 3
Knowing your tools: The Developer tab. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Understanding which file types allow macros . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Macro security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Adding a trusted location. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Using macro settings to enable macros in workbooks outside
trusted locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Using Disable All Macros With Notification. . . . . . . . . . . . . . . . . . . . . . . 8
Overview of recording, storing, and running a macro. . . . . . . . . . . . . . . . . . . 8
Filling out the Record Macro dialog box . . . . . . . . . . . . . . . . . . . . . . . . . 9
Running a macro. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Creating a macro button on the ribbon. . . . . . . . . . . . . . . . . . . . . . . . . 10
Creating a macro button on the Quick Access Toolbar. . . . . . . . . . . . 11
Assigning a macro to a form control, text box, or shape. . . . . . . . . . 12
Understanding the VB Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
VB Editor settings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
The Project Explorer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
The Properties window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

vii

9780137521524_print.indb 7 01/02/22 8:43 PM


Understanding shortcomings of the macro recorder . . . . . . . . . . . . . . . . . . 15
Recording the macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Examining code in the Programming window. . . . . . . . . . . . . . . . . . . 17
Running the macro on another day produces
undesired results. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Possible solution: Use relative references when recording. . . . . . . . 20
Never use AutoSum or Quick Analysis while
recording a macro. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Four tips for using the macro recorder. . . . . . . . . . . . . . . . . . . . . . . . . . 25
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Chapter 2 This sounds like BASIC, so why doesn’t it look


familiar?   27
Understanding the parts of VBA “speech”. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
VBA is not really hard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
VBA Help files: Using F1 to find anything. . . . . . . . . . . . . . . . . . . . . . . . 32
Using Help topics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Examining recorded macro code: Using the VB Editor and Help. . . . . . . . 33
Optional parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Defined constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Properties can return objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Using debugging tools to figure out recorded code. . . . . . . . . . . . . . . . . . . 38
Stepping through code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
More debugging options: Breakpoints. . . . . . . . . . . . . . . . . . . . . . . . . . 40
Backing up or moving forward in code. . . . . . . . . . . . . . . . . . . . . . . . . . 40
Not stepping through each line of code. . . . . . . . . . . . . . . . . . . . . . . . . 41
Querying anything while stepping through code. . . . . . . . . . . . . . . . 41
Using a watch to set a breakpoint. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Using a watch on an object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Object Browser: The ultimate reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Seven tips for cleaning up recorded code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Tip 1: Don’t select anything. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Tip 2: Use Cells(2,5) because it’s more convenient
than Range("E2"). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Tip 3: Use more reliable ways to find the last row. . . . . . . . . . . . . . . . 47
Tip 4: Use variables to avoid hard-coding rows and formulas. . . . . 49

viii Contents

9780137521524_print.indb 8 01/02/22 8:43 PM


Tip 5: Use R1C1 formulas that make your life easier. . . . . . . . . . . . . . . 49
Tip 6: Copy and paste in a single statement . . . . . . . . . . . . . . . . . . . . . 49
Tip 7: Use With...End With to perform multiple actions. . . . . . . . . 50
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

Chapter 3 Referring to ranges 53


The Range object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Syntax for specifying a range. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Referencing named ranges. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Shortcut for referencing ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Referencing ranges in other sheets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Referencing a range relative to another range. . . . . . . . . . . . . . . . . . . . . . . . . 56
Using the Cells property to select a range. . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Using the Offset property to refer to a range. . . . . . . . . . . . . . . . . . . . . . . . . 58
Using the Resize property to change the size of a range. . . . . . . . . . . . . . . 60
Using the Columns and Rows properties to specify a range. . . . . . . . . . . . . . 61
Using the Union method to join multiple ranges. . . . . . . . . . . . . . . . . . . . . . . 62
Using the Intersect method to create a new range from
overlapping ranges. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Using the IsEmpty function to check whether a cell is empty. . . . . . . . . . . 62
Using the CurrentRegion property to select a data range. . . . . . . . . . . . . . 63
Using the Areas collection to return a noncontiguous range. . . . . . . . . . . 66
Referencing tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

Chapter 4 Looping and flow control   69


For...Next loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Using variables in the For statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Variations on the For.Next loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Exiting a loop early after a condition is met . . . . . . . . . . . . . . . . . . . . . 73
Nesting one loop inside another loop. . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Do loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Using the While or Until clause in Do loops . . . . . . . . . . . . . . . . . . . . . 77

Contents ix

9780137521524_print.indb 9 01/02/22 8:43 PM


The VBA loop: For Each. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Object variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Flow control: Using If...Then...Else and Select Case. . . . . . . . . . . . . . 81
Basic flow control: If...Then...Else. . . . . . . . . . . . . . . . . . . . . . . . . 81
Using Select Case...End Select for multiple conditions . . . . . . . 83
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

Chapter 5 R1C1-style formulas  87


Toggling to R1C1-style references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Witnessing the miracle of Excel formulas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Entering a formula once and copying 1,000 times . . . . . . . . . . . . . . . 89
The secret: It’s not that amazing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Understanding the R1C1 reference style. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Using R1C1 with relative references. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Using R1C1 with absolute references. . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Using R1C1 with mixed references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Referring to entire columns or rows with R1C1 style. . . . . . . . . . . . . . 93
Replacing many A1 formulas with a single R1C1 formula. . . . . . . . . . 94
Remembering column numbers associated with
column letters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

Chapter 6 Creating and manipulating names in VBA  97


Global versus local names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Adding names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Deleting names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Adding comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Types of names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Formulas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Using arrays in names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Reserved names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Hiding names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

x Contents

9780137521524_print.indb 10 01/02/22 8:43 PM


Checking for the existence of a name. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

Chapter 7 Event programming   111


Levels of events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Using events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Event parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Enabling events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Workbook events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Workbook-level sheet events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Worksheet events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Chart events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Embedded charts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Embedded chart and chart sheet events. . . . . . . . . . . . . . . . . . . . . . . 119
Application-level events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

Chapter 8 Arrays   125


Declaring an array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Declaring a multidimensional array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Filling an array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Retrieving data from an array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Using arrays to speed up code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Using dynamic arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Passing an array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

Chapter 9 Creating classes and collections   133


Inserting a class module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Trapping application and embedded chart events. . . . . . . . . . . . . . . . . . . . 134
Application events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Embedded chart events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Creating a custom object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Using a custom object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

Contents xi

9780137521524_print.indb 11 01/02/22 8:43 PM


Using collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Creating a collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Creating a collection in a standard module. . . . . . . . . . . . . . . . . . . . . 141
Creating a collection in a class module. . . . . . . . . . . . . . . . . . . . . . . . . 142
Using dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Using user-defined types to create custom properties. . . . . . . . . . . . . . . . 148
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

Chapter 10 Userforms: An introduction   153


Input boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Message boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Creating a userform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Calling and hiding a userform. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Programming userforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Userform events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Programming controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Using basic form controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Using labels, text boxes, and command buttons. . . . . . . . . . . . . . . . 159
Deciding whether to use list boxes or combo boxes in forms. . . . 162
Using the MultiSelect property of a list box. . . . . . . . . . . . . . . . . . . 163
Adding option buttons to a userform. . . . . . . . . . . . . . . . . . . . . . . . . . 165
Adding graphics to a userform. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Using a spin button on a userform. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Using the MultiPage control to combine forms. . . . . . . . . . . . . . . . . 169
Verifying field entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Illegal window closing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Getting a file name. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

Chapter 11 Data mining with Advanced Filter   175


Replacing a loop with AutoFilter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Using AutoFilter techniques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Selecting visible cells only. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

xii Contents

9780137521524_print.indb 12 01/02/22 8:43 PM


Advanced Filter—easier in VBA than in Excel. . . . . . . . . . . . . . . . . . . . . . . . . 183
Using the Excel interface to build an advanced filter. . . . . . . . . . . . 184
Using Advanced Filter to extract a unique list of values . . . . . . . . . . . . . . . 184
Extracting a unique list of values with the user interface . . . . . . . . 185
Extracting a unique list of values with VBA code. . . . . . . . . . . . . . . . 186
Getting unique combinations of two or more fields. . . . . . . . . . . . . 190
Using Advanced Filter with criteria ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Joining multiple criteria with a logical OR. . . . . . . . . . . . . . . . . . . . . . 192
Joining two criteria with a logical AND. . . . . . . . . . . . . . . . . . . . . . . . . 193
Other slightly complex criteria ranges . . . . . . . . . . . . . . . . . . . . . . . . . 193
The most complex criteria: Replacing the list of values
with a condition created as the result of a formula. . . . . . . . . . . . 193
Setting up a condition using computed criteria. . . . . . . . . . . . . . . . . 194
Using Filter In Place in Advanced Filter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Catching no records when using a filter in place. . . . . . . . . . . . . . . . 201
Showing all records after running a filter in place. . . . . . . . . . . . . . . 202
The real workhorse: xlFilterCopy with all records rather than
unique records only . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Copying all columns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Copying a subset of columns and reordering. . . . . . . . . . . . . . . . . . . 203
Excel in practice: Turning off a few drop-down menus
in the AutoFilter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

Chapter 12 Using VBA to create pivot tables   211


Understanding how pivot tables evolved over various
Excel versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Building a pivot table in Excel VBA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Defining the pivot cache. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Creating and configuring the pivot table. . . . . . . . . . . . . . . . . . . . . . . 213
Adding fields to the data area. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Learning why you cannot move or change part of a
pivot report. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Determining the size of a finished pivot table to convert the
pivot table to values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217

Contents xiii

9780137521524_print.indb 13 01/02/22 8:43 PM


Using advanced pivot table features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Using multiple value fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Grouping daily dates to months, quarters, or years. . . . . . . . . . . . . 221
Changing the calculation to show percentages. . . . . . . . . . . . . . . . . 223
Eliminating blank cells in the Values area. . . . . . . . . . . . . . . . . . . . . . . 226
Controlling the sort order with AutoSort. . . . . . . . . . . . . . . . . . . . . . . 226
Replicating the report for every product. . . . . . . . . . . . . . . . . . . . . . . 226
Filtering a data set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Manually filtering two or more items in a pivot field. . . . . . . . . . . . 229
Using the conceptual filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Using the search filter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Setting up slicers to filter a pivot table. . . . . . . . . . . . . . . . . . . . . . . . . 237
Setting up a timeline to filter an Excel pivot table. . . . . . . . . . . . . . . 241
Formatting the intersection of values in a pivot table. . . . . . . . . . . . . . . . . 243
Using the Data Model in Excel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Adding both tables to the Data Model. . . . . . . . . . . . . . . . . . . . . . . . . 244
Creating a relationship between the two tables . . . . . . . . . . . . . . . . 245
Defining the pivot cache and building the pivot table. . . . . . . . . . . 245
Adding model fields to the pivot table. . . . . . . . . . . . . . . . . . . . . . . . . 246
Adding numeric fields to the Values area. . . . . . . . . . . . . . . . . . . . . . . 246
Putting it all together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Using other pivot table features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Calculated data fields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Calculated items. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Using ShowDetail to filter a record set. . . . . . . . . . . . . . . . . . . . . . . . . 250
Changing the layout from the Design tab. . . . . . . . . . . . . . . . . . . . . . 250
Settings for the report layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Suppressing subtotals for multiple row fields. . . . . . . . . . . . . . . . . . . 252
Comparing VBA to TypeScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256

Chapter 13 Excel power   257


File operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Listing files in a directory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Importing and deleting a CSV file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Reading a text file into memory and parsing. . . . . . . . . . . . . . . . . . . 260

xiv Contents

9780137521524_print.indb 14 01/02/22 8:43 PM


Combining and separating workbooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Separating worksheets into workbooks. . . . . . . . . . . . . . . . . . . . . . . . 261
Combining workbooks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Copying data to separate worksheets without using Filter . . . . . . 263
Exporting data to an XML file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Placing a chart in a cell note. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Tracking user changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Techniques for VBA pros. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Creating an Excel state class module. . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Drilling-down a pivot table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Filtering an OLAP pivot table by a list of items. . . . . . . . . . . . . . . . . . 271
Creating a custom sort order. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Creating a cell progress indicator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Using a protected password box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Selecting with SpecialCells. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Resetting a table’s format. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Using VBA Extensibility to add code to new workbooks. . . . . . . . . 279
Converting a fixed-width report to a data set . . . . . . . . . . . . . . . . . . 280
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284

Chapter 14 Sample user-defined functions   285


Creating user-defined functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Building a simple custom function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Sharing UDFs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Useful custom Excel functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Checking whether a workbook is open. . . . . . . . . . . . . . . . . . . . . . . . . 288
Checking whether a sheet in an open workbook exists. . . . . . . . . . 289
Counting the number of workbooks in a directory. . . . . . . . . . . . . . 290
Retrieving the user ID. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Retrieving date and time of last save. . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Retrieving permanent date and time. . . . . . . . . . . . . . . . . . . . . . . . . . 292
Validating an email address. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Summing cells based on interior color. . . . . . . . . . . . . . . . . . . . . . . . . 295
Counting unique values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Finding the first nonzero-length cell in a range. . . . . . . . . . . . . . . . . 296

Contents xv

9780137521524_print.indb 15 01/02/22 8:43 PM


Substituting multiple characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Retrieving numbers from mixed text. . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Converting week number into date. . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Sorting and concatenating. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Sorting numeric and alpha characters . . . . . . . . . . . . . . . . . . . . . . . . . 301
Searching for a string within text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Returning the addresses of duplicate maximum values . . . . . . . . . 304
Returning a hyperlink address. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Returning the column letter of a cell address. . . . . . . . . . . . . . . . . . . 305
Using Select…Case on a worksheet. . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Creating LAMBDA functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Building a simple LAMBDA function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Sharing LAMBDA functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Useful LAMBDA functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

Chapter 15 Creating charts   313


Using .AddChart2 to create a chart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Understanding chart styles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Formatting a chart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Referring to a specific chart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Specifying a chart title. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Applying a chart color. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Filtering a chart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Using SetElement to emulate changes from the plus icon. . . . . . . 322
Using the Format tab to micromanage formatting options. . . . . . 327
Changing an object’s fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Formatting line settings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Creating a combo chart. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Creating map charts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Creating waterfall charts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Exporting a chart as a graphic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Considering backward compatibility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338

xvi Contents

9780137521524_print.indb 16 01/02/22 8:43 PM


Chapter 16 Data visualizations and conditional formatting   339
VBA methods and properties for data visualizations. . . . . . . . . . . . . . . . . . 340
Adding data bars to a range. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Adding color scales to a range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Adding icon sets to a range. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Specifying an icon set. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Specifying ranges for each icon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Using visualization tricks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Creating an icon set for a subset of a range. . . . . . . . . . . . . . . . . . . . . 351
Using two colors of data bars in a range . . . . . . . . . . . . . . . . . . . . . . . 352
Using other conditional formatting methods. . . . . . . . . . . . . . . . . . . . . . . . . 355
Formatting cells that are above or below average . . . . . . . . . . . . . . 355
Formatting cells in the top 10 or bottom 5 . . . . . . . . . . . . . . . . . . . . . 355
Formatting unique or duplicate cells. . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Formatting cells based on their value. . . . . . . . . . . . . . . . . . . . . . . . . . 358
Formatting cells that contain text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Formatting cells that contain dates. . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Formatting cells that contain blanks or errors. . . . . . . . . . . . . . . . . . 359
Using a formula to determine which cells to format. . . . . . . . . . . . . 359
Using the new NumberFormat property. . . . . . . . . . . . . . . . . . . . . . . . . 361
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362

Chapter 17 Dashboarding with sparklines in Excel   363


Creating sparklines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Scaling sparklines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Formatting sparklines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Using theme colors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Using RGB colors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Formatting sparkline elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Formatting win/loss charts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Creating a dashboard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Observations about sparklines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Creating hundreds of individual sparklines in a dashboard. . . . . . 379
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

Contents xvii

9780137521524_print.indb 17 01/02/22 8:43 PM


Chapter 18 Reading from the web using M and VBA   385
Get credentials for accessing an API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Build a query in Power Query using the M language to retrieve
data from the web for one specific value. . . . . . . . . . . . . . . . . . . . . . . . . . 387
Refreshing the credentials after they expire. . . . . . . . . . . . . . . . . . . . 390
Building a custom function in Power Query. . . . . . . . . . . . . . . . . . . . 390
Using the new function in your code. . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Duplicating an existing query to make a new query. . . . . . . . . . . . 393
Querying the list of songs on an album. . . . . . . . . . . . . . . . . . . . . . . . 395
Generalizing the queries using VBA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Simplifying the SearchArtist query to a single line of code . . . . . . 396
Simplifying the ArtistAlbums query. . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Simplifying the AlbumTracks query. . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Grouping queries to clean up the queries list. . . . . . . . . . . . . . . . . . . 397
Planning the arrangement of query results on your
dashboard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Using global variables and loops in M . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Storing global variables in a Settings record in Power Query. . . . 402
Simple error handling using try with otherwise. . . . . . . . . . . . . . . . . 403
Using If logic in M . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Looping using List.Generate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Application.OnTime to periodically analyze data. . . . . . . . . . . . . . . . . . . . . 406
Using Ready mode for scheduled procedures. . . . . . . . . . . . . . . . . . 407
Specifying a window of time for an update. . . . . . . . . . . . . . . . . . . . . 407
Canceling a previously scheduled macro. . . . . . . . . . . . . . . . . . . . . . . 408
Closing Excel cancels all pending scheduled macros. . . . . . . . . . . . 408
Scheduling a macro to run x minutes in the future. . . . . . . . . . . . . . 408
Scheduling a verbal reminder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Scheduling a macro to run every two minutes. . . . . . . . . . . . . . . . . . 410
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411

Chapter 19 Text file processing   413


Importing from text files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Importing text files with fewer than 1,048,576 rows. . . . . . . . . . . . . 413
Dealing with text files with more than 1,048,576 rows. . . . . . . . . . . 419

xviii Contents

9780137521524_print.indb 18 01/02/22 8:43 PM


Writing text files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425

Chapter 20 Automating Word   427


Using early binding to reference a Word object. . . . . . . . . . . . . . . . . . . . . . 427
Using late binding to reference a Word object . . . . . . . . . . . . . . . . . . . . . . . 430
Using the New keyword to reference the Word application. . . . . . . . . . . . 430
Using the CreateObject function to create a new instance
of an object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Using the GetObject function to reference an existing instance
of Word. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Using constant values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Using the Watches window to retrieve the real value
of a constant. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Using the Object Browser to retrieve the real value
of a constant. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Understanding Word’s objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
The Document object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Controlling form fields in Word. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445

Chapter 21 Using Access as a back end to enhance


multiuser access to data  447
ADO versus DAO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
The tools of ADO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Adding a record to a database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Retrieving records from a database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Updating an existing record. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Deleting records via ADO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Summarizing records via ADO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458
Other utilities via ADO. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Checking for the existence of tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Checking for the existence of a field. . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Adding a table on the fly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Adding a field on the fly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462

Contents xix

9780137521524_print.indb 19 01/02/22 8:43 PM


SQL Server examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

Chapter 22 Advanced userform techniques   465


Using the UserForm toolbar in the design of controls
on userforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
More userform controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
CheckBox controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
TabStrip controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
RefEdit controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
ToggleButton controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Using a scrollbar as a slider to select values. . . . . . . . . . . . . . . . . . . . . 472
Controls and collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Modeless userforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Using hyperlinks in userforms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Adding controls at runtime. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Resizing the userform on the fly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Adding a control on the fly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Sizing on the fly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Adding other controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Adding an image on the fly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Putting it all together. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Adding help to a userform. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Showing accelerator keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Adding control tip text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Creating the tab order. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Coloring the active control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Creating transparent forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489

Chapter 23 The Windows Application Programming


Interface (API)   491
Understanding an API declaration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Using an API declaration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Making 32-bit- and 64-bit-compatible API declarations . . . . . . . . . . . . . . 493

xx Contents

9780137521524_print.indb 20 01/02/22 8:43 PM


API function examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Retrieving the computer name. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
Checking whether an Excel file is open on a network . . . . . . . . . . . 495
Retrieving display-resolution information. . . . . . . . . . . . . . . . . . . . . . 496
Customizing the About dialog box . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Disabling the X for closing a userform . . . . . . . . . . . . . . . . . . . . . . . . . 498
Creating a running timer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Playing sounds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500

Chapter 24 Handling errors   501


What happens when an error occurs?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
A misleading Debug error in userform code. . . . . . . . . . . . . . . . . . . . 503
Basic error handling with the On Error GoTo syntax. . . . . . . . . . . . . . . . . . 505
Generic error handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Handling errors by choosing to ignore them. . . . . . . . . . . . . . . . . . . 506
Suppressing Excel warnings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Encountering errors on purpose. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
Training your clients. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
Errors that won’t show up in Debug mode. . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Errors while developing versus errors months later. . . . . . . . . . . . . . . . . . . 510
Runtime error 9: Subscript out of range. . . . . . . . . . . . . . . . . . . . . . . . 511
Runtime error 1004: Method range of object global failed . . . . . . 512
The ills of protecting code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
More problems with passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Errors caused by different versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516

Chapter 25 Customizing the ribbon to run macros   517


Where to add code: The customui folder and file. . . . . . . . . . . . . . . . . . . . . 518
Creating a tab and a group. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Adding a control to a ribbon. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
Accessing the file structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
Understanding the RELS file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525

Contents xxi

9780137521524_print.indb 21 01/02/22 8:43 PM


Renaming an Excel file and opening a workbook. . . . . . . . . . . . . . . . . . . . . 526
Using images on buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Using Microsoft Office icons on a ribbon. . . . . . . . . . . . . . . . . . . . . . . 527
Adding custom icon images to a ribbon. . . . . . . . . . . . . . . . . . . . . . . . 528
Troubleshooting error messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
The attribute “Attribute Name” on the element
“customui ribbon” is not defined in the DTD/schema . . . . . . . . . 530
Illegal qualified name character. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Element “customui Tag Name” is unexpected according
to content model of parent element “customui Tag Name”. . . . 531
Found a problem with some content . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Wrong number of arguments or invalid property
assignment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Invalid file format or file extension. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
Nothing happens. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
Other ways to run a macro. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
Using a keyboard shortcut to run a macro. . . . . . . . . . . . . . . . . . . . . 533
Attaching a macro to a command button. . . . . . . . . . . . . . . . . . . . . . 534
Attaching a macro to a shape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Attaching a macro to an ActiveX control. . . . . . . . . . . . . . . . . . . . . . . 536
Running a macro from a hyperlink. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537

Chapter 26 Creating Excel add-ins   539


Characteristics of standard add-ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Converting an Excel workbook to an add-in. . . . . . . . . . . . . . . . . . . . . . . . . . 540
Using Save As to convert a file to an add-in . . . . . . . . . . . . . . . . . . . . 541
Using the VB Editor to convert a file to an add-in. . . . . . . . . . . . . . . 542
Having a client install an add-in. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
Add-in security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Closing add-ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Removing add-ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
Using a hidden workbook as an alternative to an add-in. . . . . . . . . . . . . . 545
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547

xxii Contents

9780137521524_print.indb 22 01/02/22 8:43 PM


Chapter 27 An introduction to creating Office add-ins   549
Creating your first Office add-in—Hello World. . . . . . . . . . . . . . . . . . . . . . . 550
Adding interactivity to an Office add-in. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
A basic introduction to HTML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Using tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Adding buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Using CSS files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
Using XML to define an Office add-in. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
Using JavaScript to add interactivity to an Office add-in. . . . . . . . . . . . . . 559
The structure of a function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Curly braces and spaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Semicolons and line breaks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 562
JavaScript for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
How to do an if statement in JavaScript. . . . . . . . . . . . . . . . . . . . . . . 564
How to do a Select..Case statement in JavaScript. . . . . . . . . . . . . 564
How to use a For each..next statement in JavaScript. . . . . . . . . . . 565
Mathematical, logical, and assignment operators. . . . . . . . . . . . . . 566
Math functions in JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 567
Writing to the content pane or task pane . . . . . . . . . . . . . . . . . . . . . . 569
JavaScript changes for working in an Office add-in. . . . . . . . . . . . . 569
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570

Chapter 28 What’s new in Excel 365 and what’s changed   571


Office 365 subscription versus Excel 2021 perpetual . . . . . . . . . . . . . . . . . . 571
If it has changed in the front end, it has changed in VBA. . . . . . . . . . . . . . 571
The ribbon. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Single-document interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
Modern array formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
LAMBDA function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Quick Analysis tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574

Contents xxiii

9780137521524_print.indb 23 01/02/22 8:43 PM


Pivot tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
Slicers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574
Icons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
3D models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
SmartArt. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
TypeScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Learning the new objects and methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Compatibility mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
Using the Version property. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
Using the Excel8CompatibilityMode property. . . . . . . . . . . . . . . . . 577
Next steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579

xxiv Contents

9780137521524_print.indb 24 01/02/22 8:43 PM


Acknowledgments

T hanks to Tracy Syrstad for being a great coauthor.

Bob Umlas is the smartest Excel guy I know and is an awesome technical editor.
At Pearson, Loretta Yates is an excellent acquisitions editor. Thanks to the Kughens for
guiding this book through production. I updated this edition in residence at the Kola Mi
Writing Camp. My sincere thanks to the staff there for keeping me on track.

Along the way, I’ve learned a lot about VBA programming from the awesome com-
munity at the MrExcel.com message board. VoG, Richard Schollar, and Jon von der
Heyden all stand out as having contributed posts that led to ideas in this book. Thanks
to Pam Gensel for Excel macro lesson #1. Mala Singh taught me about creating charts in
VBA. Suat Özgür keeps me current on new VBA trends and contributed many ideas to
Chapter 18.

My family was incredibly supportive during this time. Thanks to Mary Ellen Jelen.

—Bill

Thank you to all the moderators at the MrExcel forum who keep the board organized,
despite the best efforts of the spammers. Thank you to Joe4, RoryA, and Petersss for
helping process all the forum’s contact emails.

Programming is a constant learning experience, and I really appreciate the clients


who have encouraged me to program outside my comfort zone so that my skills and
knowledge have expanded. Thank you to Suat Özgür for helping me defeat some truly
insidious programming puzzles.

Final Fantasy XIV has become my second home. I’d like to give a special thank you
to my in-game friends who not only make gaming so much fun, but for also helping
me find the confidence to dive head first into the unknown: War, Chraz, and Shabadoo.
Thank you for sharing your love of gaming with me.

And last, but not least, thanks to Bill Jelen. His site, MrExcel.com, is a place where
thousands come for help. It’s also a place where I, and others like me, have an opportu-
nity to learn from and assist others.

—Tracy

xxv

9780137521524_print.indb 25 01/02/22 8:43 PM


This page intentionally left blank

9780137521524_print.indb 4 01/02/22 8:43 PM


About the Authors
Bill Jelen, Excel MVP and the host of MrExcel.com, has been using
spreadsheets since 1985, and he launched the MrExcel.com website
in 1998. Bill was a regular guest on Call for Help with Leo Laporte and
has produced more than 2,300 episodes of his daily video podcast,
Learn Excel from MrExcel. He is the author of 65 books about Micro-
soft Excel and writes the monthly Excel column for Strategic Finance
magazine. Before founding MrExcel.com, Bill spent 12 years in the trenches—working as
a financial analyst for finance, marketing, accounting, and operations departments of a
$500 million public company. He lives in Merritt Island, Florida, with his wife, Mary Ellen.

Tracy Syrstad is a Microsoft Excel developer and author of ten Excel books. She has
been helping people with Microsoft Office issues since 1997, when she discovered free
online forums where anyone could ask and answer questions. Tracy found out she
enjoyed teaching others new skills, and when she began working as a developer,
she was able to integrate the fun of teaching with one-on-one online desktop sharing
sessions. Tracy lives on an acreage in eastern South Dakota with her husband, two cats,
two horses, and a variety of wild foxes, squirrels, and rabbits.

xxvii

9780137521524_print.indb 27 01/02/22 8:43 PM


This page intentionally left blank

9780137521524_print.indb 4 01/02/22 8:43 PM


Introduction
In this Introduction, you will:
■■ Find out what is in this book.

■■ Have a peek at the future of VBA and Windows versions of Excel.

■■ Learn about special elements and typographical conventions in this book.

■■ Find out where to find code files for this book.

A s corporate IT departments have found themselves with long backlogs of requests,


Excel users have discovered that they can produce the reports needed to run their
businesses themselves using the macro language Visual Basic for Applications (VBA).
VBA enables you to achieve tremendous efficiencies in your day-to-day use of Excel. VBA
helps you figure out how to import data and produce reports in Excel so that you don’t
have to wait for the IT department to help you.

Is TypeScript a threat to VBA?


Your first questions are likely: “Should I invest time in learning VBA? How long will
Microsoft support VBA? Will the new TypeScript language released for Excel Online
replace VBA?”

Your investments in VBA will serve you well until at least 2049.

The last macro language change—from XLM to VBA—happened in 1993. XLM is


still supported in Excel to this day. That was a case where VBA was better than XLM, but
XLM is still supported 28 years later. Microsoft introduced TypeScript for Excel Online in
February 2020. I expect that they will continue to support VBA in the Windows and Mac
versions of Excel for the next 28 years.

In the Excel universe today, there are versions of Excel running in Windows, in MacOS,
on mobile phones powered by Android and iOS, and in modern browsers using Excel
Online. In my world, I use Excel 99% of the time on a Windows computer. There is per-
haps 1% of the time where I will open an Excel workbook on an iPad. But, if you are in a
mobile environment where you are using Excel in a browser, then the TypeScript UDFs
might be appropriate for you.

For an introduction to TypeScript UDFs in Excel, read Suat M. Ozgur’s Excel Custom
Functions Straight to the Point (ISBN 978-1-61547-259-8).
xxix

9780137521524_print.indb 29 01/02/22 8:43 PM


However, TypeScript performance is still horrible. If you don’t need your macros to
run in Excel Online, the VBA version of your macro will run eight times more quickly than
the TypeScript version. For people who plan to run Excel only on the Mac or Windows
platforms, VBA will be your go-to macro language for another decade.

The threat to Excel VBA is the new Excel Power Query tools found in the Get &
Transform tab of the Data tab in Excel for Windows. If you are writing macros to clean
imported data, you should consider cleaning the data once with Power Query and then
refreshing the query each day. I have a lot of Power Query workflows set up that would
have previously required VBA. For a primer on Power Query, check out Master Your Data
with Excel and Power BI: Leveraging Power Query to Get & Transform Your Task Flow by
Ken Puls and Miguel Escobar (ISBN 978-1-61547-058-7).

What is in this book?


You have taken the right step by purchasing this book. We can help you reduce the
learning curve so that you can write your own VBA macros and put an end to the burden
of generating reports manually.

Reducing the learning curve


This Introduction provides a case study about the power of macros. Chapter 1, “Unleashing
the power of Excel with VBA,” introduces the tools and confirms what you probably already
know: The macro recorder does not work reliably. Chapter 2, “This sounds like BASIC, so why
doesn’t it look familiar?,” helps you understand the crazy syntax of VBA. Chapter 3, “Referring
to ranges,” cracks the code on how to work efficiently with ranges and cells.

Chapter 4, “Looping and flow control,” covers the power of looping using VBA. The
case study in this chapter demonstrates creating a program to produce a department
report and then wrapping that report routine in a loop to produce 46 reports.

Chapter 5, “R1C1-style formulas,” covers, obviously, R1C1-style formulas. Chapter 6,


“Creating and manipulating names in VBA,” covers names. Chapter 7, “Event program-
ming,” includes some great tricks that use event programming. Chapter 8, “Arrays,” cov-
ers arrays. Chapter 9, “Creating classes and collections,” covers classes and collections.
Chapter 10, “Userforms: An introduction,” introduces custom dialog boxes that you can
use to collect information from a human using Excel.

Excel VBA power


Chapters 11, “Data mining with Advanced Filter,” and 12, “Using VBA to create pivot
tables,” provide an in-depth look at Filter, Advanced Filter, and pivot tables. Report

xxx Introduction

9780137521524_print.indb 30 01/02/22 8:43 PM


automation tools rely heavily on these concepts. Chapters 13, “Excel power,” and 14,
“Sample user-defined functions,” include dozens of code samples designed to exhibit the
power of Excel VBA and custom functions.

Chapters 15, “Creating charts,” through 20, “Automating Word,” handle charting, data
visualizations, web queries, sparklines, and automating Word.

Techie stuff needed to produce applications


Chapter 21, “Using Access as a back end to enhance multiuser access to data,” handles
reading and writing to Access databases and SQL Server. The techniques for using Access
databases enable you to build an application with the multiuser features of Access while
keeping the friendly front end of Excel.

Chapter 22, “Advanced userform techniques,” shows you how to go further with
userforms. Chapter 23, “The Windows Application Programming Interface (API),” teaches
some tricky ways to achieve tasks using the Windows API. Chapters 24, “Handling errors,”
through 26, “Creating Excel add-ins,” deal with error handling, custom menus, and add-
ins. Chapter 27, “An introduction to creating Office add-ins,” provides a brief introduc-
tion to building your own TypeScript application within Excel. Chapter 28, “What’s new in
Excel 365 and what’s changed,” summarizes the changes in Excel 365.

Does this book teach Excel?


Microsoft believes that the ordinary Office customer touches only 10% of the features in
Office. We realize that everyone reading this book is above average, and the visitors to
MrExcel.com are a pretty smart audience. Even so, a poll of 8,000 MrExcel.com readers
showed that only 42% of smarter-than-average users are using any 1 of the top 10 power
features in Excel.

Bill regularly presents a Power Excel seminar for accountants. These are hard-core
Excelers who use Excel 30 to 40 hours every week. Even so, two things come out in every
seminar. First, half of the audience gasps when they see how quickly you can do tasks
with a particular feature, such as automatic subtotals or pivot tables. Second, someone in
the audience routinely trumps me. For example, someone asks a question, I answer, and
someone in the second row raises a hand to give a better answer.

The point? Both the authors and the audience of this book know a lot about Excel.
However, we assume that in any given chapter, maybe 58% of the people have not used
pivot tables before and maybe even fewer have used the Top 10 Filter feature of pivot
tables. With this in mind, before we show how to automate something in VBA, we briefly
cover how to do the same task in the Excel interface. This book does not teach you how
to make pivot tables, but it does alert you when you might need to explore a topic and
learn more about it elsewhere.

Introduction xxxi

9780137521524_print.indb 31 01/02/22 8:43 PM


Case study: Monthly accounting reports
This is a true story. Valerie is a business analyst in the accounting department
of a medium-size corporation. Her company recently installed an overbudget
$16 million enterprise resource planning (ERP) system. As the project ground to
a close, there were no resources left in the IT budget to produce the monthly
report that this corporation used to summarize each department.

However, Valerie had been close enough to the implementation to think


of a way to produce the report herself. She understood that she could export
general ledger data from the ERP system to a text file with comma-separated
values. Using Excel, Valerie was able to import the general ledger data from the
ERP system into Excel.

Creating the report was not easy. As in many other companies, there were
exceptions in the data. Valerie knew that certain accounts in one particular
cost center needed to be reclassed as expenses. She knew that other accounts
needed to be excluded from the report entirely. Working carefully in Excel,
Valerie made these adjustments. She created one pivot table to produce the
first summary section of the report. She cut the pivot table results and pasted
them into a blank worksheet. Then she created a new pivot table report for the
second section of the summary. After about three hours, she had imported the
data, produced five pivot tables, arranged them in a summary, and neatly for-
matted the report in color.

Becoming the hero


Valerie handed the report to her manager. The manager had just heard from the
IT department that it would be months before they could get around to produc-
ing “that convoluted report.” When Valerie created the Excel report, she became
the instant hero of the day. In three hours, Valerie had managed to do the impos-
sible. Valerie was on cloud nine after a well-deserved “atta-girl.”

More cheers
The next day, Valerie’s manager attended the monthly department meeting.
When the department managers started complaining that they could not get
the report from the ERP system, this manager pulled out his department’s
report and placed it on the table. The other managers were amazed. How was
he able to produce this report? Everyone was relieved to hear that someone
had cracked the code. The company president asked Valerie’s manager if he
could have the report produced for each department.

xxxii Introduction

9780137521524_print.indb 32 01/02/22 8:43 PM


Cheers turn to dread
You can probably see what’s coming. This particular company had 46 depart-
ments. That means 46 one-page summaries had to be produced once a month.
Each report required importing data from the ERP system, backing out certain
accounts, producing five pivot tables, and then formatting the reports in color.
It had taken Valerie three hours to produce the first report, but after she got
into the swing of things, she could produce the 46 reports in 40 hours. Even
after she reduced her time per report, though, this is horrible. Valerie had a job
to do before she became responsible for spending 40 hours a month produc-
ing these reports in Excel.

VBA to the rescue


Valerie found Bill’s company, MrExcel Consulting, and explained her situation.
In the course of about a week, Bill was able to produce a series of macros in
Visual Basic that did all the mundane tasks. For example, the macros imported
the data, backed out certain accounts, made five pivot tables, and applied the
color formatting. From start to finish, the entire 40-hour manual process was
reduced to two button clicks and about 4 minutes.

Right now, either you or someone in your company is probably stuck doing
manual tasks in Excel that can be automated with VBA. We are confident that
we can walk into any company that has 20 or more Excel users and find a case
just as amazing as Valerie’s.

Versions of Excel
This seventh edition of VBA and Macros is designed to work with Microsoft 365 features
released up through August 2021. The previous editions of this book covered code for
Excel 97 through Excel 2019. In 80% of the chapters, the code today is identical to the
code in previous versions.

Differences for Mac users


Although Excel for Windows and Excel for the Mac are similar in terms of user interface,
there are a number of differences when you compare the VBA environment. Certainly,
nothing in Chapter 23 that uses the Windows API will work on the Mac. That said, the
overall concepts discussed in this book apply to the Mac. You can find a general list of
differences as they apply to the Mac at http://www.mrexcel.com/macvba.html. The VBA

Introduction xxxiii

9780137521524_print.indb 33 01/02/22 8:43 PM


Editor for the Mac does not let you design UserForms (Chapter 10). It also has a bug that
makes it difficult to create event handler macros (Chapter 7). Excel throws an error when
you try to select from the drop-downs at the top of the Code window. You have to first
copy and paste an empty event procedure; then the drop-downs will work.

Special elements and typographical conventions


The following typographical conventions are used in this book:

■■ Italic—Indicates new terms when they are defined, special emphasis, non-English
words or phrases, and letters or words used as words.

■■ Monospace—Indicates parts of VBA code, such as object or method names.

■■ Bold monospace—Indicates user input.

In addition to these typographical conventions, there are several special elements.


Each chapter has at least one case study that presents a real-world solution to common
problems. The case study also demonstrates practical applications of topics discussed in
the chapter.

In addition to the case studies, you will see Notes, Tips, and Cautions.

Note Notes provide additional information outside the main thread of the
chapter discussion that might be useful for you to know.

Tip Tips provide quick workarounds and time-saving techniques to help you
work more efficiently.

Caution Cautions warn about potential pitfalls you might encounter. Pay
attention to the Cautions; they alert you to problems that might otherwise
cause you hours of frustration.

xxxiv Introduction

9780137521524_print.indb 34 01/02/22 8:43 PM


About the companion content
As a thank-you for buying this book, we have put together a set of 50 Excel workbooks
that demonstrate the concepts included in this book. This set of files includes all the code
from the book, sample data, and additional notes from the authors.

To download the code files, visit this book’s webpage at


MicrosoftPressStore.com/ExcelVBAMacros365/downloads.

Errata, updates, and book support


We’ve made every effort to ensure the accuracy of this book and its companion
content. Any errors that have been reported since this book was published are listed at

MicrosoftPressStore.com/ExcelVBAMacros365/errata.

If you find an error that is not already listed, you can report it to us through the same
page.

For additional book support and information, please visit


MicrosoftPressStore.com/Support.

Please note that product support for Microsoft software and hardware is not offered
through the previous addresses. For help with Microsoft software or hardware, go to
http://support.microsoft.com.

Stay in touch
Let’s keep the conversation going! We’re on Twitter:

http://twitter.com/MicrosoftPress

http://twitter.com/MrExcel

Introduction xxxv

9780137521524_print.indb 35 01/02/22 8:43 PM


This page intentionally left blank

9780137521524_print.indb 4 01/02/22 8:43 PM


CHAPTER 3

Referring to ranges

In this chapter, you will:

■■ Learn how to reference the Range object

■■ Reference ranges in other sheets

■■ Reference a range relative to another range

■■ Use the Cells property to select a range

■■ Use the Offset property to refer to a range

■■ Use the Resize property to change the size of a range

■■ Use the Columns and Rows properties to specify a range

■■ Use the Union method to join multiple ranges

■■ Use the Intersect method to create a new range from overlapping ranges

■■ Use the IsEmpty function to check whether a cell is empty

■■ Use the CurrentRegion property to select a data range

■■ Use the SpecialCells property to interact with specific cells in a range

■■ Use the Areas collection to return a noncontiguous range

■■ Learn the syntax used for tables

A range can be a cell, a row, a column, or a grouping of any of these. The Range object is probably the
most frequently used object in Excel VBA; after all, you’re manipulating data on a sheet. Although a
range can refer to any grouping of cells on a sheet, it can refer to only one sheet at a time. If you want
to refer to ranges on multiple sheets, you must refer to each sheet separately.

This chapter shows you different ways of referring to ranges, such as specifying a row or column.
You’ll also find out how to manipulate cells based on the active cell and how to create a new range
from overlapping ranges.

53

9780137521524_print.indb 53 01/02/22 8:43 PM


The Range object
The following is the Excel object hierarchy:

Application > Workbook > Worksheet > Range

The Range object is a property of the Worksheet object. This means it requires that a sheet be active
or else it must reference a worksheet. Both of the following lines mean the same thing if Worksheets(1)
is the active sheet:

Range("A1")
Worksheets(1).Range("A1")

There are several ways to refer to a Range object. Range("A1") is the most identifiable because that is
how the macro recorder refers to it. However, all the following are equivalent when referring to cell D5:

Range("D5")
[D5]
Range("B3").Range("C3")
Cells(5,4)
Range("A1").Offset(4,3)
Range("MyRange") 'assuming that D5 has a Name of MyRange

Which format you use depends on your needs. Keep reading. It will all make sense soon!

Syntax for specifying a range


The Range property has two acceptable syntaxes. To specify a rectangular range in the first syntax,
specify the complete range reference just as you would in a formula in Excel:

Range("A1:B5")

In the alternative syntax, specify the upper-left corner and lower-right corner of the desired rectan-
gular range. In this syntax, the equivalent statement might be this:

Range("A1", "B5")

For either corner, you can substitute a named range, the Cells property, or the ActiveCell prop-
erty. The following line of code selects the rectangular range from A1 to the active cell:

Range("A1", ActiveCell).Select

The following statement selects from the active cell to five rows below the active cell and two col-
umns to the right:

Range(ActiveCell, ActiveCell.Offset(5, 2)).Select

54 Chapter 3 Referring to ranges

9780137521524_print.indb 54 01/02/22 8:43 PM


Referencing named ranges
You probably have already used named ranges on your worksheets and in formulas. You can also use
them in VBA.

Use the following code to refer to the range "MyRange" in Sheet1:

Worksheets("Sheet1").Range("MyRange")

Notice the name of the range is in quotes—unlike the use of named ranges in formulas on the sheet
itself. If you forget to put the name in quotes, Excel thinks you are referring to a variable in the pro-
gram. One exception is if you use the shortcut syntax discussed in the next section. In that case, quotes
aren’t used.

Shortcut for referencing ranges


A shortcut is available when referencing ranges. The shortcut involves using square brackets, as shown
in Table 3-1.

TABLE 3-1 Shortcuts for referencing ranges

Standard Method Shortcut

Range("D5") [D5]

Range("A1:D5") [A1:D5]

Range("A1:D5, G6:I17") [A1:D5, G6:I17]

Range("MyRange") [MyRange]

Referencing ranges in other sheets


Switching between sheets by activating the needed sheet slows down your code. To avoid this, refer to
a sheet that is not active by first referencing the Worksheet object:

Worksheets("Sheet1").Range("A1")

This line of code references Sheet1 of the active workbook even if Sheet2 is the active sheet.

To reference a range in another workbook, include the Workbook object, the Worksheet object, and
then the Range object:

Workbooks("InvoiceData.xlsx").Worksheets("Sheet1").Range("A1")

To use the Range property as an argument within another Range property, identify the range fully each
time. For example, suppose that Sheet1 is your active sheet and you need to total data from Sheet2:

WorksheetFunction.Sum(Worksheets("Sheet2").Range(Range("A1"), _
Range("A7")))

Chapter 3 Referring to ranges 55

9780137521524_print.indb 55 01/02/22 8:43 PM


This line does not work. Why not? Although Range("A1"), Range("A7") is meant to refer to the
sheet at the beginning of the code line (Sheet2), Excel does not assume that you want to carry the
Worksheet object reference over to these other Range objects; instead, Excel assumes that they refer to
the active sheet, Sheet1. So, what do you do? Well, you could write this:

WorksheetFunction.Sum(Worksheets("Sheet2").Range(Worksheets("Sheet2"). _
Range("A1"), Worksheets("Sheet2").Range("A7")))

However, not only is this a long line of code, but it is also difficult to read! Thankfully, there is a
simpler way, using With...End With:

With Worksheets("Sheet2")
WorksheetFunction.Sum(.Range(.Range("A1"), .Range("A7")))
End With

Notice now there is a .Range in your code but without the preceding object reference. That’s
because With Worksheets("Sheet2") implies that the object of the range is that worksheet. Whenever
Excel sees a period without an object reference directly to the left of it, it looks up the code for the
closest With statement and uses that as the object reference.

Referencing a range relative to another range


Typically, the Range object is a property of a worksheet. It is also possible to have Range be the prop-
erty of another range. In this case, the Range property is relative to the original range, which makes for
unintuitive code. Consider this example:

Range("B5").Range("C3").Select

This code actually selects cell D7. Think about cell C3, which is located two rows below and two
columns to the right of cell A1. The preceding line of code starts at cell B5. If we assume that B5 is
in the A1 position, VBA finds the cell that would be in the C3 position relative to B5. In other words,
VBA finds the cell that is two rows below and two columns to the right of B5, which is D7.

Again, I consider this coding style to be very unintuitive. This line of code mentions two addresses,
and the actual cell selected is neither of these addresses! It seems misleading when you’re trying to
read this code.

You might consider using this syntax to refer to a cell relative to the active cell. For example, the
following line of code activates the cell three rows down and four columns to the right of the currently
active cell:

Selection.Range("E4").Select

I mention this syntax only because the macro recorder uses it. Recall that when you recorded a
macro in Chapter 1, “Unleashing the power of Excel with VBA,” with relative references on, the following
line was recorded:

ActiveCell.Offset(0, 4).Range("A1").Select

56 Chapter 3 Referring to ranges

9780137521524_print.indb 56 01/02/22 8:43 PM


This line found the cell four columns to the right of the active cell, and from there, it selected the
cell that would correspond to A1. This is not the easiest way to write code, but it is the way the macro
recorder does it.

Although a worksheet is usually the object of the Range property, occasionally, such as during
recording, a range may be the property of a range.

Using the Cells property to select a range


The Cells property refers to all the cells of the specified Range object, which can be a worksheet or a
range of cells. For example, this line selects all the cells of the active sheet:

Cells.Select

Using the Cells property with the Range object might seem redundant:

Range("A1:D5").Cells

This line refers to the original Range object. However, the Cells property has an Item property that
makes the Cells property very useful. The Item property enables you to refer to a specific cell relative
to the Range object.

The syntax for using the Item property with the Cells property is as follows:

Cells.Item(Row,Column)

You must use a numeric value for Row, but you may use the numeric value or string value for Column.
Both of the following lines refer to cell C5:

Cells.Item(5,"C")
Cells.Item(5,3)

Because the Item property is the default property of the Range object, you can shorten these lines as
follows:

Cells(5,"C")
Cells(5,3)

The ability to use numeric values for parameters is particularly useful if you need to loop through
rows or columns. The macro recorder usually uses something like Range("A1").Select for a single cell
and Range("A1:C5").Select for a range of cells. If you’re learning to code only from the recorder, you
might be tempted to write code like this:

FinalRow = Cells(Rows.Count, 1).End(xlUp).Row


For i = 1 to FinalRow
Range("A" & i & ":E" & i).Font.Bold = True
Next i

Chapter 3 Referring to ranges 57

9780137521524_print.indb 57 01/02/22 8:43 PM


This little piece of code, which loops through rows and bolds the cells in columns A through E, is
awkward to read and write. But how else can you do it? Like this:

FinalRow = Cells(Rows.Count, 1).End(xlUp).Row


For i = 1 to FinalRow
Cells(i,"A").Resize(,5).Font.Bold = True
Next i

Instead of trying to type the range address, the new code uses the Cells and Resize properties to
find the required cell, based on the active cell. See the “Using the Resize property to change the size of
a range” section later in this chapter for more information on the Resize property.

You can use the Cells properties for parameters in the Range property. The following refers to the
range A1:E5:

Range(Cells(1,1),Cells(5,5))

This is particularly useful when you need to specify variables with a parameter, as in the previous
looping example.

Using the Offset property to refer to a range


You’ve already seen a reference to Offset when you recorded a relative reference. Offset enables you
to manipulate a cell based on the location of another cell, such as the active cell. Therefore, you do not
need to know the address of the cell you want to manipulate.

The syntax for the Offset property is as follows:

Range.Offset(RowOffset, ColumnOffset)

For example, the following code affects cell F5 from cell A1:

Range("A1").Offset(RowOffset:=4, ColumnOffset:=5)

Or, shorter yet, you can write this:

Range("A1").Offset(4,5)

The count of the rows and columns starts at A1 but does not include A1.

If you need to go over only a row or a column, but not both, you don’t have to enter both the row
and the column parameters. To refer to a cell one column over, use one of these lines:

Range("A1").Offset(ColumnOffset:=1)
Range("A1").Offset(,1)

58 Chapter 3 Referring to ranges

9780137521524_print.indb 58 01/02/22 8:43 PM


Both of these lines have the same meaning, so the choice is yours. If you use the second line, make
sure to include the comma so Excel knows that the 1 refers to the ColumnOffset argument. Referring to
a cell one row up is similar:

Range("B2").Offset(RowOffset:=-1)
Range("B2").Offset(-1)

Once again, you can choose which one to use. It’s a matter of the readability of the code.

Suppose you have a list of produce in column A, with totals next to the produce items in column B. If
you want to find any total equal to zero and place LOW in the cell next to it, do this:

Set Rng = Range("B1:B16").Find(What:="0", LookAt:=xlWhole, _


LookIn:=xlValues)
Rng.Offset(, 1).Value = "LOW"

When used in a Sub and looping through a data set, it would look like this:

Sub FindLow()
With Range("B1:B16")
Set Rng = .Find(What:="0", LookAt:=xlWhole, LookIn:=xlValues)
If Not Rng Is Nothing Then
firstAddress = Rng.Address
Do
Rng.Offset(, 1).Value = "LOW"
Set Rng = .FindNext(Rng)
Loop While Not Rng Is Nothing And Rng.Address <> firstAddress
End If
End With
End Sub

The LOW totals are noted by the program, as shown in Figure 3-1.

FIGURE 3-1 The code puts “LOW” next to the zeros in the data set.

Note Refer to the section “Object variables” in Chapter 4, “Looping and flow control,” for
more information on the Set statement.

Offsetting isn’t only for single cells; you can use it with ranges. You can shift the focus of a range
over in the same way you can shift the active cell. The following line refers to B2:D4 (see Figure 3-2):

Range("A1:C3").Offset(1,1)

Chapter 3 Referring to ranges 59

9780137521524_print.indb 59 01/02/22 8:43 PM


FIGURE 3-2 Offsetting the original range A1:C3 by one row and one column references a new range, B2:D4.

Using the Resize property to change the size of a range


The Resize property enables you to change the size of a range based on the location of the active cell.
You can create a new range as needed. This is the syntax for the Resize property:

Range.Resize(RowSize, ColumnSize)

To reference the range B3:D13, use the following:

Range("B3").Resize(RowSize:=11, ColumnSize:=3)

Here’s a simpler way to reference this range:

Range("B3").Resize(11, 3)

But what if you need to resize by only a row or a column—not both? You don’t have to enter both
the row and the column parameters.

To expand by two columns, use either of the following:

Range("B3").Resize(ColumnSize:=2)

or

Range("B3").Resize(,2)

Both lines mean the same thing. The choice is yours. If you use the second line, make sure to include
the comma so Excel knows the 2 refers to the ColumnSize argument. Resizing just the rows is similar.
You can use either of the following:

Range("B3").Resize(RowSize:=2)

or

Range("B3").Resize(2)

Once again, the choice is yours. It is a matter of the readability of the code.

60 Chapter 3 Referring to ranges

9780137521524_print.indb 60 01/02/22 8:43 PM


From the list of produce, say that you want to find the zero totals and color the cells of the total and
corresponding produce (see Figure 3-3). Here’s what you do:

Set Rng = Range("B1:B16").Find(What:="0", LookAt:=xlWhole, _


LookIn:=xlValues)
Rng.Offset(, -1).Resize(, 2).Interior.ColorIndex = 15

FIGURE 3-3 You can resize a range to extend the selection.

Notice that the Offset property first moves the active cell over to the produce column. When you’re
resizing, the upper-left-corner cell must remain the same.

Resizing isn’t only for single cells; you can use it to resize an existing range. For example, if you have
a named range but need it and the column next to it, use this:

Range("Produce").Resize(,2)

Remember, the number you resize by is the total number of rows/columns you want to include.

Using the Columns and Rows properties to specify a range


The Columns and Rows properties refer to the columns and rows of a specified Range object, which can
be a worksheet or a range of cells. They return a Range object referencing the rows or columns of the
specified object.

You’ve seen the following line used, but what is it doing?

FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

This line of code finds the last row in a sheet in which column A has a value and places the row
number of that Range object into the variable called FinalRow. This can be useful when you need to
loop through a sheet row by row; you will know exactly how many rows you need to go through.

Note Some properties of columns and rows require contiguous rows and columns in order
to work properly. For example, if you were to use the following line of code, 9 would be the
answer because only the first range would be evaluated:
Range("A1:B9, C10:D19").Rows.Count

However, if the ranges were grouped separately, the answer would be 19. Excel takes the
top-left cell address, A1, and the bottom-right cell address, D19, and counts the rows in the
range A1:D19:
Range("A1:B9", "C10:D19").Rows.Count

Chapter 3 Referring to ranges 61

9780137521524_print.indb 61 01/02/22 8:43 PM


Using the Union method to join multiple ranges
The Union method enables you to join two or more noncontiguous ranges. It creates a temporary
object of the multiple ranges, which enables you to affect them at the same time:

Application.Union(argument1, argument2, etc.)

The expression Application is not required. The following code joins two named ranges on the
sheet, inserts the =RAND() formula, and bolds them:

Set UnionRange = Union(Range("Range1"), Range("Range2"))


With UnionRange
.Formula = "=RAND()"
.Font.Bold = True
End With

Using the Intersect method to create a new range from


overlapping ranges
The Intersect method returns the cells that overlap between two or more ranges. If there is no over-
lap, an error is returned:

Application.Intersect(argument1, argument2, etc.)

The expression Application is not required. The following code colors the overlapping cells of the
two ranges:

Set IntersectRange = Intersect(Range("Range1"), Range("Range2"))


IntersectRange.Interior.ColorIndex = 6

Using the IsEmpty function to check whether a cell is empty


The IsEmpty function returns a Boolean value that indicates whether a single cell is empty: True if
empty, and False if not. The cell must truly be empty for the function to return True. If it contains even
just a space that you cannot see, Excel does not consider the cell to be empty:

IsEmpty(Cell)

62 Chapter 3 Referring to ranges

9780137521524_print.indb 62 01/02/22 8:43 PM


Say that you have several groups of data separated by a blank row. You want to make the separa-
tions a little more obvious. The following code goes down the data in column A. When it finds an
empty cell in column A, it colors in the first four cells of that row (see Figure 3-4):

LastRow = Cells(Rows.Count, 1).End(xlUp).Row


For i = 1 To LastRow
If IsEmpty(Cells(i, 1)) Then
Cells(i, 1).Resize(1, 4).Interior.ColorIndex = 1
End If
Next i

FIGURE 3-4 You can make separations more obvious by using colored rows.

Using the CurrentRegion property to select a data range


CurrentRegion returns a Range object that represents a set of contiguous data. As long as the data
is surrounded by one empty row and one empty column, you can select the data set by using
CurrentRegion:

RangeObject.CurrentRegion

The following line selects A1:D3 because this is the contiguous range of cells around cell A1 (see
Figure 3-5):

Range("A1").CurrentRegion.Select

This is useful if you have a data set whose size is in constant flux.

FIGURE 3-5 You can use CurrentRegion to select a range of contiguous data around the active cell.

Chapter 3 Referring to ranges 63

9780137521524_print.indb 63 01/02/22 8:43 PM


Case Study: Using the SpecialCells method to select specific cells
Even Excel power users might not have encountered the Go To Special dialog box. If you
press the F5 key in an Excel worksheet, you get the normal Go To dialog box (see Figure 3-6).
In the lower-left corner of this dialog box is a button labeled Special. Click this button to get
to the super-powerful Go To Special dialog box (see Figure 3-7).

FIGURE 3-6 Although the Go To dialog box doesn’t seem useful, click the Special button in the lower-left corner to
specify what type of cells to select.

In the Excel interface, the Go To Special dialog box enables you to select only cells with
formulas, only blank cells, or only the visible cells. Selecting only visible cells is excellent for
grabbing the visible results of AutoFiltered data. If you already have a range highlighted, only
cells within this range meeting the criteria will be selected. Make sure only one cell is selected
to search the entire sheet.

To simulate the Go To Special dialog box in VBA, use the SpecialCells method. This enables
you to act on cells that meet certain criteria, like this:
RangeObject.SpecialCells(Type, Value)

FIGURE 3-7 The Go To Special dialog box has many incredibly useful selection tools, such as one for selecting only
the formulas on a sheet.

64 Chapter 3 Referring to ranges

9780137521524_print.indb 64 01/02/22 8:44 PM


The SpecialCells method has two parameters: Type and Value. Type is one of the xlCellType
constants:
xlCellTypeAllFormatConditions
xlCellTypeAllValidation
xlCellTypeBlanks
xlCellTypeComments
xlCellTypeConstants
xlCellTypeFormulas
xlCellTypeLastCell
xlCellTypeSameFormatConditions
xlCellTypeSameValidation
xlCellTypeVisible

Set one of the following optional Value constants if you use xlCellTypeConstants or
xlCellTypeFormulas:

xlErrors
xlLogical
xlNumbers
xlTextValues

The following code returns all the ranges that have conditional formatting. It produces an
error if there are no conditional formats and adds a border around each contiguous section it
finds:
Set rngCond = ActiveSheet.Cells.SpecialCells(xlCellTypeAllFormatConditions)
If Not rngCond Is Nothing Then
rngCond.BorderAround xlContinuous
End If

Have you ever had someone send you a worksheet without all the labels filled in? Some
people think that the data shown in Figure 3-8 looks tidy. They enter the Region field only once
for each region. This might look aesthetically pleasing, but it’s impossible to sort.

FIGURE 3-8 The blank cells in the Region column make it difficult to sort data sets such as this.

Using the SpecialCells method to select all the blanks in this range is one way to fill the
blank region cells quickly using the region found above them:
Sub FillIn()
On Error Resume Next 'Need this because if there aren't any blank
'cells, the code will error
Range("A1").CurrentRegion.SpecialCells(xlCellTypeBlanks).FormulaR1C1 _
= "=R[-1]C"
Range("A1").CurrentRegion.Value = Range("A1").CurrentRegion.Value
End Sub

Chapter 3 Referring to ranges 65

9780137521524_print.indb 65 01/02/22 8:44 PM


In this code, Range("A1").CurrentRegion refers to the contiguous range of data in the report.
The SpecialCells method returns just the blank cells in that range. This particular formula fills
in all the blank cells with a formula that points to the cell above the blank cell. (You can read
more about R1C1-Style Formulas in Chapter 5, “R1C1-style formulas.”) The second line of code is
a fast way to simulate using the Copy and Paste Special Values commands. Figure 3-9 shows the
results.

FIGURE 3-9 After the macro runs, the blank cells in the Region column have been filled with data.

Using the Areas collection to return a noncontiguous range


The Areas collection is a collection of noncontiguous ranges within a selection. It consists of individual
Range objects representing contiguous ranges of cells within the selection. If a selection contains only
one area, the Areas collection contains a single Range object that corresponds to that selection.

You might be tempted to loop through the rows in a sheet and check the properties of a cell in a row,
such as its formatting (for example, font or fill) or whether the cell contains a formula or value. Then you
could copy the row and paste it to another section. However, there is an easier way. In Figure 3-10, the
user enters the values below each fruit and vegetable. The percentages are formulas. The following line
of code selects the cells with numeric constants and copies them to another area:

Set NewDestination = ActiveSheet.Range("I1")


For Each Rng In Cells.SpecialCells(xlCellTypeConstants, 1).Areas
Rng.Copy Destination:=NewDestination
Set NewDestination = NewDestination.Offset(Rng.Rows.Count)
Next Rng

FIGURE 3-10 The Areas collection makes it easier to manipulate noncontiguous ranges.

66 Chapter 3 Referring to ranges

9780137521524_print.indb 66 01/02/22 8:44 PM


Referencing tables
A table is a special type of range that offers the convenience of referencing named ranges. However,
tables are not created in the same manner as other ranges. For more information on how to create a
named table, see Chapter 6, “Creating and manipulating names in VBA.”

Although you can reference a table by using Worksheets(1).Range("Table1"), you have access to
more of the properties and methods that are unique to tables if you use the ListObjects object, like
this:

Worksheets(1).ListObjects("Table1")

This opens the properties and methods of a table, but you can’t use that line to select the table.
To do that, you have to specify the part of the table you want to work with. To select the entire table,
including the header and total rows, specify the Range property:

Worksheets(1).ListObjects("Table1").Range.Select

The table part properties include the following:

■■ Range—Returns the entire table.

■■ DataBodyRange—Returns the data part only.

■■ HeaderRowRange—Returns the header row only.

■■ TotalRowRange—Returns the total row only.

What I really like about coding with tables is the ease of referencing specific columns of a table.
You don’t have to know how many columns to move in from a starting position or the letter/number
of the column, and you don’t have to use a FIND function. Instead, you can use the header name of
the column. For example, to select the data of the Qty column of the table, but not the header or total
rows, do this:

Worksheets(1).ListObjects("Table1").ListColumns("Qty")_
.DataBodyRange.Select

Note For more details on coding with tables, check out Excel Tables: A Complete Guide
for Creating, Using, and Automating Lists and Tables by Zack Barresse and Kevin Jones
(ISBN: 9781615470280).

Chapter 3 Referring to ranges 67

9780137521524_print.indb 67 01/02/22 8:44 PM


Next steps
Referencing ranges is an essential part of programming in Excel. Now that you’re getting an idea of
how Excel works, it’s time to learn about a fundamental component of any programming language:
loops. If you have taken a programming class, you will be familiar with basic loop structures. VBA sup-
ports all the usual loops. Chapter 4 also describes a special loop, For Each...Next, which is unique to
object-oriented programming such as VBA.

68 Chapter 3 Referring to ranges

9780137521524_print.indb 68 01/02/22 8:44 PM


Index

Numerics converting workbooks to, 540–541


creating with VB Editor, 542–543
3D models, 575
hidden workbook as an alternative, 545–547
24-hour time, 117–118
installing, 543–544
removing, 545
A saving files as, 541–542
A1 addressing, 87, 89–91, 94–95 security, 544–545
above/below average rules, 340, 355. See also conditional Office, 549
formatting adding interactivity, 554–557, 559–560
absolute references, R1C1 addressing and, 92–93 defining, 558–559
Access Hello World, 550, 551–554
creating a shared database, 449–450 JavaScript and, 569–570
database writing to the content or task pane, 569
adding records to a, 452–453 AddTop10 method, 355
deleting records via ADO, 458 AddUniqueValues method, 356–358
retrieving records from, 453–455 ADOs (ActiveX Data Objects), 448
summarizing records via ADO, 458–459 adding tables on the fly, 461–462
updating an existing record, 455–458 checking for the existence of a field, 461
MDB (multidimensional database) files, 447–448 checking for the existence of a table, 460
reading from, 451 connection, 450, 451
tables cursor, 450–451
adding fields on the fly, 462–463 deleting records from an Access database, 458
adding on the fly, 461–462 lock type, 451
checking for the existence of, 460 record set, 450
checking for the existence of a field, 461 summarizing records via, 458–459
operations, 451 Advanced Filter, 183
ACE engine, 447–448 building, 184
ActiveX controls criteria ranges and, 191–192, 193
minimizing duplicate code, 147–148 extracting a unique list of values, 185
running a macro with, 536 changing the list range to a single column, 185
AddAboveAverage method, 355 copying the customer heading before filtering, 185–186
.AddChart2 method, 313, 314–315, 318–319, 337–338 Filter In Place, 201
adding catching no records, 201–202
color scales to a range, 346–347 showing all records, 202
data bars to a range, 342–346 formula-based conditions, 194–195
icon sets to a range, 347–350 returning above-average records, 200
names, 98–100 using in the Excel user interface, 195–196
add-ins, 539 using with VBA, 196–200
Excel joining multiple criteria
characteristics of, 539–540 with a logical AND, 193
closing, 545 with a logical OR, 192

579

9780137521524_print.indb 579 01/02/22 8:49 PM


Advanced Filter

replacing a list of values with a condition created replacing loops with, 175–178
from a formula, 193–194 selecting a dynamic date range, 180–181
retrieving unique combinations of two or more fields, selecting multiple items, 178–179
190
selecting using the Search box, 179
xlFilterCopy, 202
turning off drop-down menus, 209–210
combining multiple techniques, 205–209
AutoSort method, 226
copying a subset of columns and reordering,
Autosum button, 24–25, 26
203–205
copying all columns, 202–203
alpha characters, sorting, 301–303 B
API (Application Programming Interface), 491. See also
backward compatibility, .AddChart2 method,
Spotify
337–338
declarations, 492
Barresse, Z., 263, 278
32-bit- and 64-bit-compatible, 493–494
Excel Tables: A Complete Guide for Creating, Using,
private, 492 and Automating Lists and Tables, 67
using, 493 barriers to learning VBA, 2. See also learning VBA
functions BASIC, 2, 28
checking whether an Excel file is open on a binding. See early binding; late binding, 499
network, 495–496
blocks, With…End With, 50
creating a running timer, 498–499
bookmarks, 441–442
customizing the About dialog box, 497
BookOpen() function, 288–289
disabling the X for closing a user form, 498
borders, chart, 331
playing sounds, 499
breakpoints, 40, 41
retrieving display-resolution information,
backing up or moving forward, 40
496–497
querying
retrieving the computer name, 495
by hovering, 42
getting credentials for accessing, 386–387
using a Watches window, 42–43
applications, events, 120–124
using the Immediate window, 41–42
setting up a class module, 134
setting with watches, 43
trapping, 134–135
Bricklin, D., 87
Archibald, R., 260
building
Areas collection, 66
advanced filters, 184
arrays, 125, 145
Data Model, 247–249
declaring, 125–126
LAMBDA functions, 307–308
dynamic, 130–131
pivot tables, 245–246, 255–256
filling, 127–128
UDFs (user-defined functions), 286–287
formulas, 573
buttons
JavaScript, 562–563
Autosum, 24–25, 26
multidimensional, declaring, 126–127
command, 160, 161–162
names and, 104
command, running a macro with, 534–535
passing, 131–132
creating, 557–558
retrieving data from, 128–129
Help, 576
speeding up code with, 129–130
option
assigning, macros
adding to a userform, 165–166
to a form control, 12–13
events, 166
to an object, 13
spin, 168–169
AutoFilter
toggle, 471–472
filtering
using images on, 527
by color, 179
by icon, 179–180

580

9780137521524_print.indb 580 01/02/22 8:49 PM


collection(s)

C class modules, 133


application events, trapping, 134–135
calculated fields, 249–250
clsCtlColor, 485–486
calculated items, 250
collections, creating, 142–144
.Calculation property, 223
custom objects
calculations, changing to show percentages, 223–225
creating, 137–139
calling, userforms, 156
using, 139–140
canceling a scheduled macro, 408
embedded chart events, trapping, 136–137
Case statement, complex expressions in, 84
Excel state, 268–270
cell pointer, 20–21, 23
inserting, 133–134
cells
minimizing duplicate code for ActiveX labels,
creating a progress indicator, 274–275 147–148
duplicate, marking, 356–358 clauses
empty, 62–63, 226 Step, 72–73
formatting, 243–244 Until, 77–79
based on their value, 358 While, 77–79
that contain blanks or errors, 359 cleaning up code, 45–52
that contain dates, 359 Close method, 436–437
that contain text, 358–359 closing, add-ins, 545
using a formula, 359–361 code. See also M; XML
notes, placing charts in, 265–267 adding to new workbooks, 279–280
returning the column letter of an address, 305–306 breakpoints, 40
selecting visible only, 181–182 cleaning up, 45–52
Cells property, referencing ranges with, 57–58 comments, 18
ChartFormat object, 327 duplicate, minimizing, 147–148
charts, 313, 574. See also sparklines early binding, referencing a Word object,
applying a color, 320–321 427–430
borders, 331 examining, 17–19, 33–34
combo, 331–334 Help topics, 37
creating JavaScript, 560–561
macro recorder and, 318 late binding, referencing a Word object, 430
using .AddChart2 method, 314–315 picture catalog userform, 481–483
embedded, events, 118–120 pivot tables
events, 118, 136–137 building with a timeline, 243
exporting as a graphic, 337 building with two slicers, 238–240
filtering, 322 creating a Data model, 247–249
formatting, 327–328 creating a static summary from, 218–219
line settings, 331 generating, 216–217
map, creating, 335 producing one report per product, 227–229
placing in a cell note, 265–267 protecting, 513–514
referring to, 318–319 speeding up with arrays, 129–130
SetElement method, 323–327 Step Into feature, 38–40
sheet events, 119–120 stepping through, 40–41. See also breakpoints
specifying a title, 319–320 streamlining, 46
styles, 315–318 TypeScript, creating a pivot table, 255
trendline, formatting, 331 VBA, 27
waterfall, creating, 336–337 collection(s), 29, 140
win/loss, formatting, 377–378 Areas, 66
check boxes, 165, 466–468 controls and, 473–475

581

9780137521524_print.indb 581 01/02/22 8:49 PM


collection(s)

creating, 140–141 constants


in a class module, 142–144 Help topics, 35
in a standard module, 141–142 icon sets, 348–349
dictionaries and, 145 retrieving the real value of
FormatConditions, 340–341 using the Object Browser, 433–434
ColName() function, 305–306 using the Watches window, 433
color(s) SetElement method, 323–327
adding to fields in a userform, 485–486 values, 433
applying xlColumnDataType, 416–417
to a chart, 320–321 ContainsText() function, 303–304
to data bars, 343, 344–345, 352–354 controls
to objects, 329 accelerator keys, 483–484
filtering by, 179 adding
RGB, applying to sparklines, 373–374 on the fly, 479
scales, 339, 346–347 at runtime, 477–478
theme, applying to sparklines, 369–372 adding to a ribbon, 520
Columns property, 61 attributes, 521–525
combining bug fix when adding to a form, 159
forms, 169–171 check boxes, 466–468
workbooks, 262–263 collections and, 473–475
combo boxes, 162–163, 164–165 combo boxes, 162–163, 164–165
combo charts, creating, 331–334 command buttons, 160, 161–162
command buttons, 160 early binding, New keyword, 430–431
events, 161–162 frames, 474, 475
running a macro with, 534–535 graphic, 167
comments, 18 labels, 159, 161–162
HTML, 557 list boxes, 162–163
JavaScript, 556, 561 events, 164–165
names and, 100 multicolumn, 486–487
XML, 519 MultiSelect property, 163–164
comparing, VBA and TypeScript, 253–255 MultiPage, 169–171, 468
concatenation, 300–301 option buttons, 165–166
conceptual filter, 230–231 ProgIds, 480
conditional formatting, 340–341 programming, 158–159
above/below average rules, 355 RefEdit, 470–471
data bars, 352–354 renaming, 159
icons sets, 351–352 scrollbars, 472–473
marking unique or duplicate cells, 356–358 spin buttons, 168–169
NumberFormat property, 361–362 tab strips, 468–470
top/bottom rules, 355–356 text boxes, 160, 161–162
using a formula, 359–361 tip text, adding to userforms, 484
conditions, 81 toggle button, 471–472
formula-based ConvertWeekDay() function, 299–300
returning above-average records, 200 copy and paste, 49. See also xlFilterCopy
using in the Excel user interface, 195–196 Copy method, 49
using with VBA, 196–200 CopyFromRecordSet method, 453, 455
formulas-based, 194–195 Count function, 221
configuration, pivot table, 213–214 COUNTIF function, 360

582

9780137521524_print.indb 582 01/02/22 8:49 PM


Developer tab

counting, unique functions, 296


CreateObject function, 431
D
DAOs (data access objects), 448
CreatePivotTable method, 213–214
dashboards
creating
creating, 378
cell progress indicator, 274–275
creating individual sparklines in, 379–383
charts
placing query results on, 398–401
combo, 331–334
data bars, 339
map, 335
adding to a range, 342–346
using .AddChart2 method, 314–315
color, applying, 344–345
waterfall, 336–337
using two colors in a range, 352–354
collections, 140–141
Data Model, 244
in a class module, 142–144
creating, 247–249
in a standard module, 141–142
loading large files to, 423–424
custom objects, 137–139
tables
custom properties, 148–151
adding, 244–245
dashboards, 378, 379–383
creating a relationship between, 245
Data Model, 247–249
data sets, converting fixed-width reports to, 280–283
macro button
data visualizations, 252–253, 339
on the Quick Access Toolbar, 11–12
above/below average rules, 340
on the ribbon, 10–11
color scales, 339
new instances of objects, 431
conditional formatting, 340–341
pivot tables in VBA, 212
data bars, 339
adding fields to the data area, 214–217
highlight cells, 340
configuration, 213–214
icon sets, 340
defining the pivot cache, 213
top/bottom rules, 340
shared Access database, 449–450
date(s)
sparklines, 363–365, 379–383
converting week number to, 299–300
UDFs (user-defined functions), 285
dynamic filters and, 180–181
userforms, 155–156
grouping to months, quarters, or years, 221–223
criteria
of last save, retrieving, 292
complex ranges, 193
retrieving, 292–293
logical AND, 193
debugging tools
logical OR, 192
breakpoints, 40, 41
ranges, 191–192
backing up or moving forward, 40
replacing a list of values with a condition created
from a formula, 193–194 queries and, 41–43
CSS (Cascading Style Sheets), 558 Step Into feature, 38–41
CSV files declaring
importing and deleting, 260 arrays, 125–127
opening, 417–419 UDTs (user-defined types), 149
CurrentRegion property, selecting ranges with, 63 Delete method, 100
custom objects deleting, names, 100
creating, 137–139 delimited files, opening, 417–419
using, 139–140 Design tab, changing the layout, 250–251
custom properties, creating, 148–151 Developer tab
Add-ins group, 4
Code group, 4

583

9780137521524_print.indb 583 01/02/22 8:49 PM


Developer tab

Controls group, 4 Excel versions and, 515–516


Disable All Macros with Notification setting, 8 generic, 506
displaying, 3–4 ignoring errors, 506–507
macro settings, 7–8 message boxes and, 509
Modify group, 4 misleading errors, 503–504
Relative References, 21–24 page setup, 507–508
XML group, 4 Power Query, 403
dialog boxes protecting code and, 513–514
Advanced Filter, 184 runtime error 9, 511–512
File Open, 173–174 runtime error 1004, 512–513
Go To Special, 64, 201 suppressing Excel warnings, 508
loops and, 182–183 events, 112
Visible Cells Only option, 181–182 application, 120–124
Name Manager, 97 setting up a class module, 134
Record Macro, filling out, 9–10 trapping, 134–135
Show Values As tab of the Value Field Settings, 223 Change, 267
dictionaries, 145–146 chart, 118
DIM statement, 79–80 embedded, 118–120
displaying, Developer tab, 3–4 trapping, 136–137
DLLs (Dynamic Link Libraries), 491 check box, 467–468
Document object, 435–437 combo box, 164–165
Do…loops, 75–77 command button, 161–162
stopping, 77 enabling, 113
While and Until clauses, 77–79 graphic control, 167
drilling-down a pivot table, 270–271 label, 161–162
duplicate levels of, 111–112
cells, marking, 356–358 list box, 164–165
code, minimizing, 147–148 MultiPage, 171
values, 340 option button, 166
dynamic arrays, 130–131 parameters, 112–113
dynamic filters, 180–181 QueryClose, 172
RefEdit control, 470–471
scrollbar, 473
E sheet
early binding chart-level, 119–120
New keyword, 430–431 workbook-level, 115–116
referencing a Word object, 427–430 spin button, 169
email, validating an address, 293–295 tab strip, 469–470
embedded charts, events, 118–120, 136–137 text box, 161–162
empty cells, 62–63, 226 toggle button, 471–472
enabling, events, 113 userform, 157–158
End+down arrow shortcut, 47–48 workbook, 113–115
EndKey method, 437 worksheet, 116–117
error handling, 501–503 Excel. See also ribbon
client training and, 509–510 add-ins
custom ribbon and, 529–533 closing, 553
Debug mode, 510 converting workbooks to, 550–551
encountering errors on purpose, 509 creating with VB Editor, 551–552
On Error GoTo syntax, 505–506 hidden workbook as an alternative, 554–555

584

9780137521524_print.indb 584 01/02/22 8:49 PM


formatting

installing, 552 checking for the existence of via ADOs, 461


removing, 554 form, controlling in Word, 443–444
saving files as, 551 protected password box, 275–277
security, 553 file
Compatibility mode, 576 operations, 257
Developer tab exporting data to an XML file, 264–265
Add-ins group, 4 importing and deleting a CSV file, 260
Code group, 4 listing files in a directory, 257–260
Controls group, 4 reading a text file into memory and parsing,
Disable All Macros with Notification setting, 8 260–261
displaying, 3–4 types
macro settings, 7–8 .xlsm, 5
Modify group, 4 macro-supported, 4–5
XML group, 4 File Open dialog box, 173–174
error handling, 501–503 Filter In Place, 201
Help button, 576 catching no records, 201–202
Point mode, 185 showing all records, 202
purchasing, 571 filtering, 229
Quick Analysis tool, 573 filters
Ready mode, 407 chart, 322
RELS file, 525–526 conceptual, 230–231
single-document interface, 572–573 manual, 229–230
updates, scheduling, 407 OLAP pivot table, 271–273
versions, 571 search, 234–236
error handling and, 515–516 ShowDetail property and, 250
pivot table evolution, 211–212 slicers, 237–241
.xlsm files, 525 Timelines, 241–243
Excel8CompatibilityMode property, 577–578 types, 232–234
Execute method, 458 FirstNonZeroLength() function, 296–297
Exists method, 146 fixed-width reports, converting to a data set, 280–283
Exit For statement, 73–74 flow control. See also loops
exporting conditions, 81
charts as a graphic, 337 If-Else If-End If construct, 82–83
to an XML file, 264–265 If-Then-Else construct, 81
expressions If-Then-Else-End If construct, 82
Case statement and, 84 If-Then-End If construct, 82
text, 83 Select Case construct, 83–84
watches and, 42–43 For…Each loops, 79
extracting a unique list of values For…Next loops, 69–71
with the user interface, 185–186 Step clause, 72–73
with VBA code, 186–189 using variables in the For statement, 72
Format method, 327–328
Format tab, 327–328
F Shape Fill drop down, 328–330
fields FormatConditions collection, 340–341
active, coloring, 485–486 formatting. See also conditional formatting
adding on the fly, 462–463 cells, 243–244
adding to the pivot table data area, 214–217 based on their value, 358
calculated, 249–250 that contain blanks or errors, 359

585

9780137521524_print.indb 585 01/02/22 8:49 PM


formatting

that contain dates, 359 referring to entire columns or rows, 93


that contain text, 358–359 relative references, 91–92
using a formula, 359–361 remembering column numbers associated with
charts, 327–328 column letters, 95–96
conditional, 340–341 replacing A1 formulas with, 94–95
line settings, 331 toggling to, 88
resetting on tables, 278–279 frames, 474, 475
sparklines, 369 Frankston, B., 87
elements, 374–377 functions
using RGB colors, 373–374 API
using theme colors, 369–372 checking whether an Excel file is open on a net-
work, 495–496
win/loss charts, 377–378
creating a running timer, 498–499
forms, 160
customizing the About dialog box, 497
combining, 169–171
disabling the X for closing a user form, 498
controls. See also controls
playing sounds, 499
assigning a macro to a, 12–13
retrieving display-resolution information, 496–497
bug fix when adding to a form, 159
retrieving the computer name, 495
combo boxes, 162–163
building in Power Query, 390–393
command buttons, 160
Count, 221
graphic, 167
COUNTIF, 360
labels, 159
CreateObject, 431
list boxes, 162–163, 164–165
GetObject, 431–432
MultiPage, 169–171
InputBox, 153–154
option buttons, 166
IsEmpty, 62–63
programming, 158–159
JavaScript, 560–561
renaming, 159
LAMBDA, 307, 573
spin buttons, 168–169
building, 307–308
text boxes, 160
sharing, 308
fields, controlling in Word, 443–444
SLUGIFY.PLUS(), 309–310
getting a file name, 173–174
TOC2HTML(), 310–311
illegal window closing, 172
MsgBox, 154–155
retrieving information from, 160–161
NOW, 292
transparent, setting up, 487–488
recursive, 290
verifying field entry, 171
RGB, 373–374
Formula property, 352–354
SUM, 25
formula-based conditions, 194–195
user-defined, 285, 304–305
returning above-average records, 200
BookOpen(), 288–289
using in the Excel user interface, 195–196
building, 286–287
using with VBA, 196–200
ColName(), 305–306
formulas
ContainsText(), 303–304
A1 addressing, 87, 89–91
ConvertWeekDay(), 299–300
array, 573
creating, 285
conditional formatting and, 359–361
DateTime(), 292–293
controls, combo boxes, 164–165
FirstNonZeroLength(), 296–297
naming, 101
GetAddress(), 305
R1C1 addressing, 49, 87, 90, 91
IsEmailValid(), 293–295
absolute references, 92–93
LastSaved(), 292
mixed references, 93

586

9780137521524_print.indb 586 01/02/22 8:49 PM


JavaScript

MSubstitute(), 297–298
NumUniqueValues(), 296
I
icon(s), 340, 575
RetrieveNumbers(), 298–299
filtering by, 179–180
sharing, 288
plus sign, 322–323
SheetExists(), 289–291
Restore Down, 39
SortConcat(), 300–301
sets, adding to a range, 347–348, 351–352
SumColor(), 295
constants, 348–349
WinUserName(), 291–292
specifying an icon set, 348–349
VLOOKUP, 107
specifying ranges for each icon, 350
XLOOKUP, named ranges and, 107–109
Stop Recording, 10
using on the ribbon, 527–529
G If statements, nesting, 84–86
GetAddress() function, 305 If-Else If-End If construct, 82–83
GetObject function, 431–432 If-Then-Else construct, 81
global If-Then-Else-End If construct, 82
names, 97–98 If-Then-End If construct, 82
variables, 402 images, adding to userforms on the fly, 480–481
Go To Special dialog box, 64, 201 Immediate window (VB Editor), 35, 41–42
loops and, 182–183 importing
Visible Cells Only option, 181–182 CSV files, 260
Gonzàlez Ruiz, J. P., 268 text files, 413
gradients, applying to objects, 330 input boxes, 153–154
graphic controls, 167 InsertLines method, 280
grouping, dates, 221–223 installing, add-ins, 543–544
Intersect method, 62
IsEmailValid() function, 293–295
H IsEmpty function, 62–63
Help topics, 32–34
code and, 37
constants, 35
J
optional parameters, 34–35 JavaScript
properties and, 38 adding interactivity to Office add-ins, 559–560
ToolTips and, 42 arrays, 562–563
Hide method, 156–157 comments, 561
hiding custom functions, 549
names, 106 For each...next statement, 565–566
userforms, 156–157 functions, 560–561
HomeKey method, 437 if statements, 564
HTML, 557 for loops, 563
comments, 557 math functions, 567–568
tags, 557 Office add-ins and, 569–570
hyperlinks operators, 566–567
returning the address, 305 Select Case construct, 564–565
running a macro from, 537 strings, 562
userforms and, 476–477 variables, 561

587

9780137521524_print.indb 587 01/02/22 8:49 PM


Jet engine

Jet engine, 448 exiting early, 73–74


joining, ranges, 62 For…Each, 79
Jones, K., 263 For…Next, 69–71
Excel Tables: A Complete Guide for Creating, Using, Step clause, 72–73
and Automating Lists and Tables, 67 using variables in the For statement, 72
Go To Special dialog box and, 182–183

K JavaScript, 563
M, 402
Kaji, M., 260
If-then logic, 403–404
Kapor, M., 87
List.Generate, 404–406
keyboard shortcuts. See shortcut(s)
nesting, 74–75
keywords
replacing with AutoFilter, 175–178
New, 430–431
While...Wend, 79
Preserve, 131
Lotus 1–3, 24, 87
Klann, D., 275

M
L M, 385
labels, 159, 161–162
global variables, 402
LAMBDA functions, 307, 573
loops, 402
building, 307–308
If-then logic, 403–404
sharing, 308
List.Generate, 404–406
SLUGIFY.PLUS(), 309–310
searching Spotify for an artist, 388–389
TOC2HTML(), 310–311
macro button, creating
Lanzo, L., 264
on the Quick Access Toolbar, 11–12
late binding, 145, 430
on the ribbon, 10–11
layout
macro recorder
pivot table, 250–251
absolute references, 19–20
report, 251
charts and, 318
learning VBA, 3
relative references, 20–24
barriers
shortcomings of, 15–16
macro recorder, 2
tips for using, 25–26
syntax, 2
macros. See also code
line settings, trendline, 331
assigning
LineFormat object, 331
to a form control, 12–13
list boxes, 162–163
to an object, 13
events, 164–165
attaching
multicolumn, 486–487
to an ActiveX control, 536
MultiSelect property, 163–164
to a shape, 535
Load method, 156
canceling, 408
loading large files to the Data Model, 423–424
copying to new workbooks, 279–280
local names, 97–98, 104
passwords and, 515
logical AND, 193
recording, 8–9
logical OR, 192
running, 10
loops, 69
from a hyperlink, 537
Do, 75–77
using a keyboard shortcut, 533–534
stopping, 77
scheduling, 407, 408–411
While and Until clauses, 77–79
searching Spotify database for an artist, 400

588

9780137521524_print.indb 588 01/02/22 8:49 PM


NumberFormat property

security, 6 SetElement, 322–327


adding a trusted location, 6–7 .SetSourceData, 313
Disable All Macros with Notification setting, 8 SparklineGroups.Add, 363
enabling outside trusted locations, 7–8 SpecialCells, 64–66, 201
supported file types, 4–5 selecting with, 277–278
testing, 19, 22–23 TwoColorGradient, 330
manual filter, 229–230 TypeText, 437–438
map charts, creating, 335 Union, 62
matrix, 126 UnselectAll, 474
MDB (multidimensional database) files, 447–448 UserPicture, 329
message boxes, 154–155 Miles, T., 261, 262
methods, 28 mixed references, 93
AddAboveAverage, 355 Moala, I., 277
.AddChart2, 313, 318–319 modeless userforms, 475–476
backward compatibility, 337–338 Modify method, 343
creating charts with, 314–315 modules, 15
AddTop10, 355 MSubstitute() function, 297–298
AddUniqueValues, 356–358 multicolumn list boxes, 486–487
AutoFilter, 176–177 multidimensional arrays, declaring, 126–127
AutoSort, 226 MultiPage control, 169–171, 468
Close, 436–437
Copy, 49
CopyFromRecordSet, 453, 455 N
CreatePivotTable, 213–214 Name Manager dialog box, 97
Delete, 100 named ranges, 55, 107–109
EndKey, 437 names, 97
Execute, 458 adding, 98–100
Exists, 146 checking for the existence of, 106
Format, 327–328 comments, 100
Hide, 156–157 deleting, 100
HomeKey, 437 formula, 101
InsertLines, 280 global, 97–98
Intersect, 62 hiding, 106
Load, 156 local, 97–98, 104
Modify, 343 numbers and, 103
OneColorGradient, 330 reserved, 104–105
OnTime, 406–407 string, 101–103
Open, 436 table, 103–104
parameters, 29–31 types of, 101
.Patterned, 330 using arrays in, 104
PresetGradient, 330 nesting
PresetTextured, 329 If statements, 84–86
PrintOut, 437 loops, 74–75
SaveAs2, 436 New keyword, referencing a Word application, 430–431
Select, 38 noncontiguous ranges, 66
SelectAll, 474 NOW function, 292
NumberFormat property, 361–362

589

9780137521524_print.indb 589 01/02/22 8:49 PM


numbers

numbers OneColorGradient method, 330


naming, 103 OnTime method, 406–407
retrieving from mixed text, 298–299 Open method, 436
sorting, 301–303 opening
NumFilesInCurDir() function, 290–291 files in a text editor, 550
NumUniqueValues() function, 296 text files
delimited files, 417–419
fixed-width files, 413–417
O operators
Object Browser, 45, 433–434 JavaScript, 566–567
object-oriented programming, 28 xlFilterIcon, 179–181
object(s), 28 option buttons
assigning a macro to an, 13 adding to a userform, 165–166
properties, 31 events, 166
objects, 49. See also class modules; DAOs (data access Ozgur, S. M., 549
objects)
Chart, 318
ChartFormat, 327 P
color, applying, 329 PageFields parameter, 221
creating new instances of, 431 parameters, 29–31
custom event, 112–113
creating, 137–139 PageFields, 221
using, 139–140 parsing, text files, 260–261
gradients, applying, 330 passing an array, 131–132
LineFormat, 331 password box, 275–277
patterns, applying, 330 passwords
properties, 38, 137. See also properties cracking, 514–515
Range, 54 macros and, 515
texture, applying, 329 .Patterned method, 330
variables, 79–81 patterns, applying to objects, 330
watches, 44 picture, filling objects with a, 329
Word, 435 Pieterse, J. K., 491
Document, 435–437 pivot table(s), 211, 574. See also Data Model
Range, 438–441 adding model fields to, 246
referencing via early binding, 427–430 advanced features, 220
referencing via late binding, 430 AutoSort option, 226
Selection, 437–438 building, 245–246
ObjectThemeColor property, 329 calculated fields, 249–250
Office 365, 314 calculated items, 250
Office add-ins, 549, 550 changing the calculation to show percentages, 223–225
adding interactivity, 554–557, 559–560 counting the number of records, 221
defining, 558–559 creating in VBA, 212
Hello World, 550, 551–554 adding fields to the data area, 214–217
JavaScript and, 569–570 configuration, 213–214
writing to the content or task pane, 569 defining the pivot cache, 213
OFFSET property, 177 data visualizations, 252–253
Offset property, referencing ranges with, 58–60 defining, 220–221
OLAP pivot table, filtering by a list of items, 271–273 determining the size of, 217–220
Oliver, N., 257 drilling-down, 270–271

590

9780137521524_print.indb 590 01/02/22 8:49 PM


R1C1 addressing

evolution over various Excel versions, 211–212 programming languages. See also M
filtering data sets, 229 BASIC, 2, 28
filters M, 385
conceptual, 230–231 object-oriented, 28
manual, 229–230 procedural, 28
search, 234–236 Programming window, VB Editor, 17–19
ShowDetail property and, 250 progress indicator, cell, 274–275
slicers, 237–241 Project Explorer, 14–15
types, 232–234 properties, 31, 137
formatting the intersection of values in, 243–244 .Calculation, 223
grouping daily dates to months, quarters, or years, Cells, referencing ranges with, 57–58
221–223 ChartColor, 320–321
layout, 250–251 Columns, 61
OLAP, filtering by a list of items, 271–273 CurrentRegion, selecting ranges with, 63
producing a static summary, 218–220 custom, creating, 148–151
reports, 217 Excel8CompatibilityMode, 577–578
layout, 251 Formula, 352–354
multiple value fields, 220–221 Help topics, 38
replicating for every product, 226–229 MultiSelect, 163–164
suppressing subtotals for multiple row fields, 252 NumberFormat, 361–362
TypeScript and, 255–256 ObjectThemeColor, 329
Values area OFFSET, 177
adding numeric fields, 246–247 Offset, referencing ranges with, 58–60
eliminating blank cells, 226 Priority, 341
Power Pivot, 244. See also pivot table(s) Range(), 47
Power Query. See also M Resize, 60–61
Advanced Editor, 387–388 Rows, 61
building a custom function, 390–393 ShowDetail, 250
loading large text files to the Data Model, 423–424 TableRange2, 217–218
M and, 385 Type, 341
queries Version, 577
AlbumTracks, 395, 397 Properties window, VB Editor, 15
ArtistsAlbums, 394–395, 396–397 protected password box, 275–277
duplicating to make a new query, 393–395
error handling, 403
fnGetToken, 392 Q
grouping, 397–398 QueryClose event, 172
placing results on your dashboard, 398–401 querying, in Break mode
SearchArtist, 391, 393, 396 by hovering, 42
refreshing Spotify credentials after they expire, 390 using a Watches window, 42–43
searching Spotify database for an artist, 388–390 using the Immediate window, 41–42
storing global variables in a Settings record, 402–403 Quick Access Toolbar, creating a macro button, 11–12
Preserve keyword, 131 Quick Analysis tool, 24–25, 573
PresetGradient method, 330
PresetTextured method, 329
PrintOut method, 437 R
Priority property, 341 R1C1 addressing, 49, 87, 90, 91
procedural programming, 28 absolute references, 92–93
VBA and, 29 mixed references, 93

591

9780137521524_print.indb 591 01/02/22 8:49 PM


R1C1 addressing

referring to entire columns or rows, 93 RefEdit control, 470–471


relative references, 91–92 references
remembering column numbers associated with absolute, 19–20, 92–93
column letters, 95–96 array, 104
replacing A1 formulas with, 94–95 mixed, 93
toggling to, 88 relative, 20–24, 91–92
Range object, 54, 438–441 relationships, creating between tables, 245
Range() property, 47 relative references, 20–24, 91–92
ranges, 53 RELS file, 525–526
color scales, adding, 346–347 removing, add-ins, 545
creating from overlapping ranges, 62 renaming, controls, 159
data bars reports
adding, 342–346 fixed-width, converting to a data set, 280–283
using two colors, 352–354 pivot table, 217
finding the first nonzero-length cell in, 296–297 layout, 251
icon sets, 347–348 multiple value fields, 220–221
specifying, 348–349 replicating for every product, 226–229
specifying for subset of a range, 351–352 suppressing subtotals for multiple row fields, 252
VBA constants, 348–349 reserved names, 104–105
joining, 62 resetting, table format, 278–279
named, 55, 107–109 Resize property, 60–61
noncontiguous, 66 resizing
referencing ranges, 60–61
in other worksheets, 55–56 userforms, 479
relative to another range, 56–57 Restore Down icon, 39
shortcuts, 55 RetrieveNumbers() function, 298–299
using Cells property, 57–58 retrieving data
using Columns and Rows properties, 61 from arrays, 128–129
using the CurrentRegion property, 63 date and time of last save, 292
using the Offset property, 58–60 file names, 173–174
resizing, 60–61 from forms, 160–161
specifying for icons, 350 from mixed text, 298–299
syntax, 54 permanent date and time, 292–293
tables, 67 user ID, 291–292
worksheets and, 54 returning
reading addresses of duplicate maximum values, 304–305
from an Access database, 451 hyperlink address, 305
text files with more than 1,048, 576 rows, 420–423 ReturnMax() function, 304–305
Record Macro dialog box, 9–10 RGB color, applying to sparklines, 373–374
recording a macro, 8–9, 17 ribbon, 517, 572
absolute references, 19–20 adding a control, 520
Autosum and, 24–25 adding custom icons, 528–529
preparations, 16–17 buttons, using images on, 527
relative references, 20–24 control attributes, 521–525
tips, 25–26 error handling, 529–533
records, counting, 221 macro button, creating, 10–11
recursive functions, 290 using Microsoft Office icons, 527–528

592

9780137521524_print.indb 592 01/02/22 8:49 PM


Sullivan

Rows property, 61 ShowDetail property, 250


rules size of pivot tables, determining, 217–220
above/below average, 340 slicers, 237–243, 574
top/bottom, 340 SLUGIFY.PLUS() function, 309–310
running a macro, 10 SmartArt, 575
with a command button, 534–535 Smith, C., 267
using a keyboard shortcut, 533–534 sorting, 300–301
custom, 273–274
numeric and alpha characters, 301–303
S SparklineGroups.Add method, 363–365
SaveAs2 method, 436 sparklines, 363
scaling, sparklines, 366–369 creating, 363–365
scheduling macros, 407, 408–411 dashboards
ScrollBar control, 472–473 creating, 378
Search box, AutoFilter and, 179 creating individual sparklines in, 379–383
search filter, 234–236 formatting, 369
security, 6 elements, 374–377
add-in, 544–545 using RGB colors, 373–374
Disable All Macros with Notification setting, 8 using theme colors, 369–372
trusted locations scaling, 366–369
adding, 6–7 win/loss charts, formatting, 377–378
enabling macros outside trusted locations, 7–8 SpecialCells method, 64–66, 201, 277–278
Select Case construct, 83–84 spin buttons, 168–169
in JavaScript, 564–565 Spotify. See also Power Query
using on a worksheets, 306 creating a developer account, 386–387
Select method, 38 querying the list of songs on an album, 395
SelectAll method, 474 Search API, 388–390
selecting, with SpecialCells, 277–278 searching the database for an artist, 388–389
Selection object, 437–438 SQL Server, 463–464. See also Access
SetElement method, 322–327 standard modules, 141–142. See also class modules
.SetSourceData method, 313 statements
Shape Fill drop down, 329–330 Case, complex expressions in, 84
shared Access database, creating, 449–450 DIM, 79–80
sharing Exit For, 73–74
LAMBDA functions, 308 For, 72
UDFs (user-defined functions), 288 If, nesting, 84–86
sheet events Select Case, 83–84
chart, 119–120 Step Into feature, 38–40
workbook-level, 115–116 Stop Recording icon, 10
SheetExists() function, 289–290 streamlining code, 46
shortcut(s) strings
accelerator keys, 483–484 JavaScript, 562
Ctrl+T, 244 naming, 101–103
End+down arrow, 47–48 searching for within text, 303–304
for referencing ranges, 55 styles, chart, 315–318
running a macro with, 533–534 substituting, multiple characters, 297–298
Show Values As tab of the Value Field Settings dialog box, subtotals, suppressing for multiple row fields, 252
223
Sullivan, J., 271

593

9780137521524_print.indb 593 01/02/22 8:49 PM


SUM function

SUM function, 25 transparent forms, setting up, 487–488


SumColor() function, 295 trapping
application events, 134–135
embedded chart events, 136–137
T trendline, formatting, 331
tab stops, setting on userforms, 484–485 trusted locations, 6–7
tab strips, 468–470 Tufte, E., 363
tables. See also Access TwoColorGradient method, 330
Access Type property, 341
adding fields on the fly, 462–463 TypeScript, 576
adding on the fly, 461–462 comparing to VBA, 253–255
checking for the existence of, 460 creating a pivot table, 255–256
checking for the existence of a field, 461 TypeText method, 437–438
adding to the Data Model, 244–245
creating a relationship between, 245
naming, 103–104 U
referencing, 67 UDFs (user-defined functions), 285
resetting the format, 278–279 BookOpen(), 288–290
tblTransfer, 449 building, 286–287
testing, macros, 19, 22–23 ColName(), 305–306
text boxes, 160, 161–162 ContainsText(), 303–304
text files ConvertWeekDay(), 299–300
importing, 413 creating, 285
with more than 1,048, 576 rows, 419–420 DateTime(), 292–293
reading, 420–423 FirstNonZeroLength(), 296–297
using Power Query to load, 423–424 GetAddress(), 305
opening IsEmailValid(), 293–295
delimited files, 417–419 JavaScript, 549
fixed-width files, 413–417 LastSaved(), 292
reading into memory and parsing, 260–261 MSubstitute(), 297–298
writing, 424–425 NumFilesInCurDir(), 290–291
Text Import Wizard, 35–37 NumUniqueValues(), 296
texture, applying to objects, 329 RetrieveNumbers(), 298–299
theme colors, applying to sparklines, 369–372. See also ReturnMax(), 304–305
color(s) sharing, 288
time. See also dates SortConcat(), 300–301
24-hour, 117–118 SumColor(), 295
of last save, retrieving, 292 WinUserName(), 291–292
retrieving, 292–293 UDTs (user-defined types)
Timelines, 241–243 creating custom properties, 148–151
tip text, adding to userforms, 484 declaring, 149
TOC2HTML() function, 310–311 Union method, joining multiple ranges, 62
ToggleButton control, 471–472 UnselectAll method, 474
toolbars, UserForm, 465–466 Until clause, 77–79
ToolTips, 42 updates, scheduling, 407
top/bottom rules, 340, 355–356. See also conditional Urtis, T., 265, 270, 274
formatting
user ID, retrieving, 291–292
tracking, user changes, 267–268
UserForm toolbar, 465–466

594

9780137521524_print.indb 594 01/02/22 8:49 PM


VBA

userforms, 153. See also controls; forms placing a chart in a cell note, 265–267
accelerator keys, 483–484 reading a text file into memory and parsing, 260–261
adding option buttons, 165–166 resetting a table’s format, 278–279
calling, 156 selecting with SpecialCells, 277–278
check boxes, 466–468 separating worksheets into workbooks, 261–262
coloring the active field, 485–486 tracking user changes, 267–268
controls using a protected password box, 275–277
adding at runtime, 477–478 using VBA Extensibility to add code to new work-
adding on the fly, 479 books, 279–280
collections and, 473–475
ProgIds, 480
creating, 155–156
V
validating, email addresses, 293–295
disabling the X for closing, 498
values
events, 157–158
duplicate, minimizing, 340
graphics, 167
unique, counting, 296
hiding, 156–157
variables, 49
hyperlinks, 476–477
declaring, 14
illegal window closing, 172
global, 402
images, adding on the fly, 480–481
JavaScript, 561
input boxes, 153–154
object, 79–81
message boxes, 154–155
For statement, 72
modeless, 475–476
Variant, 128
picture catalog, 481–483
Variant variables, 128
programming, 157
VB Editor, 13–14. See also Object Browser
RefEdit control, 470–471
Break mode, 38–43. See also breakpoints
resizing on the fly, 479
converting a file to an add-in, 542–543
scrollbars, 472–473
Help and, 33–34
setting tab stops, 484–485
Immediate window, 35, 41–42
sizing on the fly, 479
Programming window, 17–19
tab strips, 468–470
Project Explorer, 14–15
tip text, adding, 484
Properties window, 15
transparent, 487–488
settings, 14
verifying field entry, 171
VBA, 31–32
UserPicture method, 329
barriers to learning
utilities
macro recorder, 2
combining workbooks, 262–263
syntax, 2
converting a fixed-width report to a data set,
280–283 charts
copying data to separate worksheets without using applying a color, 320–321
Filter, 263–264 borders, 331
creating a cell progress indicator, 274–275 combo, 331–334
creating a custom sort order, 273–274 filtering, 322
creating an Excel state class module, 268–270 formatting, 327–328
drilling-down a pivot table, 270–271 line settings, 331
exporting data to an XML file, 264–265 map, 335
filtering an OLAP pivot table by a list of items, referring to, 318–319
271–273 SetElement method, 322–327
importing and deleting a CSV file, 260 specifying a title, 319–320
listing files in a directory, 257–260 styles, 315–318

595

9780137521524_print.indb 595 01/02/22 8:49 PM


VBA

trendline, 331 reports, 251


waterfall, 336–337 search filter, 234–236
collections, 29 ShowDetail property, 250
comparing to TypeScript, 253–255 slicers, 237–241
constants, icon set, 348–349 Timelines, 241–243
data bars, 339, 352–354 procedural programming and, 29
adding to a range, 342–346 simplifying Power Query queries, 396
color, applying, 344–345 AlbumTracks, 397
using two colors in a range, 352–354 ArtistsAlbums, 396–397
data visualizations, 340–341 SearchArtist, 396
error handling, 501–503 syntax, 38
Extensibility, 279–280 verifying, field entry, 171
formula-based conditions, 196–200 Version property, 577
icon sets, 347–348 Visual Basic, BASIC and, 2
creating for a subset of a range, 351–352 VLOOKUP function, 107
specifying ranges for each icon, 350
learning, 3
methods, 28, 29–31 W
objects, 28, 31 watches, 42–43
Office 365 and, 314 on objects, 44
pivot tables setting a break point, 43
adding fields to the data area, 214–217 waterfall charts, creating, 336–337
adding model fields to, 246 Wei, J., 273
adding numeric fields to the Values area, 246–247 While clause, 77–79
advanced features, 220 While.Wend loops, 79
AutoSort option, 226 Windows API, 491. See also API (Application Program-
ming Interface)
calculated fields, 249–250
win/loss charts, formatting, 377–378
calculated items, 250
WinUserName() function, 291–292
changing the calculation to show percentages,
223–225 With…End With block, 50
conceptual filter, 230–231 Word
configuration, 213–214 bookmarks, 441–442
creating, 212 constants, retrieving the real value of, 433–434
creating a Data model, 247–249 using the Object Browser, 433–434
data visualizations, 252–253 using the Watches window, 433
defining, 220–221 controlling form fields in, 443–444
defining the pivot cache, 213, 245–246 documents
determining the size of, 217–220 closing, 436–437
eliminating blank cells in the Values area, 226 creating, 435–436
filter types, 232–234 opening, 436
filtering, 229 printing, 437
formatting the intersection of values in, 243–244 saving changes to, 436
grouping daily dates to months, quarters, or templates, 436
years, 221–223 objects, 435
layout, 250–251 Document, 435–437
manual filter, 229–230 Range, 438–441
replicating the report for every product, 226–229 Selection, 437–438

596

9780137521524_print.indb 596 01/02/22 8:49 PM


XML

referencing an existing instance of, 431–432 Select Case construct, 306


referencing objects via early binding, 427–430 separating into workbooks, 261–262
referencing objects via late binding, 430 writing, text files, 424–425
workbooks, 2
adding code to, 279–280
checking for existing sheets in, 289–290 X-Y-Z
code, 546–547 xlFilterCopy, 202
combining, 262–263 combining multiple techniques, 205–209
converting to an add-in, 540–541 copying a subset of columns and reordering, 203–205
events, 113–115 copying all columns, 202–203
open, checking for, 288–289 xlFilterIcon operator, 179–181
ribbon, 517–518 XLOOKUP function, named ranges, 107–109
sheet events, 115–116 .xlsm file type, 5, 525
themes, 369 XML
worksheets adding a control to a ribbon, 520
copying data to without using Filter, 263–264 creating a tab and a group, 519
events, 116–117 customui folder, 518–519
ranges and, 54, 55–56 defining an Office add-in, 558–559
exporting data to a file, 264–265

597

9780137521524_print.indb 597 01/02/22 8:49 PM

You might also like