Introduction - Excel 2016 Power Programming with VBA (2016)

Excel 2016 Power Programming with VBA (2016)

Introduction

For most of us, the path to Excel VBA programming starts with the need to perform some task that can’t be done with the standard tools in Excel. That task is different for each of us. Maybe that task is to automatically create separate workbooks for all the rows in a dataset. Maybe that task is to automate the emailing of dozens of reports. Whatever that task is for you, you can bet that someone has started their own journey into Excel VBA with the same need.

The beautiful thing about Excel VBA is that you don’t have to be an expert to start solving problems with it. You can learn just enough to solve a particular problem, or you can go further and discover ways to handle all kinds of automation scenarios.

Whatever your goals may be, Excel 2016 Power Programming with VBA will help you harness the power of the VBA language to automate tasks, work smarter, and be more productive.

Topics Covered

This book focuses on Visual Basic for Applications (VBA), the programming language built into Excel (and other applications that make up Microsoft Office). More specifically, it shows you how to write programs that automate various tasks in Excel. This book covers everything from recording simple macros through creating sophisticated user-oriented applications and utilities.

You can approach this book in any way that you please. You can read it from cover to cover, or you can skip around, picking up useful tidbits here and there. VBA programming is often a task-oriented endeavor. So if you’re faced with a challenging task, you might try the index first to see where the book might specifically address your problem.

This book does not cover Microsoft Visual Studio Tools for Office (VSTO), a technology that uses Visual Basic .NET and Microsoft Visual C#. VSTO can also be used to control Excel and other Microsoft Office applications.

As you may know, Excel 2016 is available for other platforms. For example, you can use Microsoft’s Excel Web App in your browser and even iPads and tablets. These versions do not support VBA. In other words, this book is for the desktop version of Excel 2016 for Windows.

What You Need to Know

This is not a book for beginning Excel users. If you have no experience with Excel, a better choice might be Excel 2016 Bible (John Walkenbach, Wiley Publishing), which provides comprehensive coverage of all the features of Excel and is meant for users of all levels.

To get the most out of this book, you should be a relatively experienced Excel user who knows how to:

· Create workbooks, insert sheets, save files, and so on

· Navigate through a workbook

· Use the Excel Ribbon user interface

· Enter formulas

· Use Excel’s worksheet functions

· Name cells and ranges

· Use basic Windows features, such as file management techniques and the Clipboard

What You Need to Have

It is important to have a full installation of Excel and, if you want to try the more advanced chapters involving communication between Excel and other Office applications, you will need a full installation of Office.

Although most of the material also applies to earlier versions of Excel, we assume that you are using Excel 2016. If you plan to develop applications that will be used in earlier versions of Excel, we strongly suggest that you use the earliest version of Excel that your target audience will be using.

The version of Windows you use is not important. Any computer system that can run Windows will suffice, but you’ll be much better off with a fast machine with plenty of memory. Excel is a large program, and using it on a slower system or a system with minimal memory can be extremely frustrating.

This book is not intended for any version of Excel for Mac.

Conventions in This Book

Take a minute to skim this section and learn some of the typographic conventions used throughout this book.

Excel commands

Excel uses a context-sensitive Ribbon menu system. The words along the top (such as Insert and View) are known as tabs. Click a tab, and the Ribbon of icons displays the commands that are most suited to the task at hand. Each icon has a name that is (usually) displayed next to or below the icon. The icons are arranged in groups, and the group name appears below the icons.

The convention used in this book is to indicate the tab name, followed by the group name, followed by the icon name. So, for example, the command used to toggle word wrap in a cell is indicated as:

· Home ➜ Alignment ➜ Wrap Text

Clicking the first tab, labeled File, takes you to the Backstage window. The Backstage window has commands along the left side of the window. To indicate Backstage commands, we use the word File, followed by the command. For example, the following command displays the Excel Options dialog box:

· File ➜ Options

Visual Basic Editor commands

Visual Basic Editor is the window in which you work with your VBA code. VB Editor uses the traditional menu-and-toolbar interface. A command like the following means to click the Tools menu and select the References menu item:

· Tools ➜ References

Keyboard conventions

You need to use the keyboard to enter data. In addition, you can work with menus and dialog boxes directly from the keyboard — a method that you might find easier if your hands are already positioned over the keys.

Input

Inputs that you are supposed to type from the keyboard will appear in boldface — for example, enter =SUM(B2: B50) in cell B51.

Lengthier inputs will appear on a separate line in a monospace font. For example, we might instruct you to enter the following formula:

=VLOOKUP(StockNumber,PriceList,2)

VBA code

This book contains many snippets of VBA code, as well as complete procedure listings. Each listing appears in a monospace font; each line of code occupies a separate line. (I copied these listings directly from the VBA module and pasted them into my word processor.) To make the code easier to read, we often use one or more tabs to create indentations. Indentation is optional, but it does help to delineate statements that go together.

If a line of code doesn’t fit on a single line in this book, we use the standard VBA line continuation sequence: At the end of a line, a space followed by an underscore character indicates that the line of code extends to the next line. For example, the following two lines are a single code statement:

columnCount = Application.WorksheetFunction. _

CountA(Range(“A:A”)) + 1

You can enter this code either on two lines, exactly as shown, or on a single line without the space and underscore character.

Functions, filenames, and named ranges

Excel’s worksheet functions appear in uppercase font, like so: “Enter a SUM formula in cell C20.” For VBA procedure names, properties, methods, and objects, we often use mixed uppercase and lowercase letters to make these names easier to read.

What the Icons Mean

Throughout the book, I use icons to call your attention to points that are particularly important:

inline Note

I use Note icons to tell you that something is important — perhaps a concept that could help you master the task at hand or something fundamental for understanding subsequent material.

inline Tip

Tip icons indicate a more efficient way of doing something or a technique that might not be obvious.

inline On the Web

These icons indicate that an example file is available on the book’s website. See the section “About This Book’s Website,” later in this Introduction.

inline Caution

I use Caution icons when the operation that I’m describing can cause problems if you’re not careful.

inline Cross-Ref

I use the Cross Reference icon to refer you to other chapters that have more to say on a subject.

How This Book Is Organized

The chapters of this book are grouped into five main parts.

Part I: Introduction to Excel VBA

In Part I we introduce you to VBA, providing the programming fundamentals you will need to create and manage Excel subroutines and functions. Chapter 1 sets the stage with a conceptual overview of Excel application development. Chapters 2 through 6 cover everything you need to know to start coding in VBA. Chapter 7 rounds out your introduction to VBA with many useful examples.

Part II: Advanced VBA Techniques

Part II covers additional techniques that are often considered advanced. Chapters 8 and 9 discuss how to use VBA to work with pivot tables and charts (including Sparkline graphics). Chapter 10 discusses various techniques that you can use to interact with other applications (such as Word and Outlook). Chapter 11 concludes Part II with a discussion on how to work with files and external data sources.

Part III: Working with UserForms

The four chapters in Part III cover custom dialog boxes (UserForms). Chapter 12 presents some built-in alternatives to creating custom UserForms. Chapter 13 provides an introduction to UserForms and the various controls that you can use. Chapters 14 and 15 present many examples of custom dialog boxes, ranging from basic to advanced.

Part IV: Developing Excel Applications

The chapters in Part IV deal with important elements of creating user-oriented applications. Chapter 16 offers a hands-on discussion of creating add-ins. Chapters 17 and 18 discuss how to modify Excel’s Ribbon and shortcut menus. Chapter 19 demonstrates several ways to provide online help for your applications. In Chapter 20, we present a primer on developing user-oriented applications. Chapter 21 rounds out your exploration of Excel VBA programming with some information regarding compatibility.

Part V: Appendix

Part V includes an appendix that offers a reference guide to all statements and functions exposed to VBA as keywords.