Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Excel VBA Programming For Dummies
Excel VBA Programming For Dummies
Excel VBA Programming For Dummies
Ebook588 pages6 hours

Excel VBA Programming For Dummies

Rating: 3.5 out of 5 stars

3.5/5

()

Read preview

About this ebook

Take control of your spreadsheets and use VBA to create yourown custom Excel applications

Author and Excel guru John Walkenbach, known to his devoted fansas "Mr. Spreadsheet," guides you through taking your Excel skillsto the next level by creating your own customized spreadsheetsolutions using Visual Basic for Applications (VBA). This updatededition of this bestselling book shows you how to use VBA, writemacros, customize your Excel apps to look and work the way youwant, avoid errors, and more.

  • Introduces you to a wide array of new Excel 2013 options,beginning with the most important tools and operations for theVisual Basic Editor
  • Provides an overview of the essential elements and concepts forprogramming with Excel, including using VBA to customize yourapplications and automate functions
  • Shares techniques for handling errors, debugging, working withrange objects, controlling flow, and much more
  • Zeroes in on creating custom dialog boxes, toolbars, andmenus

Add a personal touch to your spreadsheets and present your datathe way you want with Excel VBA Programming For Dummies, 3rdEdition.

LanguageEnglish
PublisherWiley
Release dateFeb 6, 2013
ISBN9781118490389
Excel VBA Programming For Dummies

Read more from John Walkenbach

Related to Excel VBA Programming For Dummies

Related ebooks

Enterprise Applications For You

View More

Related articles

Reviews for Excel VBA Programming For Dummies

Rating: 3.6 out of 5 stars
3.5/5

30 ratings4 reviews

What did you think?

Tap to rate

Review must be at least 10 words

  • Rating: 2 out of 5 stars
    2/5
    Not the best VBA for beginners! Tom Urtis has the Excel VBA 24-hour trainer which provides the codes
  • Rating: 5 out of 5 stars
    5/5
    Even though the latest verion of Excel is past the version written about in the manual, the content gave a good outline of Excel VBA. It was well worth the read.
  • Rating: 4 out of 5 stars
    4/5
    good job

    1 person found this helpful

  • Rating: 1 out of 5 stars
    1/5
    Good Content

Book preview

Excel VBA Programming For Dummies - John Walkenbach

Part I

9781118490372-pp0101.eps

pt_webextra_bw.TIF Visit www.dummies.com for great Dummies content online.

In this part . . .

check.png Meet Visual Basic for Applications

check.png See examples of some of the things you can do with VBA

check.png Travel back in time and see what Excel was like in the olden days

check.png Work through a real-live Excel programming session

check.png Get a handle on how Excel deals with macro security

check.png Visit www.dummies.com for great Dummies content online.

Chapter 1

What Is VBA?

In This Chapter

arrow Getting a conceptual overview of VBA

arrow Finding out what you can do with VBA

arrow Discovering the advantages and disadvantages of using VBA

arrow Taking a mini-lesson on the history of Excel

If you’re anxious to jump into VBA programming, hold your horses. This chapter is completely devoid of any hands-on training material. It does, however, contain some essential background information that assists you in becoming an Excel programmer. In other words, this chapter paves the way for everything else that follows and gives you a feel for how Excel programming fits into the overall scheme of the universe. It’s not as boring as you might think, so please try to resist the urge to jump to Chapter 2.

Okay, So What Is VBA?

VBA, which stands for Visual Basic for Applications, is a programming language developed by Microsoft — you know, the company that tries to get you to buy a new version of Windows every few years. Excel, along with the other members of Microsoft Office, includes the VBA language (at no extra charge). In a nutshell, VBA is the tool that people like you and me use to develop programs that control Excel.

Imagine an intelligent robot that knows all about Excel. This robot can read instructions, and it can also operate Excel very fast and accurately. When you want the robot to do something in Excel, you write up a set of robot instructions by using special codes. Then you tell the robot to follow your instructions, while you sit back and drink a glass of lemonade. That’s kind of what VBA is all about — a code language for robots. Note, however, that Excel does not come with a robot or lemonade.


technicalstuff.eps A few words about terminology

Excel programming terminology can be a bit confusing. For example, VBA is a programming language, but it also serves as a macro language. What do you call something written in VBA and executed in Excel? Is it a macro, or is it a program? Excel’s Help system often refers to VBA procedures as macros, so I use that terminology. But I also call this stuff a program.

I use the term automate throughout this book. This term means that a series of steps are completed automatically. For example, if you write a macro that adds color to some cells, prints the worksheet, and then removes the color, you have automated those three steps.

By the way, macro does not stand for Messy And Confusing Repeated Operation. Rather, it comes from the Greek makros, which means large — which also describes your paycheck after you become an expert macro programmer.


What Can You Do with VBA?

You’re probably aware that people use Excel for thousands of different tasks. Here are just a few examples:

check.png Analyzing scientific data

check.png Budgeting and forecasting

check.png Creating invoices and other forms

check.png Developing charts from data

check.png Keeping lists of things such as customers’ names, students’ grades, or holiday gift ideas (a nice fruitcake would be lovely)

check.png Yadda, yadda, yadda

The list could go on and on, but I think you get the idea. My point is simply that Excel is used for a wide variety of tasks, and everyone reading this book has different needs and expectations regarding Excel. One thing virtually every reader has in common is the need to automate some aspect of Excel. That, dear reader, is what VBA is all about.

For example, you might create a VBA program to import some numbers and then format and print your month-end sales report. After developing and testing the program, you can execute the macro with a single command, causing Excel to automatically perform many time-consuming procedures. Rather than struggle through a tedious sequence of commands, you can click a button and then hop on over to Facebook and kill some time while your macro does the work.

In the following sections, I briefly describe some common uses for VBA macros. One or two of these may push your button.

Inserting a bunch of text

If you often need to enter your company name, address, and phone number in your worksheets, you can create a macro to do the typing for you. You can extend this concept as far as you like. For example, you might develop a macro that automatically types a list of all salespeople who work for your company.

Automating a task you perform frequently

Assume you’re a sales manager and you need to prepare a month-end sales report to keep your boss happy. If the task is straightforward, you can develop a VBA program to do it for you. Your boss will be impressed by the consistently high quality of your reports, and you’ll be promoted to a new job for which you are highly unqualified.

Automating repetitive operations

If you need to perform the same action on, say, 12 different Excel workbooks, you can record a macro while you perform the task on the first workbook and then let the macro repeat your action on the other workbooks. The nice thing about this is that Excel never complains about being bored. Excel’s macro recorder is similar to recording live action on a video recorder. But it doesn’t require a camera, and the battery never needs to be recharged.

Creating a custom command

Do you often issue the same sequence of Excel menu commands? If so, save yourself a few seconds by developing a macro that combines these commands into a single custom command, which you can execute with a single keystroke or button click. You probably won’t save that much time, but you’ll probably be more accurate. And the guy in the next cubicle will be really impressed.

Creating a custom button

You can customize your Quick Access toolbar with your own buttons that execute the macros you write. Office workers tend to be very impressed by buttons that perform magic. And if you really want to impress your fellow employees, you can even add new buttons to the Ribbon.

Developing new worksheet functions

Although Excel includes hundreds of built-in functions (such as SUM and AVERAGE), you can create custom worksheet functions that can greatly simplify your formulas. I guarantee you’ll be surprised by how easy this is. (I show you how to do this in Chapter 20.) Even better, the Insert Function dialog box displays your custom functions, making them appear built-in. Very snazzy stuff.

Creating custom add-ins for Excel

You’re probably familiar with some of the add-ins that ship with Excel. For example, the Analysis ToolPak is a popular add-in. You can use VBA to develop your own special-purpose add-ins. I developed my Power Utility Pak add-in by using only VBA, and people all around the world pay me real money so they can use it.

Creating complete, macro-driven applications

If you’re willing to spend some time, you can use VBA to create large-scale applications complete with a custom Ribbon tab, dialog boxes, on-screen help, and lots of other accoutrements. This book doesn’t go quite that far, but I’m just telling you this to impress you with how powerful VBA really is.

Advantages and Disadvantages of VBA

In this section, I briefly describe the good things about VBA — and I also explore its darker side.

VBA advantages

You can automate almost anything you do in Excel. To do so, you write instructions that Excel carries out. Automating a task by using VBA offers several advantages:

check.png Excel always executes the task in exactly the same way. (In most cases, consistency is a good thing.)

check.png Excel performs the task much faster than you can do it manually (unless, of course, you’re Clark Kent).

check.png If you’re a good macro programmer, Excel always performs the task without errors (which probably can’t be said about you or me).

check.png If you set things up properly, someone who doesn’t know anything about Excel can perform the task.

check.png You can do things in Excel that are otherwise impossible — which can make you a very popular person around the office.

check.png For long, time-consuming tasks, you don’t have to sit in front of your computer and get bored. Excel does the work, while you hang out at the water cooler.

VBA disadvantages

It’s only fair that I give equal time to listing the disadvantages (or potential disadvantages) of VBA:

check.png You have to know how to write programs in VBA (but that’s why you bought this book, right?). Fortunately, it’s not as difficult as you might expect.

check.png Other people who need to use your VBA programs must have their own copies of Excel. It would be nice if you could press a button that transforms your Excel/VBA application into a stand-alone program, but that isn’t possible (and probably never will be).

check.png Sometimes, things go wrong. In other words, you can’t blindly assume that your VBA program will always work correctly under all circumstances. Welcome to the world of debugging and, if others are using your macros, technical support.

check.png VBA is a moving target. As you know, Microsoft is continually upgrading Excel. Even though Microsoft puts great effort into compatibility between versions, you may discover that the VBA code you’ve written doesn’t work properly with older versions or with a future version of Excel.

VBA in a Nutshell

Just to let you know what you’re in for, I’ve prepared a quick and dirty summary of what VBA is all about. Of course, I describe all this stuff in semi-excruciating detail later in the book.

check.png You perform actions in VBA by writing (or recording) code in a VBA module. You view and edit VBA modules by using the Visual Basic Editor (VBE).

check.png A VBA module consists of Sub procedures. A Sub procedure has nothing to do with underwater vessels or tasty sandwiches. Rather, it’s a chunk of computer code that performs some action on or with objects (discussed in a moment). The following example shows a simple Sub procedure called AddEmUp. This amazing program displays the result of 1 plus 1:

Sub AddEmUp()

    Sum = 1 + 1

    MsgBox The answer is & Sum

End Sub

A Sub procedure that doesn’t perform properly is said to be substandard.

check.png A VBA module can also have Function procedures. A Function procedure returns a single value. You can call it from another VBA procedure or even use it as a function in a worksheet formula. An example of a Function procedure (named AddTwo) follows. This Function accepts two numbers (called arguments) and returns the sum of those values:

Function AddTwo(arg1, arg2)

    AddTwo = arg1 + arg2

End Function

A Function procedure that doesn’t work correctly is said to be dysfunctional.

check.png VBA manipulates objects. Excel provides dozens and dozens of objects that you can manipulate. Examples of objects include a workbook, a worksheet, a cell range, a chart, and a shape. You have many more objects at your disposal, and you can manipulate them by using VBA code.

check.png Objects are arranged in a hierarchy. Objects can act as containers for other objects. At the top of the object hierarchy is Excel. Excel itself is an object called Application. The Application object contains other objects such as Workbook objects and Add-In objects. The Workbook object can contain other objects, such as Worksheet objects and Chart objects. A Worksheet object can contain objects such as Range objects and PivotTable objects. The term object model refers to the arrangement of these objects. (Object model mavens can find out more in Chapter 4.)

check.png Objects of the same type form a collection. For example, the Worksheets collection consists of all the worksheets in a particular workbook. The Charts collection consists of all Chart objects in a workbook. Collections are themselves objects.

check.png You refer to an object by specifying its position in the object hierarchy, using a dot (a.k.a., a period) as a separator. For example, you can refer to the workbook Book1.xlsx as

Application.Workbooks(Book1.xlsx)

This refers to the workbook Book1.xlsx in the Workbooks collection. The Workbooks collection is contained in the Application object (that is, Excel). Extending this to another level, you can refer to Sheet1 in Book1.xlsx as

Application.Workbooks(Book1.xlsx).Worksheets(Sheet1)

As shown in the following example, you can take this to still another level and refer to a specific cell (in this case, cell A1):

Application.Workbooks(Book1.xlsx).Worksheets(Sheet1).Range(A1)

check.png If you omit specific references, Excel uses the active objects. If Book1.xlsx is the active workbook, you can simplify the preceding reference as follows:

Worksheets(Sheet1).Range(A1)

If you know that Sheet1 is the active sheet, you can simplify the reference even more:

Range(A1)

check.png Objects have properties. You can think of a property as a setting for an object. For example, a Range object has such properties as Value and Address. A Chart object has such properties as HasTitle and Type. You can use VBA to determine object properties and also to change properties.

check.png You refer to a property of an object by combining the object name with the property name, separated by a dot. For example, you can refer to the Value property in cell A1 on Sheet1 as follows:

Worksheets(Sheet1).Range(A1).Value

check.png You can assign values to variables. A variable is a named element that stores information. You can use variables in your VBA code to store such things as values, text, or property settings. To assign the value in cell A1 on Sheet1 to a variable called Interest, use the following VBA statement:

Interest = Worksheets(Sheet1).Range(A1).Value

check.png Objects have methods. A method is an action Excel performs with an object. For example, one of the methods for a Range object is ClearContents. This aptly named method clears the contents of the range.

check.png You specify a method by combining the object with the method, separated by a dot. For example, the following statement clears the contents of cell A1:

Worksheets(Sheet1).Range(A1).ClearContents

check.png VBA includes all the constructs of modern programming languages, including variables, arrays, and looping. In other words, if you’re willing to spend a little time mastering the ropes, you can write code that does some incredible things.

Believe it or not, the preceding list pretty much describes VBA in a nutshell. Now you just have to find out the details. That’s why this book has more pages.

An Excursion into Versions

If you plan to develop VBA macros, you should have some understanding of Excel’s history. I know you weren’t expecting a history lesson when you picked up this book, but bear with me. This is important stuff that might make you a hit at the next nerd party.

Here are all the major Excel for Windows versions that have seen the light of day, along with a few words about how they handle macros:

check.png Excel 2: The original version of Excel for Windows was called Version 2 (rather than 1) so that it would correspond to the Macintosh version. Excel 2 first appeared in 1987, but nobody uses it anymore, so you can pretty much forget that it ever existed.

check.png Excel 3: Released in late 1990, this version features the XLM macro language. Nobody uses this version either.

check.png Excel 4: This version hit the streets in early 1992. It also uses the XLM macro language. Perhaps 10 to12 people still use this version. (They subscribe to the philosophy if it ain’t broke, don’t fix it.)

check.png Excel 5: This one came out in early 1994. It was the first version to use VBA (but it also supports XLM). I haven’t heard from anyone who uses Excel 5 in years.

check.png Excel 95: Technically known as Excel 7 (there is no Excel 6), this version began shipping in the summer of 1995. It has a few VBA enhancements, and it supports the XLM language. It’s rarely used any more.

check.png Excel 97: This version (also known as Excel 8) was born in January 1997. It has many enhancements, and it features an entirely new interface for programming VBA macros. Excel 97 also uses a new file format (which previous Excel versions cannot open). Occasionally, I run into someone who still uses this version.

check.png Excel 2000: This version’s numbering scheme jumped to four digits. Excel 2000 (also known as Excel 9) made its public debut in June 1999. It includes only a few enhancements from a programmer’s perspective. Excel 2000 is also rarely used.

check.png Excel 2002: This version (also known as Excel 10 or Excel XP) appeared in late 2001. Perhaps this version’s most significant feature is the ability to recover your work when Excel crashes. People still use it.

check.png Excel 2003: Of all the Excel upgrades I’ve ever seen (and I’ve seen them all), Excel 2003 has the fewest new features. In other words, most hard-core Excel users (including yours truly) were very disappointed with Excel 2003. As I write this, Excel 2003 is still a commonly used version. It’s also the last pre-Ribbon version of Excel.

check.png Excel 2007: Excel 2007 signaled the beginning of a new era. Excel 2007 dumped the old menu and toolbar interface and introduced the Ribbon. I, for one, was disappointed to discover that you can’t modify the Ribbon by using VBA. But it had enough new features to satisfy me, such as a new file format and support for much larger worksheets — more than a million rows.

check.png Excel 2010: Microsoft outdid its corporate self with this version. This version has some slick new features (such as sparkline graphics), and it also performs quite a bit better in some areas. And if you need really, really huge workbooks, you can install the 64-bit version. But again, I was disappointed because there’s still no way to modify the Ribbon using VBA.

check.png Excel 2013: The latest version, and the one I used while I was writing this edition of the book. Excel 2013 is also available in an online version and for tablets. The Ribbon is still around, but it now has a flat look — and you still can’t modify it using VBA!

remember.eps This book is written for Excel 2010 and Excel 2013. If you don’t have one of those versions, you run the risk of getting confused in a few places.

So what’s the point of this mini history lesson? If you plan to distribute your Excel/VBA files to other users, it’s vitally important that you understand which version of Excel they use. People using an older version won’t be able to take advantage of features introduced in later versions. For example, if you write VBA code that references cell XFD1048576 (the last cell in a workbook), those who use a version prior to Excel 2007 will get an error because those pre-Excel 2007 worksheets only had 65,536 rows and 255 columns (the last cell is IV65536).

Excel 2010 and later also have some new objects, methods, and properties. If you use these in your code, users with an older version of Excel will get an error when they run your macro — and you’ll get the blame.

tip.eps The good news is that Microsoft has made available an Office Compatibility Pack, which allows users of Excel 2003 and Excel XP to open and save workbooks in the new file format. This product (which is free, by the way) doesn’t give these older versions the new features. It just lets them open and save files in the newer file format.

Chapter 2

Jumping Right In

In This Chapter

arrow Developing a useful VBA macro: A hands-on, step-by-step example

arrow Recording your actions by using Excel’s macro recorder

arrow Examining and testing recorded code

arrow Dealing with macro security issues

arrow Changing a recorded macro

I’m not much of a swimmer, but I have found that the best way to get into a cold body of water is to jump right in — no sense prolonging the agony. By wading through this chapter, you can get your feet wet immediately but avoid getting in over your head.

By the time you reach the end of this chapter, you may start feeling better about this Excel programming business, and you’ll be glad you took the plunge. This chapter provides a step-by-step demonstration of how to develop a simple but useful VBA macro.

First Things First

Before you can call yourself an Excel programmer, you must go through the initiation rites. That means you need to make a small change so Excel will display a new tab at the top of the screen: Developer. Getting Excel to display the Developer tab is easy (and you only have to do it one time). Just follow these steps:

1. Right-click any part of the Ribbon and choose Customize the Ribbon.

2. In the Customize Ribbon tab of the Excel Options dialog box, locate Developer in the second column.

3. Put a check mark next to Developer.

4. Click OK, and you’re back to Excel with a brand-new tab: Developer.

When you click the Developer tab, the Ribbon displays information that is of interest to programmers (that’s you!). Figure 2-1 shows how the Ribbon looks when the Developer tab is selected in Excel 2013.

9781118490372-fg0201.tif

Figure 2-1: The Developer tab is normally hidden, but it’s easy to unhide.

What You’ll Be Doing

In this section, I describe how to create your first macro. The macro that you’re about to create will do this.

check.png Type your name into a cell.

check.png Enter the current date and time into the cell below.

check.png Format both cells to display bold.

check.png Change the font size of both cells to 16 point.

This macro won’t be winning any prizes in the Annual VBA Programmer’s Competition, but everyone must start somewhere. The macro accomplishes all these steps in a single action. As I describe in the following sections, you start by recording your actions as you go through these steps. Then you test the macro to see whether it works. Finally, you edit the macro to add some finishing touches. Ready?

Taking the First Steps

This section describes the steps you take prior to recording the macro. In other words, you need to make a few preparations before the fun begins.

1. Start Excel if it’s not already running.

2. If necessary, create a new, empty workbook (Ctrl+N is my favorite way to do that).

3. Click the Developer tab, and take a look at the Use Relative References button in the Code group.

If the color of that button is different than the other buttons, then you’re in good shape. If the Use Relative References button is the same color as the other buttons, then you need to click it.

I explain more about the Use Relative References button in Chapter 6. For now, just make sure that the option is turned on. When it’s turned on, it will be a different color.

Recording the Macro

Here comes the hands-on part. Follow these instructions carefully:

1. Select a cell — any cell will do.

2. Choose DeveloperCodeRecord Macro or click the macro recording button on the status bar.

The Record Macro dialog box appears, as shown in Figure 2-2.

9781118490372-fg0202.eps

Figure 2-2: The Record Macro dialog box appears when you’re about to record a macro.

3. Enter a name for the macro.

Excel provides a default name (something like Macro1), but it’s better to use a more descriptive name. NameAndTime (with no spaces) is a good name for this macro.

4. Click in the Shortcut Key box and enter Shift+N (for an uppercase N) as the shortcut key.

Specifying a shortcut key is optional. If you do specify one, then you can execute the macro by pressing a key combination — in this case, Ctrl+Shift+N.

5. Make sure the Store Macro In setting is This Workbook.

6. You can enter some text in the Description box, if you like.

This step is optional. Some people like to describe what the macro does (or is supposed to do).

7. Click OK.

The Record Macro dialog box closes, and Excel’s macro recorder is turned on. From this point, Excel monitors everything you do and converts it to VBA code.

8. Type your name in the active cell.

9. Move the cell pointer to the cell below and enter this formula:

=NOW()

The formula displays the current date and time.

10. Select the formula cell and press Ctrl+C to copy that cell to the Clipboard.

11. Choose HomeClipboardPasteValues (V).

This command converts the formula to its value.

12. With the date cell selected, press Shift+up arrow to select that cell and the one above it (which contains your name).

13. Use the controls in the HomeFont group to change the formatting to Bold and make the font size 16 point.

14. Choose DeveloperCodeStop Recording.

The macro recorder is turned off.

Congratulations! You just created your first Excel VBA macro. You may want to phone your mother and tell her the good news.

Testing the Macro

Now you can try out this macro and see whether it works properly. To test your macro, move to an empty cell and press Ctrl+Shift+N.

In a flash, Excel executes the macro. Your name and the current date and time are displayed in large, bold letters.

tip.eps Another way to execute the macro is to choose Developer⇒Code⇒Macros (or press Alt+F8) to display the Macros dialog box. Select the macro from the list (in this case, NameAndTime) and click Run. Make sure you select the cell that will hold your name before executing the

Enjoying the preview?
Page 1 of 1