Introduction - Excel VBA Programming For Dummies, 4th Edition (2015)

Excel VBA Programming For Dummies, 4th Edition (2015)

Introduction

Greetings, prospective Excel programmer… .

Thanks for buying my book. I think you’ll find that it offers a fast, enjoyable way to discover the ins and outs of Microsoft Excel programming. Even if you don’t have the foggiest idea of what programming is all about, this book can help you make Excel jump through hoops in no time. (Well, it will take some time.)

Unlike most programming books, this one is written in plain English, and even normal people can understand it. Even better, it’s filled with information of the “just the facts, ma’am” variety — not the drivel you might need once every third lifetime.

About this Book

Go to any large bookstore (in-person or online), and you’ll find many Excel books (far too many, as far as I’m concerned). A quick overview can help you decide whether this book is really right for you. This book

· Is designed for intermediate to advanced Excel users who want to get up to speed with Visual Basic for Applications (VBA) programming.

· Requires no previous programming experience.

· Covers the most commonly used commands.

· Is appropriate for Excel 2013 or Excel 2016.

· Just might make you crack a smile occasionally.

If you’re using Excel 2000, XP, or 2003, this book is not for you. If you’re using Excel 2007 or 2010, it might be okay, but some things have changed. You'd probably be better off with the previous edition.

Oh, yeah — this is not an introductory Excel book. If you’re looking for a general-purpose Excel book, check out either of the following books, which are all published by Wiley:

· Excel 2016 For Dummies, by Greg Harvey

· Excel 2016 Bible, by John Walkenbach (yep, that’s me)

These books are also available in editions for earlier versions of Excel.

Notice that the title of this book isn’t The Complete Guide to Excel VBA Programming For Dummies. I don’t cover all aspects of Excel programming — but then again, you probably don’t want to know everything about this topic. If you consume this book and find that you’re hungry for a more comprehensive Excel programming book, you might try Microsoft Excel 2016 Power Programming with VBA, by John Walkenbach, also published by Wiley. And yes, editions for older versions of Excel are also available.

Obligatory Typographical Conventions Section

All computer books have a section like this. (I think some federal law requires it.) Read it or skip it.

Sometimes, I refer to key combinations — which means you hold down one key while you press another. For example, Ctrl+Z means you hold down the Ctrl key while you press Z.

For menu commands, I use a distinctive character to separate items on the Ribbon or menu. For example, you use the following command to create a named range in a worksheet:

Formulas  ⇒  Defined Names  ⇒  Define Name

Formulas is the tab at the top of the Ribbon, Defined Names is the Ribbon group, and Define Name is the actual command.

The Visual Basic editor still uses old-fashioned menus and toolbars. So I might tell you to choose Tools  ⇒  Options. That means choose the Tools menu and then choose the Options menu item.

Excel programming involves developing code — that is, the instructions Excel follows. All code in this book appears in a monospace font, like this:

Range("A1:A12").Select

Some long lines of code don’t fit between the margins in this book. In such cases, I use the standard VBA line-continuation character sequence: a space followed by an underscore character. Here’s an example:

Selection.PasteSpecial Paste:=xlValues, _
Operation:=xlNone, SkipBlanks:=False, _
Transpose:=False

When you enter this code, you can type it as written or place it on a single line (omitting the space and underscore combination).

Check Your Security Settings

It's a cruel world out there. It seems that some scam artist is always trying to take advantage of you or cause some type of problem. The world of computing is equally cruel. You probably know about computer viruses, which can cause some nasty things to happen to your system. But did you know that computer viruses can also reside in an Excel file? It's true. In fact, it’s relatively easy to write a computer virus by using VBA. An unknowing user can open an Excel file and spread the virus to other Excel workbooks and to other systems.

Over the years, Microsoft has become increasingly concerned about security issues. This is a good thing, but it also means that Excel users need to understand how things work. You can check Excel's security settings by choosing the File  ⇒  Options  ⇒  Trust Center  ⇒  Trust Center Settings command. There is a plethora of options in there, and people have been known to open that dialog box and never be heard from again.

If you click the Macro Settings tab (on the left side of the Trust Center dialog box), your options are as follows:

· Disable all macros without notification. Macros will not work, regardless of what you do.

· Disable all macros with notification. When you open a workbook with macros, you see the Message Bar open with an option you can click to enable macros, or (if the Visual Basic Editor window is open) you get a message asking if you want to enable macros.

· Disable all macros except digitally signed macros. Only macros with a digital signature are allowed to run (but even for those signatures you haven’t marked as trusted, you still get the security warning).

· Enable all macros. All macros run with no warnings. This option is not recommended because potentially dangerous code can be executed.

Consider this scenario: You spend a week writing a killer VBA program that will revolutionize your company. You test it thoroughly and then send it to your boss. He calls you into his office and claims that your macro doesn't do anything at all. What's going on? Chances are, your boss's security setting does not allow macros to run. Or maybe he chose to go along with Microsoft's default suggestion and disable the macros when he opened the file.

Bottom line? Just because an Excel workbook contains a macro, it is no guarantee that the macro will ever be executed. It all depends on the security setting and whether the user chooses to enable or disable macros for that file.

To work with this book, you need to enable macros for the files you work with. My advice is to use the second security level. Then, when you open a file that you've created, you can simply enable the macros. If you open a file from someone you don't know, you should disable the macros and check the VBA code to ensure that it doesn't contain anything destructive or malicious. Usually, it's pretty easy to identify suspicious VBA code.

Another option is to designate a trusted folder. Choose File  ⇒  Options  ⇒  Trust Center  ⇒  Trust Center Settings. Select the Trusted Locations option and then designate a particular folder to be a trusted location. Store your trusted workbooks there, and Excel won't bug you about enabling macros. For example, if you download the sample files for this book, you can put them in a trusted location.

Foolish Assumptions

People who write books usually have a target reader in mind. For this book, my target reader is a conglomerate of dozens of Excel users I’ve met over the years (either in person or out in cyberspace). The following points more or less describe my hypothetical target reader:

· You have access to a PC at work — and probably at home. And those computers are connected to the Internet.

· You’re running Excel 2013 or Excel 2016.

· You’ve been using computers for several years.

· You use Excel frequently in your work, and you consider yourself to be more knowledgeable about Excel than the average bear.

· You need to make Excel do some things that you currently can’t make it do.

· You have little or no programming experience.

· You understand that the Help system in Excel can actually be useful. Face it — this book doesn't cover everything. If you get on good speaking terms with the Help system, you'll be able to fill in some of the missing pieces.

· You need to accomplish some work, and you have a low tolerance for thick, boring computer books.

Icons Used in This Book

tip Don’t skip information marked with this icon. It identifies a shortcut that can save you lots of time (and maybe even allow you to leave the office at a reasonable hour).

This icon is also used to let you know that the code being discussed is available on the web. Download it to eliminate lots of typing. See “Beyond the Book” for more information.

remember This icon tells you when you need to store information in the deep recesses of your brain for later use.

technicalstuff This icon flags material that you might consider technical. You may find it interesting, but you can safely skip it if you’re in a hurry.

warning Read anything marked with this icon. Otherwise, you may lose your data, blow up your computer, cause a nuclear meltdown — or maybe even ruin your whole day.

Beyond the Book

Hungry for more?

· Sample files: This book has its very own website where you can download the example files. To get these files, point your web browser to

http://dummies.com/extras/excelvbaprogramming

Please note that this URL is case-sensitive and uses all lowercase letters. If you don’t type it exactly, it won’t work.

Having the sample files will save you a lot of typing. Better yet, you can play around with them and experiment with various changes. In fact, I highly recommend playing around with these files. Experimentation is the best way to master VBA.

· Cheat Sheet: The Cheat Sheet is a handy list of common VBA statements for Excel programming. You can find it at

http://dummies.com/extras/excelvbaprogramming

· Updates: Occasionally, we have updates to our technology books. If this book does have technical updates, they will be posted at

http://dummies.com/extras/excelvbaprogramming

Again, this URL is case-sensitive.

Where to Go from Here

Reading this introduction was your first step. Now it’s time to move on and become a programmer. (There’s that p word again!)

If you’re a programming virgin, I strongly suggest that you start with Chapter 1 and progress through the book until you’ve discovered enough to do what you want to do. Chapter 2 gives you some immediate hands-on experience, so you have the illusion that you’re making quick progress.

But it’s a free country (at least, it was when I wrote these words); I won’t sic the Computer Book Police on you if you opt to thumb through randomly and read whatever strikes your fancy.

I hope you have as much fun reading this book as I did writing it.