Introduction

Welcome to Excel 2013 Power Programming with VBA. If your job involves developing Excel workbooks that others will use — or if you simply want to get the most out of Excel — you've picked up the right book.

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 will show 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.

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 2013 is available for other platforms. For example, you can use Microsoft's Excel Web App in your browser, and even run Excel on ARM-based Windows RT devices. These versions do not support VBA. In other words, this book is for the desktop version of Excel 2013 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 my Excel 2013 Bible, 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. I assume that you know 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

If you don't know how to perform the preceding tasks, you could find some of this material over your head, so consider yourself warned. If you're an experienced spreadsheet user who hasn't used Excel 2013, Chapter 1 presents a brief overview of what this product offers.

What You Need to Have

To make the best use of this book, you need a copy of Excel 2013. Although most of the material also applies to Excel 2003 and later versions, I assume that you're using Excel 2013. Excel 2007 and later versions are radically different from their predecessors, but the VBA environment hasn't changed. If you plan to develop applications that will be used in earlier versions of Excel, I strongly suggest that you don't use Excel 2013 for your development work. Rather, use the earliest version of Excel that your target audience will be using.

This book isn't intended for any version of Excel for Mac. 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.

I recommend using a high-resolution monitor because you'll often be working with two windows. For optimal results, try a dual-monitor system and place Excel on one screen and Visual Basic Editor on the other. You'll soon become spoiled.

Conventions in This Book

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

Excel commands

Beginning with Excel 2007, the product features a menu-less user interface. In place of a menu system, Excel uses a context-sensitive Ribbon 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 I use 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 a new screen called Backstage. The Backstage window has commands along the left side of the window. To indicate Backstage commands, I 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

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

More lengthy input appears on a separate line in a monospace font. For example, I 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, I 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, I 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, I often use mixed uppercase and lowercase letters to make these names easier to read.

Mouse conventions

I assume that you're well versed in mouse usage. The mouse terminology I use is all standard fare: pointing, clicking, right-clicking, dragging, and so on.

What the Icons Mean

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

newfeature.eps

I use this icon to indicate that the material discussed is new to Excel 2013.

note.eps

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.

tip.eps

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

on_the_web.eps

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.

caution.eps

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

cross_ref.eps

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 eight main parts.

Part I: Some Essential Background

In Part I, I set the stage for the rest of the book. Chapter 1 is a conceptual overview of Excel 2013. In Chapter 2, I cover the essentials of formulas, including some clever techniques that might be new to you. Chapter 3 covers the ins and outs of the various files used and generated by Excel. Chapter 4 introduces the concept of application development using Excel.

Part II: Understanding Visual Basic for Applications

Chapters 5 through 9 make up Part II, and these chapters include everything that you need to know to start learning VBA. In this part, I introduce you to VBA, provide programming fundamentals, and detail how to develop VBA subroutines and functions. Chapter 9 contains many useful VBA examples.

Part III: Working with UserForms

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

Part IV: Advanced Programming Techniques

Part IV covers additional techniques that are often considered advanced. The first three chapters discuss how to develop utilities and how to use VBA to work with pivot tables and charts (including Sparkline graphics). Chapter 17 covers event handling, which enables you to execute procedures automatically when certain events occur. Chapter 18 discusses various techniques that you can use to interact with other applications (such as Word). Chapter 19 concludes Part IV with a hands-on discussion of creating add-ins.

Part V: Developing Applications

The chapters in Part V deal with important elements of creating user-oriented applications. Chapter 20 discusses how to modify the new Ribbon interface. Chapter 21 describes how to modify Excel's shortcut menus. Chapter 22 demonstrates several ways to provide online help for your applications. In Chapter 23, I present some basic information about developing user-oriented applications, and I describe such an application in detail.

Part VI: Other Topics

The six chapters in Part VI cover additional topics. Chapter 24 presents information regarding compatibility. In Chapter 25, I discuss various ways to use VBA to work with files. In Chapter 26, I explain how to use VBA to manipulate Visual Basic components such as UserForms and modules. Chapter 27 covers the topic of class modules. Chapter 28 explains how to work with color in Excel. I finish the part with a useful chapter that answers many common questions about Excel programming.

Part VII: Appendixes

Three appendixes round out the book. Appendix A is a reference guide to all VBA keywords (statements and functions). I explain VBA error codes in Appendix B, and Appendix C describes the files available on the companion website.

About This Book's Website

You can (and should) download many useful examples that I discuss in the text. When I write about computer-related material, I emphasize learning by example. I learn more from a well-thought-out example than from reading a dozen pages in a book, and I assume that this is true for many other people. Consequently, I spent more time developing the examples than I did writing chapters.

The files are at www.wiley.com/go/Excel2013PowerProgramming.

cross_ref.eps

Refer to Appendix C for a description of each file.

About the Power Utility Pak Offer

Toward the back of the book, you'll find a coupon that you can redeem for a discounted copy of my popular Power Utility Pak add-in software. PUP is an award-winning collection of useful Excel utilities and many new worksheet functions. I developed this package exclusively with VBA.

I think you'll find this product useful in your day-to-day work with Excel. You can also purchase the complete VBA source code for a nominal fee. Studying the code is an excellent way to pick up some useful programming techniques.

You can take Power Utility Pak for a test drive by installing the 30-day trial version available at my website: http://spreadsheetpage.com.

How to Use This Book

You can use this book any way that you please. If you choose to read it from cover to cover, be my guest. But because I'm dealing with intermediate-to-advanced subject matter, the chapter order is often immaterial. I suspect that most readers will skip around, picking up useful tidbits here and there. If you're faced with a challenging task, you might try the index first to see whether the book specifically addresses your problem.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset