Excel 2010: Macros in Depth

Excel 2010: Macros in Depth

with Dennis Taylor

 


In Excel 2010: Macros in Depth, author Dennis Taylor explains how macros can be used to automate tasks in Excel and walks through the process of creating and running simple macros. This course also covers advanced topics such as making macros widely available through the Personal Macro Workbook, recording a macro in stages, absolute and relative recording, and using Visual Basic for Applications (VBA) for actions that can't be recorded. The course wraps up with a macro project that brings together each of the elements in a real-world scenario. Exercise files are included with the course.
Topics include:
  • Understanding what a macro does
  • Recording and adding functionality to a macro
  • Running macros
  • Creating non-recordable VBA code
  • Testing a macro in Step mode
  • Joining two macros
  • Using loop structures
  • Streamlining macros
  • Creating a Personal Macro Workbook

show more

author
Dennis Taylor
subject
Business, Productivity
software
Excel 2010, Office 2010
level
Intermediate
duration
2h 44m
released
Jan 20, 2011

Share this course

Ready to join? subscribe


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:04Hi! I am Dennis Taylor.
00:06Welcome to Microsoft Excel 2010: Macros in Depth.
00:10If you want to eliminate some of the drudgery associated with repetitious
00:14command sequences or data manipulation tasks, then you need to know more about
00:19macros, Excel's automation capability.
00:23I'll show you when it's appropriate to use macros, and in the process
00:27familiarize you with the following features:
00:30creating macros by recording keystrokes, expanding a macro for more than one
00:35task, making macros available to all Excel workbooks, launching macros with
00:41keystroke shortcuts and toolbar buttons, editing VBA code and making sense of
00:47that VBA environment, debugging and testing macros via step mode and split
00:52screens, and expanding the power of macros with Do loops and If commands.
00:59For many Excel users, macros become a power tool with seemingly
01:03unlimited capabilities.
01:05I'll show you this power as we get started with Excel 2010:
01:10Macros in Depth.
Collapse this transcript
Using the exercise files
00:01If you have access to the exercise files for this course, you can put them on
00:05the Desktop as I have--
00:06I am about to open one here-- or anywhere else you want.
00:09If you do not have access to these files, you can follow along with your own
00:13files as we proceed through the course.
Collapse this transcript
1. Getting Started
Understanding what a macro does
00:00When using Excel, do you sometimes say to yourself--or maybe even out loud--"here
00:05I go again," as you repeat a frequently used command sequence or maybe a series of
00:09steps--something you do frequently?
00:11If so, you need to learn how macros can automate these steps.
00:16Let's take a look at a few examples, and I also want to suggest that, in a certain sense,
00:21you've been using macros all along;
00:23you just haven't thought about it in that way, or you maybe haven't used that word.
00:27Let's take a simple process here.
00:29Maybe in this list here, when someone leaves the organization, at least for a
00:34while, what you don't do is remove the record.
00:37You simply want to use strikethrough.
00:40In other words, Madelyn O'Brien here has left; I want to use strikethrough on these cells.
00:45Most of you probably know where to find that feature.
00:47You'll go to the Home tab, and possibly click the Dialog Box launcher right here.
00:54You might know a shortcut by pressing Ctrl+1, or you could right-click and
00:59go to Format Cells.
01:01The point is, it's a few clicks here and there.
01:04You will track down this capability.
01:06If you're going to Format Cells, you might have to activate the Font tab if it's
01:10not already active, and there is Strikethrough.
01:14Now, I wouldn't suggest that a lot of people will use that feature
01:17frequently, but you could imagine if it becomes pretty important to you, and you
01:21use it a lot, you'd like to make it faster. And you certainly can use a couple
01:26of shortcuts like I suggested here.
01:28Maybe click this next time, or press Ctrl+1 to get into that dialog box a bit faster.
01:34But it still is going to to take a few clicks. And then one day somebody tells
01:38you out of the blue "Oh, by the way, there is a keystroke shortcut already built into Excel, Ctrl+5
01:44and furthermore, it's a toggle; it's an on/off."
01:46We don't call that a macro, but in a certain sense it is.
01:51It embodies the idea that a short sequence, in this case a keystroke shortcut,
01:56takes the place of those four or five clicks that we just went through before.
02:00It would be incorrect to suggest that macros are simply about short, little
02:05processes that are going to save you five steps here or five steps there.
02:10But we start with this idea that anytime you want to take a series of steps or
02:15actions and essentially turn them into a single action, that's the starting
02:20point for talking about macros.
02:23At other times we do this as well too. With so many different features, we
02:27have shortcuts already.
02:29If you didn't know Excel very well, and you said, "I want to make this bold,"
02:33I certainly couldn't imagine anybody using Excel and not knowing about the
02:36letter B, but if you were using the Data tab for whatever reason, and you
02:40couldn't find it, you'd be complaining a little bit saying, "I got to go back here."
02:44and "What's the sequence?"
02:46You'd think out how many steps it takes you to get to a certain feature.
02:50Let's go into things that maybe are not quite so well known and suggest more
02:55ideas regarding macros.
02:57Here is a worksheet called Formulas, and it's a real mess.
03:02It needs some cleanup, and maybe you encounter worksheets like this from time to
03:06time, and you need to get your bearings kind of quickly.
03:09In prior versions of Excel going way back, it used to take quite a while to
03:14figure out where the formulas are.
03:16What I'm about to do here is going to be fast and easy.
03:19It probably doesn't require a macro, but sometimes you get annoyed at processes
03:24that even take only three or four steps.
03:26How to we find all the formulas in this particular worksheet?
03:30On the Home tab, the extreme right button called Find & Select, there is a
03:35choice called Formulas.
03:36It highlights all the formulas.
03:38Now you might want to add a color to them.
03:41That would take another step.
03:42Make them yellow or something like that if you wish. But that's certainly a handy feature.
03:46If you use it often, does it bother you that you always have to go to the Home
03:50tab, then click that button, then make the choice, then add the colors?
03:55Well, certainly the steps that we just went through could be turned into a macro.
03:59So what I'm suggesting here, at least initially, is the idea that any sequence of
04:05actions could be turned into a macro by a process known as Recording.
04:11We can turn on a recorder and go through those various steps manually while
04:15they're being stored for us.
04:17Then in the future, we can either use a keystroke shortcut or a new button in
04:23the Quick Access Toolbar to actually execute that sequence with a single action.
04:29I'm going to click Ctrl+Z to undo this.
04:32A companion feature to this would take considerably longer.
04:36I'm not necessarily suggesting this is a feature that everybody needs, but it
04:40certainly could be valuable in some worksheets if you say, I would like to
04:44highlight all of the cells that just have pure values in them. That cell's got
04:49a pure number in it.
04:50So does this cell. Not that cell, that's got a formula.
04:54We already know how to find those.
04:56So how do we find the cells that have pure values?
04:59It's important in both of these examples that you click on just one cell.
05:04If you highlight two or three or five, the search will only take place
05:08within those cells.
05:10So we click on the single cell here, and again on the Home tab, Find & Select.
05:15This time the not so obvious choice, Go To Special.
05:20We see a choice here called Constants, but that will also select text and
05:25logicals and errors, so then we would uncheck Text and Logicals and
05:30Errors, and click OK.
05:32What do we have highlighted? Just values. And here, too,
05:37if you would like to apply a color, go do that.
05:40So the issue is, is this the kind of sequence that maybe you've already been
05:44performing from time to time, and you start to use it more frequently, and it's
05:49six or eight or 10 steps--
05:50we're not really counting here--
05:52you say, I wish I had a faster way to do that.
05:55That's the starting point for certain kinds of macros.
05:58For want of a better term, perhaps we would call these "utility macros," but that's
06:03certainly one thought process that we have here.
06:06But let's suggest another idea as well.
06:09Suppose a couple of times a week it's important for different people in the
06:13organization to get a list of the employees and who they are and where
06:18they work and so on.
06:19Maybe every time you do this you sort the list first by Department, and then you
06:24sort it again by Employee Name.
06:26There are a few people that need that list.
06:29Maybe another thing you need to do is to use a filter on this list and show
06:34only the hourly people.
06:36Now I'm not just suggesting make-work projects.
06:38I'm suggesting these are things that you do on a regular basis.
06:42Maybe you do that twice a week. And it doesn't take that long really, but it's
06:46five, ten minutes, whatever.
06:47What if you could click a button, and have all those steps essentially run for you?
06:53It's going to save you some time.
06:55That's the idea behind macros.
06:58Initially, we want to focus on the idea that any actions that you take in
07:02Excel could be recorded so that the next time you need to use them, those
07:08actions happen all at once.
07:10It even can be set up in such a way that no matter how big this list becomes, or
07:15whether it shrinks or grows, the macros will still work.
07:19So we're giving you some ideas for the rationale of macros.
07:23Why do I need macros?
07:25To automate your work and save some time--and sometimes lots of time!
Collapse this transcript
Reviewing macro examples
00:00Before actually creating some macros, it's worthwhile to see some more examples
00:06to deepen the rationale as to why we want to use this great feature.
00:10In this particular worksheet, you see these numbers in millions--nothing
00:13wrong with this at all.
00:15Many people who deal with large numbers, however, might want to use a different format.
00:19I'm just going to highlight these cells here For example, in cell E5 here,
00:24how about that being display as 415.1?
00:27That sounds like something easy to do.
00:31I'm going to do it manually here by simply right-clicking, going to Format
00:36Cells. And the solution is not one of the built-in number formats, but you need
00:40to go to Custom--and actually you can start with almost any one of these--and
00:45alter it to include a .0 and, believe it or not, comma, comma.
00:51And that will display these values in millions with a single decimal place,
00:55and you could easily imagine someone wanting to have that at his or her figure tips.
01:00And we'll be showing you how you can have macros be executable, or you can
01:04make them happen, with a single keystroke shortcut or button in the Quick Access Toolbar.
01:10Another feature, one that I don't use a whole a lot--but take a look at this idea
01:14here with this data here.
01:17Double underlining is a feature that not everybody needs, but nevertheless on
01:22the Home tab you will see an underline button--and some of you use double-
01:26underlining--that's one kind of double underlining.
01:29There is another kind called double accounting. In this example here, I'm not
01:34making the case for saying everybody needs this, but if you need this, you'll
01:38right-click, go to Format Cells, go to Font tab, and choose Double Underlining.
01:45The only difference between two of these, by the way, is how close it puts
01:48the lines to the texts.
01:49But the idea is it's a feature that you need, and if you do, you get annoyed at
01:55the fact that takes to long to get here.
01:58Now we have already suggested some other macros, for example, a list here, the
02:03idea that we might frequently sort this and might hide column along the way,
02:07we might filter it. If it's a process that we use often, we will definitely want to
02:12use macros to record those processes, and then the next time we need them, just
02:17execute them quickly.
02:19But here is another list.
02:20This is about 2,400 rows.
02:23It's a list of realtors from one state.
02:26We've got six rows of information about each person here. And the data is great. We need it.
02:31It's going to help us enormously.
02:33It's not in the form that we want, however, and try as we might, they won't send
02:37it to us the way we want.
02:38What are we trying to do here?
02:40You could easily imagine the typical Excel users saying "I would like to get all
02:44the names, for example, in column D, get those name over there, put the company
02:49names right here maybe, put the business addresses right here." And as you look
02:54at the data, a good Excel user will probably think out three or four different
02:58ways, maybe, to do this.
03:00You might use a filter.
03:01You might just start copying the data. What you wouldn't recognize at first
03:06though is the following idea, and that's a new idea here.
03:10If you were to highlight these cells, either one by one or in clusters, and copy
03:16the data, for example, over here, so it will look like that and then do the same
03:22thing with the next set of names and the next set of names,
03:27you could do that in the certain way while recording your actions. And then, and
03:32this would be a huge leap and you wouldn't make this intuitively.
03:36you could essentially say, let's take these actions and perform them over and
03:42over and over throughout this entire list of data here.
03:46And again, that's the kind of thing that macros can do that you don't quite
03:51pick up on at first.
03:52If we simply confine the idea of a macro, it only picks up recorded actions.
03:58You wouldn't make this leap. But this macro that we're talking about here
04:02has been written. We'll show you how a bit later, and here is a button to make it happen.
04:072,400 rows. Watch what happens. And it's not going to take very long, and there is
04:15way also to not show this interim activity, and it will happen even faster.
04:21When you start to see some examples like this, I thing there is a little doubt as
04:25to why you need to know more about macros.
Collapse this transcript
Understanding security
00:00Even before grasping the full meaning of what a macro is, you need to know about
00:04the security issues associated with macro usage.
00:08Currently on the screen I'm using a file that doesn't have any micros in it.
00:11It's called Profits.
00:12You might notice its file extension, and depending upon your display
00:16settings, you might not be seeing a file extension yourself. But the standard
00:20file extension in Excel files in 2010 is .xlsx.
00:26I'm about open another file.
00:28This file is called Large Macro Example.
00:32You will notice its file extension, xlsm. That means it has macro in it.
00:37I'm going to open this simply by double-clicking, and here is a prompt:
00:42"Microsoft office has identified a potential security concern," and the warning sounds
00:48rather dire. But I think lot of you know, and you been warned by your IT
00:52departments in many cases, that macros can be a repository for viruses.
00:57So the default notice in Excel will appear like this when you open a file
01:02that has macros in it.
01:04We certainly like to run macros, so we'll enable the macros.
01:08Of course you do have the other choice. And if you know this file, of course
01:12you're going to be choosing enable macros.
01:14Now if you know this file well, if you store it in a particular location, you're
01:19very familiar with it and no body else has accesses to it,
01:22you are going to get a little tired of that notification all the time. And using
01:26the File tab in the menu, we can go to Options, and then on the left-hand side,
01:34Trust Center. We've got some control here by then clicking on Trust Center
01:40settings and then, on the left side, Trusted Locations. If a file comes from one
01:47of these locations, and I believe these are the defaults you get at start-up, and you
01:51can change them or get rid of some of them if you think that's necessary.
01:55But you can add trusted locations, meaning paths here, folders.
02:01We can add new locations, so when we open files from those location,
02:05we're not going to be getting the prompt.
02:07Add a new location, and I was strongly recommended here that you use browse.
02:13So I'm going to find the location where some of the files are that I need.
02:17It's in a folder called Exercises.
02:20Now that does have other folders within it. And possibly in some situations you
02:25might want to treat some of these folders as Trusted Locations but not others.
02:30I hardly doubt that, so what might we do here? I'm pressing backspace here.
02:35We can choose the exercises file folder. Now, that by itself doesn't mean that
02:41all the subfolders are trusted as well, so you do want to click the box here to the left:
02:46Subfolders of this location are also trusted. So there is the path right there.
02:52That's the trusted location, and all the subfolders of that are also
02:56trusted. Click OK, OK, OK.
03:00I will close this file and open it again. I could just do it either through
03:07File > Open, or the Recent File list.
03:10There it is right there. Open this file, no questions I asked.
03:13We're ready to run the macro or do with this file what we wish without
03:18getting that prompt.
03:19So there are some security concerns. You want be aware of the idea, and perhaps
03:24many of you working with your IT departments have been told, about the security
03:28concerns associated with opening files that have macros in them.
03:33Unlike many Excel features, security concerns here, as we have seen, are much more
03:37important when we talk about macros.
Collapse this transcript
Reviewing the recording method
00:00The best way to get started creating a macro is by recording your actions.
00:05Even power-macro users will use this is a fallback method for creating a macro.
00:11In this particular worksheet, we've decided it's important to us. We are also
00:15thinking how important it might be in other worksheets here as well.
00:19We'd like to highlight just the cells that have pure values in them.
00:23A quick reminder of how we do this: on the Home tab, the rightmost button, Find &
00:29Select, has an option called Go To Special and by clicking Constants and then
00:34unchecking the boxes for Text, Logicals, and Errors and then clicking OK,
00:40we have highlighted only the cells with pure values in them.
00:44We could then apply a color to these. But we'd like to have that happen
00:48instantaneously because we've identified it is something we'd like to use often,
00:54and we want it to be fast.
00:56If we highlight a few cells and go through those steps, by the way, we will only
01:00be selecting the cells within this range. By clicking on a single cell, we are
01:04about to check the entire worksheet.
01:08So how do we create a macro by recording?
01:11In the lower-left corner of your screen, you'll see the word "Ready," and to the
01:15right of it you might or might not be seeing a button.
01:18I'm not seeing a button here,
01:20so I'm going to right-click in that status bar. And you'll notice a lot of
01:24selections here, including one called Macro Recording.
01:28By checking this option, you see the check in front of it, and also at the bottom
01:33of the screen, we now have a button.
01:36This is one of two major ways to start recording a macro.
01:40The current message says, "No macros are currently recording.
01:44Click to begin recording a new macro."
01:48Another way to start macro recording is to click the View tab in the Ribbon and
01:54the rightmost button called Macros.
01:56There is a drop arrow there, Record Macro.
02:00A third way is adding a new tab in the Ribbon called Developer.
02:05We don't really need to do that, but that's another way.
02:07It will give us some buttons up here for starting a macro recording as well.
02:12We can use either of the two techniques I first alluded to.
02:15We've identified the need, possibly we've written down the steps;
02:18we are ready to do recording.
02:21In the lower-left corner, I am going to click that button, and here's a
02:24dialog box, Record Macro.
02:27We need to give the macro a name.
02:29We always represent it with the default name, but we want it to be meaningful.
02:32That name can be pretty free-form with a few obvious exceptions.
02:37It cannot begin with a number.
02:39It cannot contain spaces.
02:42If you'd like to simulate different words and make it looks somewhat like a
02:45sentence, that's fine, but use the underscore character or simply shift between
02:49upper- and lowercase.
02:51Ideally we want this to describe what the macro does.
02:55As you see more and more macro, many, many times, a good name helps define and
03:01describe what the macro dose.
03:03Why not "HighlightValueCells?"
03:04I am not using underscore.
03:09I am just shifting case here.
03:10A keystroke shortcut is extremely valuable, and it's a fast way to run a macro
03:15later--make it happen. But we have only 52 choices here: any of the lowercase
03:20letters, any of the uppercase letters. And if I were to use the letter C here,
03:25thinking of the word "cells," I would not be able to use Ctrl+C for copy anymore.
03:30So there's a tendency among some users to simply use the Shift key because these
03:34combinations are not widely used.
03:36So I am holding down the Shift key here, and maybe I'll use the letter H. I am
03:39thinking of the word "highlight."
03:41You can't always come up with the letter that you always want here.
03:44Ctrl+Shift+H will be the way that we make this macro happen later.
03:49We want to store it in this workbook.
03:51There are some other options here, which we'll see a bit later, and describe the macro.
03:56Ideally, you want to provide this because the more macros you see--as you're
04:01looking at code, rather than trying to figure out what's going on--you want a description.
04:06We want to say also here we are on the verge of creating a macro, and what that
04:11really means is whatever actions we take are going to be recorded so we can play
04:16them back, but they actually get converted into a language called Visual Basic
04:21for Applications, VBA. More about that later.
04:24The description here what I often do is simply copy the name, make some
04:28adjustments, maybe expand it a little bit.
04:30I just press Ctrl+C. Down here I'll press Ctrl+V to paste. Maybe do this:
04:35"HighlightValueCells," and maybe we'll just add the phrase "in this worksheet"
04:40because that's what it mean, of course, only this worksheet.
04:46As we click OK, a quick reminder here: we are on the verge of saying let's
04:51record everything until we stop recording.
04:54So as I click OK, and you will notice in the lower-left corner of the screen,
04:58the previous icon has been replaced with a box: "A macro was currently recording.
05:04Click to stop recording."
05:06Nearly everything we do will be recorded.
05:09Now if I do scrolling like this and-- maybe that was a mistake--that's actually
05:13going to be in the macro. It's so innocuous
05:16we'll never know it anyway, but you want to stay focused and do only the things
05:20that you want to have recorded.
05:22If you happen to enter a dialog box, if, for example, if you were to right-click
05:26on a cell and press Format Cells--anything you do here--
05:30if you press Cancel, it is meaningless.
05:33So those kinds of actions don't appear in the Macro; nearly everything else does.
05:38So what do we want to do?
05:39Exactly what we did before, Home tab > Find & Select > Go To Special >
05:46Constants, uncheck Text, Logicals, Errors, click OK.
05:51Let's apply color; maybe we'll make it green. And although its not critical,
05:56right now these cells are highlighted, and they are in color. It's a convention
05:59to click outside, or maybe we'll just click on cell A1. And they are no longer
06:03highlighted, but the color is there.
06:05We are done recording. Lower-left corner,
06:08there is a box. Click it.
06:10We have stopped recording.
06:12We have created a macro.
06:13It's fast, it's easy. And let's test it.
06:17Now something you'll learn early on when you run macros: you cannot use the Undo
06:23feature to undo what you just did. And that makes us think about macros might be
06:28deleting data--and we want to stay away from those for a while. But since we
06:32went through the action of recording these, maybe the best thing to do if we
06:36want to test this is go to a different worksheet, over to Profits here.
06:39It looks like there's some color already there.
06:41Let me take it of manually. Click here, No Fill.
06:47Are there cells in this worksheet that contain values?
06:49Well, we know there are, but where are they?
06:51We'll use that keystroke shortcut.
06:54I use Ctrl+Shift+H. It highlights these cells in color.
06:58We could go to different worksheets and try this.
07:01How about over here? Any cells with pure values? Ctrl+Shift+H. We see that it's working.
07:08The macro was recorded when using the formula sheet. But it's not stored here;
07:13it's stored in this workbook, so it can be used anywhere. And we've just
07:17demonstrated the relatively easy process of recording a macro.
07:21It's going to be the dominant, but certainly not the only, method of
07:25creating macros.
Collapse this transcript
Understanding other techniques for macros
00:00In this workbook a macro has been run, and its purpose was to highlight
00:04just the cells that have pure values and make them green, and that helps us
00:09understand the workbook.
00:10The method used for creating the macro was recording, the dominant way to create
00:15macros. But there are other techniques, and we need to see, from time to time,
00:20the actual macro code.
00:22When you record a macro, you actually create a small program.
00:27Well, let's take a look at it. Where is it? How do we find it?
00:31On the View tab in the Ribbon, the right-most button called Macros, the drop
00:36arrow, the choice View Macros.
00:39This will show us the macros in this workbook, and potentially others.
00:42The macro we're talking about here you can guess by the name, Highlight Value Cels.
00:47There is another macro that displays values in millions.
00:50Let's look at the code.
00:52When we create a macro by recording, we create a small program in the language
00:57called VBA, Visual Basics for Applications.
01:00Let's edit this. Here is the code. We're in a different window with the Visual Basic
01:06editor. This is the macro that performs the action of selecting cells that
01:12have values in them.
01:13During the recording of this, I did scrolling, and that appears in the macro.
01:18Now it doesn't hurt anything.
01:19You don't see when a macro runs. But these are the kind of things that
01:22happen sometimes, and just to make the point here, we're going to get rid of this.
01:26We'll run the macro again perhaps later. It won't make any difference at all.
01:30It's still going to work just fine.
01:32What do we need to know about this?
01:34Well we worry about that later. But we might just want to make a small change
01:37for the sake of making a change.
01:39To emphasize the idea increasingly over the time, as you work with Visual Basic,
01:45as you create more and more macros, you'll get more familiar with the code and
01:49actually make some changes.
01:51Now it's a small matter to say, when this macro ends, the active cell goes to A1.
01:56We want it to go somewhere else.
01:58All I'm suggesting here is we're going to make a change, and simply by putting in
02:04the zero here, the next time we run this macro the active cell will go to cell
02:08A10. That's just to prove the point. We are not going to change anything else.
02:12It's a reminder that you have control over this environment, and the more that
02:16you eventually learn about VBA will have some impact.
02:20People who know VBA well are more likely to be typing some of these commands--
02:25not necessarily these, but certain commands and the actions.
02:28And a bit later we will see that there are certain structures within VBA that you'll type.
02:34In this particular workbook is another macro.
02:37It's located in what we call a module. Then over on the left side I am
02:40double-clicking here. And here is another small macro. It simply displays
02:44the values in millions.
02:46If, for example, though you want to alter this macro to also apply color to
02:52the cells when you apply this format, then you could easily re-record this.
02:57But another idea here, I'm going to go back to the other module in the left-hand
03:01side and highlight this.
03:04Now you don't know the details of what each line is doing here, but this is
03:08the portion of the other macro-- and it wouldn't take long to figure it out--
03:11that applies the color.
03:12So what we're going to do with this? We're going to copy this code, right-click >
03:17Copy, go back to the other module, and right here after applying the number
03:22format, right-click > Paste. The next time we run this macro,
03:26it'll not only apply numeric format, but it'll also apply a color to the cell.
03:32We're opening the door to the idea that although recording will be the dominant
03:37method of creating macros,
03:39there will be times when you step into the Visual Basic code to make changes.
03:43There will be other times when you copy code from one macro to another.
03:47As we jump back into Excel here-- and we can do this from the menu at the top,
03:52File and close and return to Microsoft Excel--
03:55we could go right back in here.
03:57We could run those macros again.
03:59But we're introducing the idea that recording, although the dominant method, is
04:03not the only way that we create and alter macro code.
Collapse this transcript
2. Running a Macro (Execute, Play Back, Etc.)
Running macros from the View tab
00:00Once you have created a macro, no matter how, you need to run it at
00:04different times. And it just so happens sometimes you forget that there is a
00:08keystroke shortcut.
00:09You don't know if there is, or maybe you forgot it.
00:12It doesn't have a button associate with it.
00:13How do we track down our macros?
00:16You might later assign the keystroke shortcut. But let's imagine we want to
00:19run a macro that displays these values here. Maybe you're only thinking about the first quarter.
00:25We want these values displayed in millions.
00:28We've already written a macro to do that.
00:29The macro will display these values in millions with a single decimal,
00:33a lot easier to read.
00:35But we have forgotten the keystroke shortcut. What are we going to do?
00:38On the View tab, go to the Macros button. Choose View Macros.
00:44We don't care about seeing the code or making any changes. Don't worry about the
00:47keystroke shortcut now. We simply want to run this macro.
00:50Well, here it is values in millions.
00:53We could either double-click that or highlight it and then click Run.
00:58We've run the macro. That's our fallback method. And the other reason it's
01:01good to go to that location is it does remind you which macros are in this
01:07workbook, or possibly others. So viewing the macros, and even though I have only
01:12two here right now,
01:13nevertheless it will help you to figure out where the macros are, and we can
01:16also come here for other reasons, such as assigning keystroke shortcuts and
01:20actually jumping into the code as well.
01:22So it's a valuable tool for keeping track of macros, and when necessary actually
01:28running macros--although it's not the fastest way.
Collapse this transcript
Running macros from a keyboard shortcut
00:00The fastest way to activate or run a macro is by way of a keystroke shortcut.
00:05There are some problems though, associated with this idea.
00:08Many times you want to use a meaningful letter for your keystroke shortcut.
00:12So maybe you've got a macro like one you might have seen here that activates a
00:17macro that displays numbers in millions with a single decimal.
00:21Ctrl+Shift+M. There it is, not bad. But you surely can't count on every feature
00:28that you want to have that letter available to you.
00:31And so there is a tendency over the time to maybe drift away from using
00:35keystroke shortcuts.
00:36I often use them when I'm testing macros, and then later I don't bother with
00:40them. But it is a time-honored method.
00:42But you want to know how to change them or possibly get rid of them or
00:46maybe even add them if you forgot to assign a keystroke shortcut when the macro is recorded.
00:52On the Macros button, in the View tab, the choice View Macros allows us to view
01:00the code of the macros and also by clicking the Options tab, to either assign the
01:06keystroke shortcut, remove it, or change it to be something else.
01:11We don't see the list of which other shortcuts are currently in use, so
01:15sometimes we have to go back and forth looking at other macros to see what their
01:19key stroke shortcuts are.
01:21And there is also this concern: Am I using a keystroke shortcut
01:25that's already in use?
01:27If you use Ctrl and Shift when you create this, that's usually not a problem.
01:32But on sheet 1 is a list of the commonly used Ctrl key combinations.
01:37In Excel 2010, for example, Ctrl+C is copying, Ctrl+X is cut--those are widely
01:43known--Ctrl+V is paste, and so on.
01:46Now if you never Fill Right--I don't-- then you can use Ctrl+R for a macro if
01:52that makes sense to you.
01:54Ctrl+Q is wide open; it doesn't have any assignment.
01:56Use Ctrl+Q or use Ctrl+E. It's your call on what is you want to use, but do
02:01remember if you use one of these combinations here, you're overriding its other
02:07use, and that may not be what you want.
02:09That's why a lot of people automatically use the Shift key when they're
02:12making assignments.
02:14So once again, from the Macro button on the View tab, this is where you, by
02:20clicking the appropriate macro and then choosing Options, make those changes.
02:24But keystroke shortcuts are a time-honored way. It's fast, and it's easy.
02:27It's just a question,
02:29sometimes you forget this keystroke shortcut or that one.
Collapse this transcript
Running macros from the Quick Access toolbar button
00:00For macros that you run frequently, you might want to decide to have a new
00:05button in the Quick Access Toolbar at the top of your screen
00:09in addition to a keystroke shortcut, or instead of a keystroke shortcut. We also
00:13want to introduce the idea that if you plan to use a macro in many different
00:18workbooks, we store the macro in a different location.
00:21This not a course in using functions, but if we look in column B here,
00:25something interesting is going on.
00:27The code in column B uses the column A data, and it gets rid of this unnecessary
00:33spaces like we see in row 7 here, for example, and here, too.
00:37It also uses proper case to clean up the data.
00:40But when you work with formulas that clean up data--typically in an extra column
00:45like this--eventually what you want to do is to turn these into the pure values,
00:51in other words, the results.
00:52We would like to have this cell, the first one, simply consist of Baxter, Donna.
00:57No formulas, throw all that away. Keep the results.
01:00And many of you know how to do this multi-step process.
01:04We are doing something similar in column E, but over here a different function
01:08is being used. And what's happening here?
01:09It's simply turning all the letters into uppercase letters.
01:12Column I is doing something little more sophisticated, or actually
01:15manipulating the code, and column I is doing something even more involved
01:19manipulating the code.
01:21But in all three cases, what we would like to be able to do is to take these
01:25results and essentially convert them into their values.
01:29Let's record a macro that does this, and we want to store this in such a way
01:32that it's always going to be available, and then we will make it accessible by
01:36way of a new button in the Quick Access Toolbar.
01:39A lot of macros are based on the concept that you have already selected the data
01:45before you want the macro to do its work.
01:48So we are going to highlight these cells here to record the macro, and we are
01:52thinking, we could use this macro not only in this worksheet but in other
01:58worksheets in this workbook and in other workbooks as well.
02:02So let's say we've got this data highlighted.
02:04The steps we want to take, we've written them down, or we know what
02:08they're going to be.
02:09They are going to be off the Home tab.
02:11It's going to be a Copy and then a Paste Special values.
02:14So if we have either written that down, or we know that. We are ready to record a macro.
02:19Bottom of the screen, we click the button. No macros are currently recording.
02:23Let's record, and we will call this PasteValues.
02:32And this time instead of storing them in this workbook, we want to store them in
02:35personal macro workbook.
02:38Now this is a special workbook that at the moment might not even exist.
02:43The very first time that you record a macro and indicate that you want to
02:47store it in a personal macro workbook, you're setting up the steps to actually
02:51create this workbook.
02:53It's a workbook that from now on will always open every time you open Excel, and
02:58it will be the repository for macros that you want to have available no matter
03:03which workbooks are open.
03:05We could give it a keystroke shortcut, but we are more interested this time in
03:08showing that we want to use it by way of a button. So we could give it a
03:12keystroke shortcut, but I won't. And I could get to the description later.
03:17So we are about to click OK to record this. So what do we do?
03:21We are trying to turn these results into their values.
03:24We will use the Copy button and then Paste > Values. And then you can see what's
03:31happened in cell B1. That's now Baxter, Donna.
03:35A minor point here, we will press Escape to remove the marquee lights. The macro is done.
03:40We will click the button right here.
03:44We don't have a keystroke shortcut;
03:46we have to fall back on our other method of going into the View tab to run this. Let's try this.
03:51Let's highlight the data here.
03:53These are different formulas here, but we want to turn them into their values as well.
03:57We will use the View tab, the Macros button, View Macros. And where are we are looking?
04:04We are looking for macros in the PERSONAL macro workbook;
04:09there it is PasteValues. Let's run this.
04:12Obviously that's not the fastest way, but it proves that our macro was set up,
04:17and we are ready to use it.
04:18Now, let's add a button to the Quick Access Toolbar.
04:22The Quick Access Toolbar might appear above the Ribbon.
04:26You have the option of putting it below it. Perhaps you have done that. But in
04:29either case, we want to right-click on the Quick Access Toolbar and customize it.
04:35And in this dialog box called Excel Options, you will see Choose commands from >
04:42Popular commands.
04:43Click the drop arrow. We want to choose Macros, and here's the macro
04:50in question, the one called PasteValues.
04:53Let's add this to the Quick Access Toolbar.
04:56Now you might or might not like that icon there.
04:59If you don't, click Modify, and you've got 181 choices out here. And I doubt if
05:06any particular one here suggests for what we are doing, so it's just a question
05:09of what you like and something looks a little different from other things.
05:13So I will just pick one at random here, maybe this one.
05:17Some people might be looking for a number if they're thinking of values meaning numbers.
05:22But in this case, the values don't necessarily mean numbers; they could mean
05:25text. So pick whatever you want.
05:26I will pick this one. Click OK.
05:30More important than the icon is the fact that if you gave it a sensible name
05:34like PasteValues, this is the name you'll see in the pop-up when you actually
05:38see the button in the Quick Access Toolbar.
05:41So I will click OK and this column I data out here, this rather long set of
05:47formulas here, we want to turn those formulas into their results, we want to do
05:52that PasteValues, we want to run that macro.
05:55We now have this button in the Quick Access Toolbar up there, PasteValues.
06:00We will click it, and what do we do?
06:02We turn these values into the results.
06:06Every time we need to do this--and maybe we need to do it a lot--we've got this
06:10button up here in the Quick Access Toolbar.
06:13Now maybe it's not quite as fast as the keystroke shortcut, but it's here in
06:17definitely. And every time we slide across, it will remind us to what it does.
06:23So it's a valuable way to run a macro. And for those kinds of macros that
06:27you're likely to run frequently, assign-- as we did here--a button to it in that
06:33Quick Access Toolbar.
Collapse this transcript
Running macros from an object or clip art
00:00If a macro is designed to be used by others, or is designed to work only in
00:05specific worksheets, you might want to consider activating them by clicking a button.
00:10And that's the case in this workbook right here called Phone List.
00:14When we put the mouse right on Sort by Name, you see the pointer there. It's going
00:18to actually sort these by last name, too.
00:20Notice it does that. At another time you might want to sort this list by department.
00:25The macros associated with the Sorting buttons are actually in this workbook,
00:31and we can create buttons.
00:35We can use clip art and make them be the starting point for macros.
00:39Let's assume that people using this don't know how to sort data in Excel.
00:44But this makes it simple and easy for them.
00:46So how do we set this up?
00:47What I'm going to do is hold down the Ctrl key and simply click one of these,
00:51for example Sort by Name, and then delete it, and show how we can create a button like this.
00:56So I want a button right here, just select the one we saw.
01:00Quick look here on the View tab first though to remind us--because I did say it
01:04and didn't show it to you--
01:05the View tab, Macros button by Viewing Macros, we can see the macros in this
01:12particular workbook.
01:14There is a macro called SortByDepartment. There is macro called SortByName. They are in this workbook.
01:21We would like a button here in column A. Where do we go?
01:25Insert tab > Shapes. Use any shape you want. Sometimes you'll see these maybe done
01:31whimsically with a smiley face. There is a heart shape.
01:35There all called kinds of buttons, explosion-type buttons, 32- 16-point
01:40stars, all kinds of stuff.
01:41I'll be a little conservative here and just use rounded rectangle.
01:46Draw it. If you want to look so-so, hold down the Alt key, drag these edges.
01:52They line up perfectly with the cell boundaries. Not critical, but some
01:55people love that idea.
01:56Hold down Alt to do that.
01:58We can typewrite on it from the beginning "Sort by Name."
02:04And let's get the cosmetics out of the way, just click the corner, maybe go back to
02:07the Home tab, make it Bold, change the font if you wish, use centering, top-down,
02:12left-right, whatever.
02:14Redesign that, change the color of it, and so on, and so on, and so on.
02:17The key step of course is to right- click on this and do this near the edge,
02:23and you'll see a choice called Assign Macro. You're assigning a macro to this
02:28button. And which one?
02:31Here are the available macros, the ones in this workbook only; we could narrow it that way.
02:37We want SortByName; that's the macro that will sort these by last name.
02:42Click OK, and we've made the association.
02:44If we're done with all the cosmetics and everything, click outside of it.
02:48We'll click this button. It will run our macro to sort this by last name.
02:53Now you can do this with clip art, too. Just a quick example here. I'm going to go
02:57to the Insert tab > Clip Art. Recently I used something like this. I simply
03:02typed in "car". Click Go.
03:05I'd be hard-pressed to give you good strong reason for using this here, but
03:10nevertheless any object--whether it's a shape like what we saw or one of these--
03:14could be a vehicle for running a macro.
03:17And here too, right-click > Assign Macro, and there it is. Maybe it's the
03:24SortByDepartment macro. Click OK.
03:27Now until we click away from it-- maybe I want to make that smaller. Okay.
03:32Now again, a little bit frivolous here, I can't give you good reason for using
03:34that. But point here--and there is no pop-up that tells us what it's going to do--
03:38but this will sort by department. And you saw how we did this with the objects.
03:43So here and there, particularly in workbooks where you have got macros designed
03:47only to manipulate data within that workbook, not a bad idea sometimes to use
03:51either a button or clip art to be the starting point for running the macros.
Collapse this transcript
3. Using Visual Basic for Applications (VBA)
Introducing the VBA environment
00:00Every time you record a macro, you create a small program using VBA, Visual Basic
00:07for Applications, the programming language of macros.
00:11And the big question in many of your minds would be, what is VBA,
00:15and what do you really need to know about this programming language?
00:20In this particular workbook called Code--notice it's file extension, xlsm--
00:26there are macros here. On the View tab in the Ribbon, rightmost button, Macros >
00:33View Macros, there are a few macros in this workbook.
00:38We might want to see what the macro code is about.
00:42I recommend that anytime you record a macro that you quickly look at the code.
00:46It's not that you need to know a whole lot about this environment, but you'll
00:51learn more and more about macros as you view the VBA code.
00:56And certainly one method is to select a macro in question--like this particular
01:00one here, Highlight_Value_Cells--and in this dialog box, click Edit. There we are.
01:08This is a macro created in a prior movie that highlights just the value cells
01:14within a given worksheet.
01:15And the big concern of course is what do we need to know about what we were saying here?
01:20And what is this environment all about?
01:22We're on a different screen completely.
01:24When this macro was recorded, the initial stepped involved clicking the menu and
01:30choosing--by clicking in four different places--a command process, and all that
01:36translates into this.
01:38When you view macro code, you're viewing an environment that begins with the word "Sub."
01:44It's sort of out of the past; it means subroutine. It ends with the phrase "End Sub."
01:49These are automatically generated.
01:51When you provide the name of the macro, that's what provided in the very first line.
01:55If you provide a keystroke shortcut at the time of creation, you'll see
01:59that here. Don't expect to be changing this and have any real effect;
02:04this doesn't change anything.
02:06Don't even think of changing that there; that's not how we change the
02:10keystroke shortcut.
02:11These lines here all begin with a single quote. Those are considered comments.
02:17If we remove them, the macros runs exactly the same.
02:20This has nothing to do with the operation of the macro, but it is
02:24good documentation.
02:26Many times I will remove this one. It's simply repeated at the top line.
02:30So it doesn't hurt to get rid of it, and you can casually do that or keep it,
02:33doesn't make any difference. Press delete.
02:36You can introduce empty lines if you think it makes the reading of this clearer to you.
02:41I am going to click right here and press Enter.
02:43I might click down here and press Enter.
02:46Totally unnecessary, but on the other hand, if it somehow clarifies what I think
02:50this macro is doing, fine.
02:52And at different times we'll see code like this, and of course, aren't you glad
02:56you never had the type something like that.
02:58And you can more or less figure out what's going on in here, although again, the
03:02focus is not so much on mastering the code but getting familiar with it and not
03:07getting intimidated by it.
03:09And anytime we want to leave this environment, we can press Alt+F11, as I'm doing here.
03:15In fact, you can use Alt+F11 as a toggle. In any workbook that has macros, say
03:22this one, we can press Alt+F11 to take us into the world of Visual Basic,
03:27do what we want to do here, or maybe just look around, press Alt+F11 and we're
03:31automatically back into Excel.
03:34So we're getting you used to the idea that every macro automatically generates a
03:39small program in Visual Basic.
03:41You want to check it out, get little familiar with it, as we learn more about this environment.
03:45This is not a course in VBA, and yet you cannot ignore that feature of the software.
03:50It's a programming environment easily accessible from any workbook.
Collapse this transcript
Viewing the VBA components
00:00There are additional aspects of the VBA environment that you should
00:04become familiar with.
00:05Once again, a reminder: the quick way to enter the Visual Basic window is to
00:10press Alt+F11, and this is also the keystroke shortcut for returning from
00:15Visual Basic, Alt+F11.
00:18When you come into the world of Visual Basic here, the screen isn't always
00:22looking the way you want it to. And things can go wrong here and there; it can
00:26be a little confusing.
00:28I'm not suggesting that the screen as we see it now is the way it will
00:31always appear to you.
00:33But many, many times--and you'll see this depicted in books on macros as well--
00:37what you're likely to see on the left side of the screen--and that portion can
00:41be made wider or narrower,
00:43there's no strong reason for doing this right now--
00:46but the upper-left corner typically is occupied by a section where you'll see the word "Project."
00:52I'm going to purposely get rid of this because there will be times when you
00:55jump into this environment, particularly if it's the first time you're using
00:59macros on a particular PC,
01:01you might not see the portion of the screen that I just purposely got rid of.
01:06On the View Tab, you'll see the term Project Explorer.
01:10It sounds like National Geographic. Click this.
01:14This is certainly one of the major components and one of the most
01:16important parts of the Visual Basic environment because it gets us
01:21oriented with our macros.
01:23And what we'll see on the left-hand side-- and these images might be collapsed;
01:27now I am doing this on purpose--
01:29we will see the names of all open files, and, if it has been created, the personal
01:35macro workbook as well, looking like this, PERSONAL.XLSB.
01:40The pluses in front of them will expand this.
01:43If this particular workbook, called Code, has macros in it, when we click the plus
01:49to the left of it, it will expand and show us the sheet names, and we will also
01:55see under here, Modules.
01:57Now that minus in front of it might be collapsed.
01:59As soon as we see the word Modules, we know that at least potentially there are
02:03some macros here. Click the plus.
02:06And within Modules you might see one or two or three or more separate modules.
02:12Clicking on these does not reveal the actual macrocode, but if we double-click
02:18on one, to the right we see the macro that's in that module.
02:22As you open and close workbooks and create new macros, every time you create a
02:27new macro after opening, it will go into a new module.
02:31What you'll do from time to time is simply move these macros around like
02:36copying the code from one module into another, and perhaps even changing the names of these.
02:42One of these I renamed Utility.
02:44Think of these as file cabinets, and you can have one macro per cabinet or three or four.
02:50There's a tendency to store them together if you have more than a few, by name;
02:55otherwise you are trying to organize them somehow.
02:58If you have created a macro for the personal macro workbook, you will see it
03:02indicated out here with a plus, and you will see Modules.
03:05And there's Module 1.
03:06I am going to double-click it, and there is a macro that's in the personal macro
03:10workbook. More about this personal macro workbook later.
03:14But the VBA Project, the Project window as it's called here, is extremely
03:19important because it helps us jump to this particular macro or that macro by
03:24going to different modules.
03:25In the lower-left corner, less important but occasionally valuable is
03:29the Properties window.
03:30We will use that just briefly to change the name here.
03:34It's not critical, but you'll probably want to have it visible.
03:36But by far the most important part for people using macros is the Code window on the right.
03:43If that's not there--and occasionally it's not--you will want to go to the View
03:47menu and choose View > Code, and that will make it appear.
03:51But a common problem, in particular when you're getting started, is that if you
03:55press Alt+F11--which I do recommend as the fast way to get into the Visual Basic
03:59window--you are not always viewing the macro that you want to right away.
04:03And that means, again, we go into the Project Explorer and double-click the
04:06appropriate module to track down the macro that we're are looking for.
04:10So Project Explorer here helps us locate and manage different macros by
04:15providing a staging area for copying or moving code.
04:18This is also where we sometimes change our mind and say, "You know this macro
04:23here, for example, that's in our current workbook?
04:26This is going to be so useful. We should also put this in personal and maybe
04:31even not have it here."
04:33So we can move or copy this, and it's real simple, using standard techniques
04:38simply to highlight this.
04:40If we think that this macro could be used in different workbooks, why don't
04:43we simply copy this? Copy button there and go to Module 1 down here over on
04:49the left-hand side under PERSONAL. Module 1, and we will put right here
04:53below the other one.
04:55There is a Paste button or Ctrl+V. We will paste it there.
04:59Do I want to have it in both places? Probably not.
05:02We could've moved it. I can go back and get rid of it;
05:04it doesn't hurt to have it in both places.
05:07Anytime you create a macro and you store it in a location and maybe you say to
05:12yourself, it's the wrong place, or later change your mind, it's very easy to move
05:17or make a copy of a macro.
05:19You remember also this idea that when you are creating macros here and there,
05:24rather than recording an action, if macro code already exists, it's real easy to
05:29copy code from one macro and paste it into another.
05:32It's commonly done, and it helps you build macros faster.
05:37There's no question that getting familiar with this environment is helpful.
05:40It does certainly can be intimidating at first if you obsess too much
05:44about knowing the code.
05:46Another aspect of this--and it's a minor one--
05:49some people don't like the look of this because of the font, Courier.
05:52But the reason this is used is that the letters are all the same width.
05:56And although that's not providing a reason for using it,
05:59when you are comparing code on different lines when the letters are all the same
06:03size, it's easier to make comparisons.
06:06So that's what the look is here.
06:08You can display size changes here.
06:11I've made these larger than I would usually.
06:13Simply click the Tools menu up top, go to Options, and if you want that font
06:18size to be larger or smaller, jump into Editor Format. I've got size 14 right
06:23now. Choose the one you want.
06:25But I would recommend keeping Courier or Courier New out here as the
06:28particular typeface.
06:31So in wrapping up, Project Explorer helps us locate and manage different macros.
06:35It gives us the staging area for copying and moving code.
06:39The Code window contains the actual code and comments that actually get recorded and
06:44gives us insight into what the macro is all about. Press Alt+F11.
06:48We are right back into Excel.
Collapse this transcript
4. Recording a Macro in Stages
Recording a simple macro
00:00If you're relatively new with recording macros, it's quite likely that the first
00:04few macros are going to be short, quick, easy.
00:07It's better to understand the small processes first.
00:10One question that might occur to you at some point is, do I ever need to change these?
00:15We've already suggested the idea that you will from time to time want to do that.
00:19We want to expand this idea, and as we work with macros and go into VBA back
00:24and forth into the world of the VBA and back into Excel,
00:27we want to become familiar with the techniques for why we want to alter a macro
00:33and then how we do that.
00:34I am going to take a small process here, create a new macro for double
00:39accounting underlining, a feature that some people might find useful.
00:43Part of the reason there is I am using this is that it's more multi-step than we
00:46might want it to be if we do it manually.
00:48If I want double accounting here,
00:50I am going to have to go into the-- either the Home tab, or possibly make it
00:54as fast as possible,
00:55you know, right-click > Format Cells, click the Font button, drop arrow here
01:00is Double Accounting.
01:01That's the one we want.
01:03There we go. Click OK, and there it is.
01:05But why not make it faster?
01:07Let's record a macro to do this. I am going to press Ctrl+Z right now to undo that.
01:11Let's record a macro.
01:12This time we will also emphasize the idea that we want this macro to be
01:16available to any workbook, or potentially any workbook.
01:20So, as we highlight this we want to be thinking where we are going to be storing the macro.
01:24We will click the button in the lower left-hand corner for activating
01:28the recording process.
01:30Give this macro a name like DoubleAccountingUnderlining.
01:34Give it a keystroke shortcut, Ctrl+Shift+U. Store it in a personal macro
01:41workbook, the workbook that's always open every time we open Excel.
01:44Once it's been created, it's always open.
01:46We will do the Description later. Click OK.
01:49We are in recording mode now.
01:51We can also get into that Format Cells dialog box with Ctrl+Shift+F. We'll do
01:56it that way this time,
01:57Ctrl+Shift+F. There's the Font tab.
01:59Underline > Double Accounting. Okay, we are done. Stop recording.
02:06Let's look at the code.
02:07I always recommend looking at the code every time you record a macro, even if
02:11you are not going to make changes. It's good to start getting familiar with more Visual Basic.
02:15Alt+F11, this doesn't always take us where we want.
02:19Where do we want to go here?
02:20This is stored in the personal macro workbook.
02:23The downside, if any, of Alt+F11 is that it doesn't always take us to the exact
02:29code window that we want.
02:30On the left-hand side of the screen, we see Personal, plus in front of the Modules.
02:35Double-click Module 1.
02:36It's the only one there, and there is the code that just got recorded.
02:41If you look around a little bit,
02:42you get familiar with what's going on.
02:44Many of these settings are defaults.
02:47In a bit, we will show you how you can actually get rid of them just to tighten
02:50up the code. It's not necessary; it doesn't hurt to leave them in there either.
02:54Here's the key line, DoubleAccountingUnderline.
02:57You wouldn't know that, but certainly when you see it, it makes perfect sense.
03:01So this macro does what we want.
03:04Now, it's sounding a little contrived, but nevertheless emphasizing the idea that
03:08sometimes you want to make changes.
03:10Maybe when you do this, to make it stand out even more prominently because you
03:14are not going to use it maybe in all the locations,
03:16you will also want to apply a background color.
03:19You remember that there's another macro out there that you recorded a while ago
03:25that applies color to value cells.
03:27Let's see if we can find that. Alt+F11, we are back here.
03:31We have only one other workbook open.
03:33It's called WatchCode.
03:35It has some modules in it.
03:36Let's take a look at Module 1.
03:39That doesn't contain the macro we are talking about. How about Module 2? This is the one;
03:43it highlights cells on all worksheet that have values.
03:47After selecting those cells--that's done here. Once again, a line you would never
03:52guess what it is, but having recorded it, you begin that recognize that it has
03:57some kind of a code. It's making constants, it looks like.
04:00But all of this code here is simply about applying a color, a solid color, to
04:06the selected cells.
04:09Now we could, if we wanted to, record another macro that just applies the color
04:15and then take that code and put it into the other macro.
04:18But here the code is already, so what are we going to do with this?
04:22Let's just copy this code.
04:23Now remember, this is from a workbook called WatchCode.
04:26It's a macro that was recorded there, and this portion of the macro
04:31simply applies color.
04:32We are going to copy this with the Copy button and then jump back to the
04:37macro under PERSONAL.
04:39It's in Module 1.
04:40This is the one we just recorded.
04:42We want to alter this macro by saying that not only will it underline using
04:48DoubleAccountingUnderlining the selected data, but after doing that it's
04:52going to apply a color.
04:54So at the bottom here after End With, I just pressed Enter, click here, and now
04:58I'm going to press either Ctrl+V or the button in the toolbar up top, Paste.
05:05And there is the code.
05:06Now, we could click Backspace here;
05:08that's certainly not critical. But here's the code that got copied.
05:11That's the portion of the previous macro that applies the color. Here's the
05:15macro that we just recorded to apply DoubleAccountingUnderlining.
05:17Now, although not a heavy-duty use of the concept, it does remind us that there
05:24will be times when we need to change a macro, and certainly one way will be by
05:29copying code from another macro.
05:30So I am going to press Alt+F11 and try that revised macro right here.
05:37Control+Shift+U, and there it is, and you see what's happened.
05:41A minor use of it, but it's opening the door to the idea that we've got control
05:46over the Visual Basic environment, and when we record macros, there will be times
05:50when we want to make some changes.
Collapse this transcript
Adding functionality to a macro
00:00What happens when you realize that a macro that you've already written doesn't
00:04do everything you want it to do?
00:06Now if you're relatively new with macros, that's not a thought that
00:09immediately occurs to you.
00:10But the more you work with macros, the more you realize that sometimes you want
00:15to expand the functionality of a macro.
00:18In this particular workbook called AddCode, there is a macro available that
00:24applies Double Accounting Underline.
00:26It's been used over here.
00:26It's Ctrl+Shift+U. Probably wouldn't use it here, but just to demonstrate it,
00:30highlight these cells, Ctrl+Shift+U.
00:33Now say in that case, a few minutes later I come back to this, and I say well, I
00:36don't really need that there, and furthermore, let's get rid of this.
00:38Well, maybe it's too late.
00:40Can we do an undo here?
00:41We can't undo what a macro does.
00:43The Undo button isn't available for what we did in a macro.
00:47So what do we do manually here?
00:49We're going to have to go back into Format Cells.
00:51We could simply right-click and go to Format Cells here, and remove the Double
00:55Underlining that way. Not exactly fast.
00:57Underlining, drop arrow, None.
01:01What if we could record a macro that does what we just did manually?
01:04In other words, remove underlining.
01:06Then there should be a way to take that code and mix it with the code that
01:11already does the underlining.
01:13Now, let's first of all look at the macro code by pressing Alt+F11.
01:17We're in the world of Visual Basic.
01:19Once again, don't always count on seeing the macro you want.
01:22But in this case, perhaps we are.
01:24If you're not seeing this macro, you want to be sure to go on the left-hand side
01:29of the screen here into the AddCode workbook.
01:32Make sure the modules are present.
01:34This one happens to be in Module 4. There is the code.
01:39That's the key line that applies Double Accounting Underlining.
01:43So let's record a new macro--put it right here, too.
01:47Now when we record macros, we also have the ability to keep this Visual Basic
01:52Editor window on the screen.
01:54What we need to do here, and you might have to click the button in the
01:58upper right-hand corner--
01:59this is either restore or maximize--
02:01we want to take its bottom edge or any of its edges, drag it upward, make it
02:05roughly half screen.
02:07In the other half, on the Excel screen essentially, do the reverse.
02:12So in this case, restore down possibly and make these be roughly 50-50.
02:17Now if we record a macro into this current workbook, it's going to go into a new
02:23module, because we just opened the workbook. And nothing wrong with that,
02:28but what I want to do is essentially create that new module, so that when we
02:32record our macro we'll see the code in the top of the screen.
02:36So, in the Excel portion of the screen here, I'm going to quickly record a macro,
02:42make sure it's stored in this workbook for now--eventually we might change it--
02:46click OK, and essentially do nothing here. Close the macro.
02:51The new module has been created.
02:52In the upper portion of the screen here, left-hand side on the Project
02:56window, under AddCode, we now have a Module 5 that we'll throw away. That's what we just recorded.
03:04Just to orient the screen this way.
03:06Seems like kind of a runaround, but it's a good way to get oriented so
03:09that from now on when we record, we will see the code in the top portion of the screen.
03:14It helps us understand VBA better.
03:17All we're doing for the moment now is recording.
03:20We're about to record a macro that will remove these lines.
03:24Active cell is in place right there.
03:26We'll click the Record button in the lower left-hand corner.
03:29Since we are going to be throwing this away, we don't care about the name of it,
03:33nor the description, nor the shortcut key.
03:36Just make sure it goes in this workbook.
03:38Click OK, and on this cell, right-click > Format Cells, None.
03:46As soon as I click OK, watch the code appear in the top part of the window.
03:50A lot more code than you would have expected, but similar to the other macro,
03:55and there is the key line right there.
03:57Now all of these other lines in here, and nearly all of them, are default settings.
04:02We're not going to be needing them,
04:04so why don't we just get rid of them right now, like this.
04:10Now, the other macro that we need is going to look similar, the macro that's
04:14already there. It's in Module 4.
04:16As it turns out, this is the only line we really care about,
04:19let's say this portion over right here.
04:21We're going to copy this, Ctrl+C. Go back into Module 4--
04:26that's where the DoubleAccounting macro is--and right below this at the bottom,
04:30I'll put it right at the end for the moment.
04:33Press Enter, and then Ctrl+V, or paste.
04:37So, we're trying to pull everything together here into one macro.
04:41Just as this code was unnecessary in the other macro, and you saw how I got rid
04:45of it, I'll do the same thing here.
04:47In other words, these are default settings, and they're not the essence of
04:50what we're trying to do. So we'll get rid of those lines and these lines.
04:55One more simplification that isn't so obvious but relatively easy,
04:59this body of code here can be simplified into a single line by taking off the
05:03With structure, and get rid of this.
05:07Click up here, press Delete a few times.
05:09That line will apply Double Accounting Underlining.
05:13From the beginning, after recording this macro for the first time, we could've
05:17simplified it to do just this.
05:19Now we want to do this with the other macro code that we've just created and copied.
05:24Same idea here. And now we're ready to reconstruct these two macros,
05:29put them together, and insert some logic that cannot be recorded.
05:34Any number of occasions we've mentioned the idea:
05:37as much as possible, we want to record our actions, but we can't do
05:41everything using that process.
05:43Now we want to insert the If statement.
05:45Every programming language has some kind of If construction.
05:49It's central to the idea of programming.
05:52We want to cover all bases here by saying, in English, if the current selection
05:58has Double Accounting underlining on it, then let's apply no style, or turn off
06:04underlining. So after that first line up there, space and then the word Then.
06:09It's a good idea when you're typing code, use lowercase.
06:13When I press Enter here, you'll notice two things happen.
06:17The word If will become capitalized, and also the word Then.
06:20You'll also see a color change.
06:22You get used to that idea, as you write code, you'll see that visual switch.
06:26If you misspelled a word, it won't happen.
06:29The next thing we want to do, and again going back to English here for the moment,
06:33if the current underlining status has double underlining, then what do we want to do?
06:38We want to turn off underlining. Enter.
06:42But now we want to cover the other situation.
06:46In other words, sometimes the current selection will not be underlined, and so
06:51what do we want to do there?
06:52We do want to apply Accounting Underlining.
06:56So take this code right here, Ctrl+C to copy, paste it right here after Else,
07:03and we're almost done.
07:05One more statement, not so obvious, but we need to end this with the statement End If.
07:10You can type it this way, Enter.
07:13A minor point: we can indent with the Tab; it looks better.
07:16Our logic is in place. And in English, one more time:
07:20If the current underlining status is Double Accounting, then turn it off.
07:25If it's not Double Accounting, then make it be DoubleAccounting.
07:28Keystroke shortcut is still in effect.
07:30I'm going to press Alt+F11. And in the Excel portion of our screen here, some
07:36cells that have double underlining. What are we going to do?
07:38Ctrl+Shift+U. A few minutes later, we come back, we change our minds.
07:43We want to use Double Underlining here.
07:45What are we going to do?
07:46Ctrl+Shift+U. We want to use it over here,
07:49Ctrl+Shift+U. It's applying Double Underlining.
07:53We activate cells where it already exists,
07:55Ctrl+Shift+U. We're introducing the idea that you need to alter some macros.
08:00It's not always using an If statement.
08:02But the If construction here, common to all programming languages, is one way we
08:07alter our macros to make them do the things that we want them to do.
Collapse this transcript
5. Creating Non-Recordable VBA Code
Understanding absolute and relative references
00:00A key concept in macro recording, and one you're more likely to encounter when
00:05you're dealing with large amounts of data, is the concept of absolute versus
00:09relative references.
00:11On the View tab in the Ribbon, the rightmost Macro button has a drop arrow. Click it.
00:17You'll see a choice Use Relative References.
00:20The pop-up tip there, practically an essay, reminds us and says, "Use relative
00:25references so that macros are recorded with actions relative to the
00:30initial selected cell."
00:32Without context, I don't think that means a whole lot.
00:35But let's try and put this in context.
00:38What if we were to do something relatively simple?
00:40I'm looking at a sheet called Shortlist.
00:43It's simply a copy or a pared-down version of data gathered from the September or
00:48October sheets here, which were substantially longer.
00:50They are about 2,400 rows.
00:52What we'd like to be able to do here is something fairly easy,
00:56but we want to use this as an example,
00:58a jumping off point for talking about relative versus absolute.
01:03What if we wanted to highlight, in each of these examples here, the first three
01:07rows and make them bold and then make the next three italic,
01:12then do that again for the next three and the next three and over and over and over?
01:16A leap that you might not have made yet is the following idea:
01:19if we turn on the Macro Recorder, and make these bold and then make these
01:24italics, as we move to different portions of the data here, the code that will
01:30be recorded, for example, right now, will explicitly refer to cells B4 to B6.
01:36As we highlight these, it will explicitly refer to B7 to B9, and so on.
01:41So any hope or idea that we could somehow take our macro code and compact it is
01:47not going to work so well.
01:48Again, the pieces are not quite there yet.
01:51What if somehow we could say, after making these bold, move down three rows and
01:58then make these italicized,
02:00then move down three rows select those and make those bold?
02:04The idea of recording absolute addresses versus relative movement is a key idea.
02:09I want to start this off with just a simple example.
02:12The active cell here is in C7, and I want to record a macro that puts the active cell in B1.
02:19It's certainly not very heavy duty.
02:21As I do this, we'll take a look at the code and then contrast it with
02:25relative references.
02:27If you don't use the Macros button and choose Relative References, it is not in
02:31effect; Absolute References are.
02:34So the active cell is at C7.
02:36Let's record a macro using the button in the lower left-hand corner, and
02:41let's call it Absolute.
02:43No keystroke shortcut needed.
02:45We'll store it in this workbook. Click OK.
02:47All I'm doing is clicking on cell B1, and that's all we want to do.
02:52We will stop the macro, and as I press Alt+F11--and this workbook is
02:56called MailingLists,
02:59we have a Module here Module1--
03:01There is the macro.
03:02Not too exciting. It doesn't do very much.
03:04I'm simply going to remove this too, and emphasize the idea it's a single action.
03:10Although we probably never really use a macro like this, whenever we run this
03:13macro, no matter where the active cell is at the start, we run the macro,
03:18it's going to go B1, no matter what.
03:21Now, let's use split-screen techniques.
03:24For the Excel window, we can drag the top border.
03:27You may have to click the button here for restore or maximize.
03:30Let's make our Excel window be roughly half screen.
03:35Top or bottom doesn't make any difference.
03:37In the other half, you'll see Visual Basic, and possibly it's full screen.
03:41If it is, you may or may not have to click this button.
03:44Once you do that though, you can resize it as well.
03:47It's very helpful to see the code as we record it.
03:51Now in the Excel portion of the screen-- I'm going to give it slightly more dominance
03:55here so we can see everything--
03:57I'm going to put the active cell at C7 just the way it was before, and I'm going
04:02to do the exact same thing in terms of what we do during the recording.
04:06But prior to the recording I'm going to click the button associated with macros,
04:12the drop arrow, and choose Use Relative References.
04:16It's a little tricky, by the way, when you come back and look at this.
04:19Is it on or is it off?
04:20If it appears to have an orange background, it is active.
04:24So right now, the Use Relative References setting is on.
04:28We do this ahead of time.
04:30You can do that within a macro also, and it will make sense, as we'll see a bit later.
04:35So that feature is on.
04:37I'm going to create another macro, click the Record button in the lower-left corner.
04:41We'll call this one Relative. No keystroke shortcut needed, no
04:45Description needed.
04:46It's going to go right into this workbook. And as soon as we click OK, we'll see
04:49it appear above its name, all set. And I'm about to click on cell B1.
04:55It's pretty obvious we're seeing something quite a bit different up there.
04:59Let's stop the recording here, shift back to the top of the screen, and compare
05:04these two. And here too, I'll get rid of all the comments and the empty lines.
05:09It's pretty obvious those macros say something different.
05:12Yet in both cases, if I put the active cell at C7, exactly the same thing will happen.
05:19Well, using your own logic and trying to put things together, as we look at this, it
05:23starts to make a little bit of sense, maybe not everything.
05:25Let me suggest, first of all, rarely will you ever type this. You certainly might
05:30copy it from time to time and certainly record as well.
05:33How do we read this line?
05:35From wherever the ActiveCell is, move up six rows--
05:40if that were positive, of course it would mean move down--
05:43and move to the left one column.
05:46Anytime in Excel when you see a rows and columns used in the same function or
05:50in the same Command, rows always appear first: R,C. So, even though it's not so
05:56obvious at first, this portion of the code means from wherever the ActiveCell
06:02is, move up six rows and to the left one column. So that means
06:08in this case move up six rows here and then to the left one column into cell B1.
06:14Now this portion here looks a little unusual, and doesn't quite say--what does it mean?
06:20First of all, it does not mean go to cell A1.
06:22That's pretty obvious.
06:24It means, as we see it here, select a single cell.
06:29Now, even though you might not be typing this, at different times you will see this line.
06:34What if, just for purposes of demo here, I want the active cell to move up six
06:40rows, over one, and when it gets there, I want it to highlight these three cells?
06:46Now what I do, if I want to alter this manually,
06:50I'll put a period behind the A1 and put in A3. Don't have to capitalize, but looks better.
06:56Now this does not say go to cell A1 to A3.
07:02It doesn't mean highlight that, either.
07:03So in the worksheet, I'm going to click right here in cell C7. Don't have a
07:07keystroke shortcut for this, so what am I going to do?
07:09Go to the Macros button on the View tab, and there is Relative.
07:14I'm going to choose Run.
07:16Watch what happens.
07:18From the location C7--that was the ActiveCell--we moved up six rows.
07:25That would take us into C1, to the left one column right into B1. And when we get
07:30there, what are we doing?
07:31We are not highlighting A1 to A3;
07:34we are highlighting cells in the same layout style as if we were
07:38highlighting A1.A3.
07:40That's a mouthful, and it's a concept that's a little bit awkward to grasp at
07:44first, but this does not mean literally go to A1 through A3.
07:50When you're looking at macro code and you happen to see addresses here and
07:53there, you can be thrown off track by misinterpreting a line like this.
07:58Now just for purposes of demo--and again I couldn't give you a strong reason for
08:02doing this right now--
08:03what if we wanted to move the ActiveCell up six over two and then
08:08highlight these two cells?
08:11How would the code be changed?
08:12We need to go up six and then to the left two. There is the 2.
08:18Then if we want to highlight two cells left or right, if we're during this manually,
08:22we're going to put in A1.B1.
08:26Now in this case because it matches up exactly with the code, that's why
08:29you could be misled.
08:31So if I were to go here, for example, and run this macro again manually Relative > Run,
08:37what do we do?
08:39Here is where it could be misleading, because we'd literally did highlight A1.B1.
08:43But that throws off track because what we really have done here is we've
08:46moved up six rows, to the left two columns, and highlighted two cells in this layout style.
08:53What if we move up to B1 instead?
08:55So we'll go back to the previous setting where we moved left one column, and
09:00then we move to the right, and we put in A1.D1.
09:05How do we interpret this?
09:06This means from wherever the ActiveCell is, move up six rows, to the left one
09:11column, and when we get there, highlight cells in the same layout style as A1 to D1.
09:17In other words, highlight four cells.
09:19So if this is going to work properly, it's going to be highlighting these four cells.
09:23Now again, this is a little bit academic here, a little bit removed from what
09:27we really want to do.
09:28But it's getting us familiar with this concept of absolute versus relative.
09:32We still have the Relative recording on.
09:34That's what this macro does.
09:36So what do we do now?
09:37Macros button > View Macros > Relative > Run.
09:44We moved up six rows, one to the left,
09:46and when we got there, we highlighted four cells.
09:48Now if we want to move through this particular set of data here and highlight
09:55to make these three bold and these three italic and so on, and so on, and so on,
10:00if we use absolute recording, we're going to see the actual addresses every time we do this.
10:07So by using relative recording, we'll actually record the actually moving down three rows.
10:13This is absolutely indispensable, absolutely required in certain kinds of macros.
10:18So getting familiar with the concepts of absolute and relative recording are
10:22extremely important in some macros.
Collapse this transcript
Using For and For...Next statements
00:00The concept of repetition comes into play all the time when we talk about macros.
00:05It's the reason we actually have macros:
00:07we are trying to eliminate repetition. But there's another perspective on this
00:11that uses the idea of repetition to help us build certain macros.
00:15In this particular worksheet is a short list that's about 42 rows.
00:19It's based on data from the longer October and September lists, which go
00:23to about 2,400 rows.
00:25And it's often easier to work with smaller amounts of data.
00:27Now what we are going to do here is not very heavy-duty, but it's going to cause
00:31some concepts to emerge.
00:33Let's say that in this list
00:34we need to highlight the first three rows of each name set, make them bold, then
00:39the next three italic, and then back here again bold and then italic.
00:44If we turn on the macro recorder and go through these actions, through five
00:49or six sets of names perhaps, the code that we see in Visual Basic will be repetitious.
00:55But that's not going to happen automatically.
00:57We have to be sensitive to the idea of absolute versus relative references.
01:03If we have got absolute references turned on, and we highlight these three
01:07cells, the code in Visual Basic will explicitly say B1:B3. And if we then jump to
01:14here, it will explicitly say B4:B6.
01:20So we want to be sure, as we are highlighting different ranges here, that we are
01:24embodying the concept of moving down the data, not picking specific cells, but
01:29moving down the data. It's a relative reference.
01:32However, as we start the process, we want to make sure that we do explicitly go to cell B1.
01:39So before starting the macro-recording process, we want to make sure that our
01:43setting--it's on the arrow on the Macros button of the View tab--
01:47we want to make sure that relative references are turned off.
01:51If there's an orange border there, then they're on.
01:55By clicking this, we have turned that off, and just to check it again, click the
01:58drop arrow. Absolute references are in effect.
02:02So we are going to click anywhere here, and imagine the active cell could be anywhere.
02:06As we begin the process of clicking on B1, we want that to be an absolute
02:11reference, but from there on in--and we'll make this wish--
02:14we will make them be relative references.
02:16And it's going to be helpful to see the code being built as we create this macro.
02:21So we want to make our Excel screen be roughly half the screen, use that
02:25middle button in the upper-right there.
02:27If that maximizes it, you have it down manually.
02:31And then we want to jump into the Visual Basic editor.
02:34If you don't see this, press Alt+F11.
02:36If you do, you can just click there,
02:38and do essentially the reverse. If you have done it recently, I'll revert back to a previous look.
02:43So we want these two windows to roughly share half the screen each.
02:48Notice that there are no macros in this workbook called MailingList.
02:53When we are looking in the Visual Basic window, here is the name of the file.
02:57There are no modules here. But we're about to create a macro, and we'll see a module.
03:02So in the Excel portion of the screen, we are ready to record. Lower-left corner,
03:07we will click the button for macro.
03:09We don't need to name this or give it a keystroke shortcut.
03:12We want to store it in this workbook. Click OK.
03:16At the moment, we don't see anything happening in the top portion of our screen.
03:20Let's click the plus next to Modules, double-click Module1.
03:24That's the beginning of our macro.
03:27We want to jump to B1.
03:29That's an absolute reference, and that's exactly what we see in the macro.
03:33Now we want to switch to relative references.
03:35The arrow on the Micros button, choose Use Relative References.
03:41Now as we highlight B1 through B3, you see the code as it appears in the window
03:46up above. And of course that does not mean A1, A3;
03:50it means from the active cell, highlight three cells in the same vertical layout
03:56style as if you were highlighting A1, A3.
03:59Next step, let's make these cells bold.
04:02We can go back to the Home tab or simply press Ctrl+B. We have made them bold,
04:08and you see the code that has emerged there.
04:10We'd then want to highlight the next three. And to make this process a little more
04:15visible in the bottom half of our screen, let's simply collapse the Ribbon by
04:19double-clicking one of the tabs. How about the View tab? Just double-click.
04:23It's going to make this easier to see.
04:25We'll highlight the next three cells here and make these be italics, Ctrl+I,
04:32and then the next three, and now Ctrl+B. And as we do this, note how the code
04:37is at the top the screen.
04:39It's starting to look like it's repeating.
04:41They were not quite there yet, looks like the line we just put in, we saw
04:45earlier, but we need to do a few more of these.
04:47So in the bottom of the screen, I am going to highlight a few more, three more
04:50rows, and we want to make these italic.
04:53That's Ctrl+I. And then three more rows, Ctrl+B. Three more rows, Ctrl+I.
05:02That's probably enough.
05:03Let's stop recording. Click that button in the lower-left corner.
05:07Let's look at this code in a little more detail.
05:11In these kinds of situations usually the first line or two, each of them in this
05:15case happen to be unique.
05:17You can press Enter after that to separate these.
05:19And now as we start to look at the code-- and I could've recorded some more, but
05:22we see a repeating pattern.
05:24I am going to press Enter in front of this fifth line here, and that pattern
05:29gets repeated four or more times.
05:31We don't see three complete cycles.
05:33We begin to see what's happening here.
05:35If we kept recording, we would see the same lines over and over and over again.
05:40And the purpose here is not to record through all the data;
05:43we want to avoid all that.
05:44But by recording a few sets of data here, we've introduced the idea, at least
05:50potentially, of repetition.
05:51Let's get rid of this repetitious code
05:55and now introduce the idea that there are structures in the Visual Basic
06:00language that allow us to perform certain actions over and over and over again.
06:06One of those kinds of constructions is referred to as a for. That's F-O-R loop.
06:12And in front of this code, right before it, we want to type "for" followed by a
06:18variable name that you could make up,
06:20say counter. You can use different word if you wish.
06:24If you are learning VBA in a more formal way, you will learn more about
06:27variables and how they need to be declared, and there are other aspects of that
06:31that we don't need to get into here. It's okay to do this.
06:34For a counter =, now imagine in our list here that we've got one name for each state in DC.
06:44We know there are 51 of them, so we simply want to say, in a way we use this
06:49particular feature here, do this 51 times, and the syntax is for counter = 1 to 51--
06:58and of course in different situations, whatever that number might be.
07:01The concluding statement after these actions are taken is--
07:05and notice, by the way, when I clicked down there that the word For became
07:08capitalized, the word To became capitalized.
07:11It's a good idea to always type these in lowercase. Keywords will
07:15automatically become capitalized.
07:17Next, counter. So even though we are not saying this reads exactly the way we
07:22want it to in English, you get used to it pretty quickly.
07:25Do these actions here, or perform these actions, 51 times, over and over and over again.
07:31And that's the ideal command sequence to be using when we know that we have a
07:36specific set of names. This will work.
07:40In fact, we can jump right back into the worksheet here. And manually, let's say
07:46we take off Ctrl+B here. That will take off the bold. Ctrl+I takes off all of
07:52the italicized entries.
07:53Click it again. There we go.
07:54Put the active cell here and run this macro.
07:58How do we run this?
07:59We don't have a keystroke shortcut.
08:01We will go to View, click the drop arrow for Macros > View our Macros;
08:06it's the only one we have got:
08:07Run the macro. And it went way beyond where we need it because this macro ran 51
08:14times. The main point here is it certainly took care of this data.
08:18But you can see how this has a lot of use, a lot of applicability.
08:22It doesn't cover all situations, but it's one of those basic structures that
08:26you will encounter and need from time to time as you deal with large amounts of
08:30data in an Excel macro.
Collapse this transcript
Using Do...While and Do...Until statements
00:00In this workbook, there is a macro that is designed to work on column B and
00:05make the first three rows of an entry be bold, the next 3 italic, the next 3 bold, and so on.
00:11This macro is in place.
00:13Press Alt+F11 to take a look at it.
00:15We are in a workbook called DoLoops, and here is that macro.
00:19It's called BoldItalic, and it is familiar to you if you have seen the previous movie.
00:24This kind of macro code here using a for loop makes perfect sense if there are
00:3051 entries or fewer. It doesn't make difference if there are fewer; that's okay.
00:34Eventually maybe the list will have that many sets of names.
00:38However, what if we have a series of data, and the amount will vary, perhaps
00:43from month to month?
00:44May be we are envisioning a list that we copy to this worksheet at different
00:48times; it's not always going to be the same length.
00:51We need to consider some other structures to handle information even when we
00:56don't know how many rows of data are going to exist.
01:00And we are talking about some variations on a term called "do" loop.
01:05It's similar to a "for" loop as we see it here, but different in the sense that
01:10it has more flexibility.
01:12And as I jump back into the Excel environment with Alt+F11, the current macro is
01:17set up to run in this particular worksheet called Shortlist.
01:21For the moment, it has only 42 rows, and that's only seven sets of names, but
01:26eventually there will be 51 sets of names here.
01:29The October List here is substantially larger.
01:32It goes down 2,400 rows, and conceivably it's going to grow to, or shrink;
01:37we don't know necessarily.
01:39We want to cover all of our bases to take the existing macro and alter it so
01:43as that it handles data no matter how many entries there are.
01:46Now in this example, what we are doing is fairly innocuous. It's not that critical.
01:52But the focus here is on the concept and how we set up the syntax and alter the
01:56code in Visual Basic to make this process worked repeatedly.
02:01I don't need to tell you that an Excel worksheet has a million rows;
02:05we might have a lot of data here eventually.
02:08Now as is frequently convenient, it will be good to see the Code window and
02:13the worksheet together.
02:14Let's change the display in the upper right-hand corner, that middle button.
02:17Either restore down or maximize and drag the edges.
02:21And if you're not in the Visual Basic environment, press Alt+F11, do the
02:25same sort of thing here so that our screens are sharing roughly 50 percent of the space each.
02:30Now here is what we would like to do,
02:32recognizing that the "for" construction here works fine if we know exactly how
02:37many entries are going to be here.
02:38So we are going to introduce the concept of a "do" loop, and there are a number
02:42of different variations on this.
02:43We will show you two major ones.
02:46This construction actually ends with the word "loop."
02:48We will put that in first.
02:50Notice I didn't capitalize it. When I click elsewhere, it will
02:53become capitalized.
02:55And this construction starts off with the word "do."
02:58No noun follows it, however. For some people, it looks a little bit strange.
03:02By implication, we want to do everything from the next line until we see the word "loop."
03:08And one of the words that we can use here is "while."
03:14Now switching to English for the moment, how do we want to say this?
03:18We want to do these actions while there is data present, and we can't say it quite that way.
03:23But we can use the term ActiveCell.
03:27By the way, spell this as one word.
03:29When we eventually press Enter or click on a different line, the A and the C will
03:33become capitalized in there.
03:35So as we move through the data, we want to do these actions while the active
03:39cell has data it in.
03:42I can't quite say it that way, but we can say if it's not equal to blank. And not
03:46equal to, similar to what we use in regular Excel and functions, the less than
03:51arrow followed by the greater than arrow,
03:54this means not equal to.
03:55So we want to take these actions as long as the active cell is not equal to blank.
04:01And how we do that?
04:03Double quote, double quote.
04:04I just click somewhere, and you saw the capitalization take
04:07place automatically.
04:08This will work, but it's probably not the best way to say this. It's a little
04:11bit awkward to read.
04:13Why don't we change the Equal sign?
04:16Or can we just come along and change that?
04:17If we change it to equal, then the wording wouldn't make sense.
04:21But there's another word we could conceivably use here.
04:23I am not suggesting in any way you come along and just make up words, but the
04:27opposite of "while" in Visual Basic code here is "until."
04:32So how do we want to say this now?
04:34We want to take these actions until the ActiveCell = empty.
04:40And I think for many people that's a lot clearer.
04:43So this would be a better construction than the one we just saw.
04:46Do all of these actions until you see the word Loop.
04:49Keep doing these over and over and over again until the ActiveCell is empty.
04:54Now recognize, too, that in the code here, we are moving down the worksheet.
04:59This line right here means move from the active cell down three rows, and
05:05this code here does the same thing.
05:07So as we repeat the process, we are moving down the worksheet three rows at a
05:12time with those two statements.
05:14Now another variation on this, and perhaps clearer to many people--and certainly more
05:18readable--is to introduce a word you wouldn't necessarily know, but since we see
05:24it, you'll know what it means.
05:25I am going to type the phrase "isempty," left parenthesis and take off the
05:32remaining portion of the code to the right here and put in a right parenthesis.
05:36And as soon as I either press Enter or click on a different line, watch the upper- and
05:40lowercase change, and this will work.
05:43And for many people this is the clearest of the three variations here.
05:47And in English we are saying, perform these actions repeatedly until the
05:52active cell is empty.
05:54So let's try this on our Shortlist.
05:57For the moment, in the original data-- see it at the bottom of the screen--it looks
06:01as if we got bold italic there already,
06:03so why don't we manually click Column B, go to the Home tab town here, click B,
06:08click it again, click I. There we go.
06:11Nothing here is bold or italic, and we can run this macro.
06:15Active cell is anywhere because the macro automatically starts at B1.
06:20There is no keystroke shortcut, so we will use the View tab, rightmost button,
06:25Macros > View Macros.
06:28We are looking for BoldItalic. There it is.
06:30Let's run.
06:31Looks like it did the job.
06:33And notice that it did stop just below the data.
06:35Well, let's try this on October List.
06:38This is 2,400 rows not 42 rows, so active cell is anywhere.
06:45Notice that we are not changing the macro at all.
06:47We are not indicating which worksheet we are in. But I manually put the active
06:51cell on a different worksheet, and let's run this macro again.
06:54And of course, in a real-life situation, we would either have a keystroke
06:58shortcut associated with it, possibly a button right here in the worksheet.
07:02For the moment, we don't have that.
07:04We will click the Macros button > View Macros.
07:07Let's run this macro again.
07:09Double-click that or click Run.
07:11Watch it happen.
07:122,400 rows. It's done.
07:15There it is, and it stopped just below the data.
07:18And you might experiment with certain macros from time to time. And I've done
07:22this different times where I simply copied this data, doubled it, copied it
07:26all again, quadrupled it, just to see how fast it would work. And many, many times
07:31it's almost as fast when we have more data.
07:34And we will see a bit later also, there is a way to make macros run even faster.
07:39But the key idea here is that the "do" loop construction commonly used in
07:43certain kinds of macros is ideal when we don't necessarily know how many records
07:49or rows we are going to be dealing with.
07:51And particularly if the lists that we are using here are going to vary, this is
07:55exactly what we need to use.
Collapse this transcript
Creating interactive macros
00:00To give a macro even more flexibility, we can introduce the idea of an
00:05interactive macro: a macro that pauses and asks for input from a user.
00:11In this workbook called Interactive, you'll notice worksheets September, October,
00:15November. And you could easily imagine a different type of workbook where each
00:20tab is a different region.
00:22And maybe the macro action that needs to be performed, you might need to have
00:26that take place on one worksheet or another.
00:29And at different times when you run the macro, you want reaction from the user as
00:34to which worksheet is to be selected.
00:36So in this particular workbook, there is already a macro in place.
00:40I'm going to press Alt+F11. And there it is. It makes alternate cells bold
00:46and italic, but this could happen on any sheet.
00:49We want to add code here that provides input from the user.
00:53So it's going to be the very first line of the macro, so clicking in front of
00:56Range and hitting Enter.
00:58We want to introduce the Visual Basic statement that's actually going to be
01:03stored in a variable that we're going to make up, and let's just call it sheet
01:06name or worksheet name.
01:08And you can make it upper- or lowercase. You can even have a mix of upper-
01:12and lowercase. Equals.
01:14And now the Visual Basic statement inputbox, left parenthesis, and you'll see a prompt here.
01:21And you're likely to see more of these prompts the more you work with Visual
01:24Basic and use standard features.
01:27It reminds us what we're supposed to put in next.
01:30The first entry we'd want to put in here will be a prompt, and we do this within
01:34double quotes. So double quote, and the wording is strictly yours.
01:39And ideally once this to be as user- friendly as possible, so why don't we just
01:43type in "type sheet name," or "type worksheet name," or "type tab name," whatever
01:48is clearest to you.
01:50And if you want to put a colon behind it, that's fine, but you must terminate it
01:54with a double quote.
01:55So anything is fair game between the double quotes.
01:59Comma. And although there are lots of other options, the only one that's recommended
02:03now is to put a title on the dialog box that will appear at the top here.
02:07Now a title in the box will be something like "Worksheet Selector," and we end
02:13with the parentheses.
02:14Now the syntax here isn't obvious if you haven't worked with programming
02:18languages, but this action here will prompt the user,
02:22the user will type something, and whatever he is type will gets stored into this
02:26variable called WorksheetName.
02:28I'm going to click on the next line, and you see how spaces are introduced
02:32automatically .Visual Basic does that.
02:34We're not quite finished yet, because this variable is holding the actual typed
02:38information that the user will type in, and we then need to connect that with
02:43the statement that moves to that particular worksheet.
02:47And the next statement could easily have been copied from another macro, or maybe
02:52you saw it in book on Visual Basic.
02:55It reminds us that there are some analogies here with how we learn code.
02:59It's very similar to how we learn a language; a lot is by imitation, or having
03:03seen a previous example.
03:05So what I'm going to be typing here-- something you probably haven't seen
03:08before--Worksheets and then the name that we created, WorksheetName .activate,
03:17and that is a valid statement.
03:18Whatever name was typed in on the prompt is stored in worksheet name, and that's
03:22placed right here in this Visual Basic statement.
03:26Now when we test this, we're not necessarily sure it's going to work, so how
03:31might we say, in effect, ignore all this? Let's just do the first two lines.
03:35Let's run that part of the macro and see what happens.
03:38A couple of different ways to do this, one way is to introduce something called
03:42a breakpoint. We can just click right here, and that will stop as it comes to this line.
03:47By the way, you can click that again to get rid of it.
03:49Another way, a time-honored way, is simply to put a single quote in front of these
03:53lines, and that make some comments for now.
03:56Maybe this is a little bit of work here, but certainly not much.
03:59So by doing this, we are essentially saying these lines are no longer code;
04:03they are just comments.
04:05And that's handy at times, particularly with just a few lines where you say
04:09just don't use these right now.
04:11We're going to run this macro. It's only going to do two things. As we pres
04:14Alt+F11 to go back into Excel, click anywhere here. We're going to run this macro.
04:20No keystroke shortcut available yet. It hasn't been assigned. We'll use the Macros
04:23button on the View tab, View Macros, there is our only macro, let's Run. And there
04:30is the prompt: Type worksheet name.
04:32So what I'm going to be typing here is going to be either September, October, or
04:36November, or of course any other sheet that might be here.
04:39And then we will see if this actually causes the macro to jump to that
04:43particular worksheet.
04:44So I'm going to type October here. Upper- or lowercase isn't critical, but spelling
04:48certainly is, October. And I'll click OK.
04:52And it looks like it did that. It went exactly to October.
04:55Now the reason the cells are highlighted is for whatever reason the last time I
04:59happened to been on the sheet that's why it was highlighted.
05:02So don't make any special conclusions out of having seen that.
05:05Now if I run this again--I'm on the October sheet--just to test that portion of it,
05:10click the drop arrow for Macros > View Macros. There we go again, and this time
05:14I'll type in September and OK.
05:18That cell on the September sheet. So that portion of it is working, and as we
05:22press Alt+F11, that's just a simple way to test this.
05:26There are couple of other ways to do this, too.
05:27So take off these single quotes here. And we're ready to run this macro.
05:31The purpose here is simply to show the interactive portion of the macro and
05:36to emphasize the idea that you can prompt for input. And you can easily imagine
05:40taking the same kind of feature and based on logic inside of a worksheet, prompt
05:46for a different workbook to be opened or to be closed.
05:49So we're only beginning to touch the surface of this capability, but it certainly
05:53make sense in some macros to have an interactive component that prompts for user
05:58input, and that input is then used in the course of the macro.
Collapse this transcript
6. Macro Project: Converting a Mailing List into a Database List
Planning a macro
00:00How can you plan what you want a macro to do?
00:03How do you know all the alternatives?
00:05Here is a mailing list.
00:07It's for the state of Colorado, a group of realtors.
00:10It's about 2,400 rows.
00:12You belong to a consortium, and you're going to be getting lists like this from
00:16other states as well.
00:17We've already got an Arizona list and a California list planned.
00:21And although the data is valuable, in its current form it's not really the way
00:26you want it to look. You would like it to be in the database kind of form.
00:29And I'm actually going to zoom back a little bit here and make column C narrower,
00:34and on the right is how we'd like this list to look eventually.
00:37We like to convert it into a list like this.
00:41We can sort it, we can do filtering on it. It's much easier to work with for most
00:45Excel users than the list that we see in columns A and B. This is probably great
00:51for mailing labels, that sort of thing, or at least portion of it is. But the big
00:55issue here is how do you get this data into this shape?
00:59And there are probably five or six major ways to tackle this.
01:02One way is certainly could need some kind of a filtering, or maybe we filter and
01:06just share the names and then copy them over there.
01:09We do have to be thinking about what happens with hidden data as we do that, but
01:13that's certainly a viable approach.
01:15Another one could be--as I delete this data to the right, right-click and Delete.
01:23Another approach could be what if we could put Sheldon's name here over in
01:27column D, and Connie's name right below it right there, and Patricia right here,
01:33and so on, and do the same thing with the companies in column A? When you start
01:38to think about doing this manually, you get weary even thinking about it.
01:42One thing that certainly could help is a widely used feature, and some of you may
01:46know of it: you can simply take this data, the information about one particular
01:51realtor, you could right-click and copy it and then over here right-click and do
01:56a Paste Special transpose.
01:59You can do it this way. There are some other ways to do it, too. Transpose.
02:03Flip the data sideways.
02:05I guess you could say it makes this six times faster.
02:07Then we get handle this information and paste it over here.
02:12Again, that may not sound like the best way to do this. And so the whole idea of
02:16"how do you tackle data?" there are times when there is no simple answer.
02:21But let's say that we have decided to take this latter approach.
02:24Now, you might not think at first that it's the best, and I can hear rational
02:27arguments for that. But we want to get used to the idea that the recording
02:32process gives us tools sometimes for scoping out what we want to do.
02:37If we think of only the first three or four sets of names here--and we've done
02:41this in a prior example--if we simply were to turn on the macro recorder and do
02:47what I just did here for one set of names and do it for the next set and the next set,
02:52what we're going to learn in the process are techniques for using macros and
02:58making them more efficient in ways that we could handle a list like this no
03:02matter how many rows it is.
03:04Our Colorado list is a few hundred rows, but our Arizona list is going to be
03:09bigger, and of course our California list is likely to be huge.
03:12And we eventually anticipate getting these lists from other states as well.
03:17Maybe four or five times a year we want to update this.
03:20So we want to have a quick method that's going to work with different volumes of
03:24data, and we want it to be foolproof.
03:26We eventually would want a title on here, too. We'll get to that as well. But in
03:30the process what we're going to show you here are those macro techniques that
03:35allow us to handle massive amounts of data.
03:38Again, no easy answers upfront, and no simple answer of the question "how do you
03:43scope out a macro?" But you think of alternatives, and you consider some of the
03:48problems you're going to encounter along the way.
Collapse this transcript
Recording partial code
00:00It seems that many times when you're trying to record a macro, you want to get it done fast.
00:05So the question comes up, should I be recording all the keystrokes and all the
00:09actions that I need at once, or should we do this in pieces?
00:13Sounds kind of disjointed to do this in pieces. But we want to show you the
00:17value of actually recording portions of what you need to do, saving that
00:22portion, testing it out a little bit, and then recording more.
00:26If we've decided that we want to copy and transpose these records one by one--at
00:31least for a few of them to get our feet wet--eventually, what we will need to do
00:34is somehow pull that data together, put in titles, and do some other things.
00:39So let's possibly think of this as a series of steps, rather than trying to
00:43accomplish this all at once.
00:45As we do this too, we need to think out some other issues, too.
00:49There's no question that when you're handling large amounts of information, as
00:53you move through the data,
00:55you will always have to be thinking about this absolute versus relative setting.
01:00Remember, if I highlight this data, and the references are absolute, the
01:05explicit code refers to the cells B1 to B6.
01:09And so as we record macros--and we can do this within the macro and sometimes
01:14even before the macro begins--we will be going to the View tab and on the drop
01:20arrow for the Macros button that choice Use Relative Reference.
01:24Again, remember as you look at this, if the orange banner is there, the Relative
01:30Reference option is set, and we turn it off by clicking. And now if we look at
01:35it again, it doesn't have the orange banner.
01:38I think a lot of us agree that this could be done a bit better here.
01:41It's just not obvious until you get used to it.
01:43But we do have to think out the issue in the following way:
01:46If we were to highlight this data, copy and transpose it and put it into cell
01:51D1, if we're recording in relative mode, this means that we have moved two
01:58columns to the right.
02:00If we then come down and pick up the next data, we've moved down six rows
02:05and over two columns.
02:07If we paste this in row 2, it means we are moving up five rows and over here two columns.
02:15It would make sense, and that seems like the right thing to do.
02:18One of our goals though is to say, if we have to move through a large amount
02:23of data, every action what we take as we are testing this, we want it to be recorded.
02:29If we just moved up five rows, and now we have got to move down to row 13,
02:33that's moving down 11 rows.
02:36And so the code that we will see will show a movement of 11 rows.
02:41And so the idea of saying, let's put this record over there, and let's put this
02:45record there doesn't sound so good now.
02:48It might not seem so good if we copy this just rightward.
02:52In other words, what if we put the information for Sheldon right there and then
02:56take Connie's information right here and put it there? What's the advantage?
03:01We are moving over two columns to paste the data.
03:04We did the same thing up there.
03:06When we finish the pasting here, we came down six rows to here.
03:11If we paste Connie's information here, and now go down to Patricia Willett, we
03:16again go down six rows.
03:18And so, though not intuitively obvious, the idea of pasting the data to the right
03:23instead of going up below the previous pasting starts to make some sense.
03:27These are the kinds of things you think about, and when we are recording in small
03:31chunks they just emerge all the more obviously.
03:35Let's start the process.
03:36We will turn on the macro recorder and copy just a few records here.
03:42We want to get our feet wet, so to speak, take a look at this code, and look at
03:46some of the implications of it.
03:48And it's always going to work better if we actually see this with the code being
03:53created at the same time.
03:55In the upper right-hand corner, use the Restore button.
03:58We want to make our Excel window roughly half-screen.
04:01Now you can put it on the bottom or top.
04:03You need to shrink it a little bit. And if you don't see the Visual Basic window
04:07on the background, press Alt+F11. We want to be there.
04:11And the current workbook that we are working with, let me jump back for a
04:14second, remind you, it's called Partial.xlsm.
04:18We do see it in the list here in Visual Basic.
04:21If you're not seeing the Code window-- and you might or might not be--then you
04:25went to View > Code from the window. Now there is nothing there yet.
04:30But let's do make our Visual Basic window roughly half-screen.
04:34Again, click that Restore button in the upper right-hand corner, move
04:37this around a little bit perhaps, and let's start the recording process.
04:42In the Excel window, I am going to zoom back a little bit, so we can see the
04:45data a little bit better.
04:48And the active cell could be anywhere as we start it.
04:51We want to always think out what happens at the beginning of a macro.
04:54We want to make sure that no matter what we do with this data, we start in cell B1.
04:59We can either make the setting right now--we can make sure that's
05:03absolute, already is anyway.
05:04We can do that in the course of running the macro because eventually we do want
05:09it to be relative, right after we go to B1.
05:12So as we start the process, we can start recording a macro. Lower-left corner,
05:18the button next to Ready, if that button is not there, right-click in your
05:22status bar, click Macro Recording, so that it has a check in front of it.
05:26You will only need to do that once.
05:28Let's click this button here.
05:31We want to create a macro, and why not give this a sensible name, CreateDatabase,
05:36whatever it is you want to call it.
05:38No spaces in the name.
05:40You can use the underscore character to separate words.
05:43You can shift upper- and lowercase.
05:45Macros never can begin with the number, as well.
05:48Let's give this a keystroke shortcut.
05:50Once again, one of 52 choices: any of the lower case letters, any of the upper
05:54case letters. And you probably don't want to be using those lowercase letters
05:58for commonly used features, like C for copy, X for cut, V for paste, and so on.
06:04So I'm going to use Shift+D here, and we want to store that in this workbook.
06:10We will get to the description later, which I don't mean to discount, but
06:13it's the kind of thing we often do after our macros are working we provide
06:17some documentation.
06:19As we click OK, we have just created--and you will see the upper window here--a module here.
06:26There it is right there under the VBAProject for Partial.
06:29That's the name of our workbook. Click the plus, here's Module, and we started the process.
06:35I'm going to click on B1, but I want make sure the arrow on the Macros button,
06:41Relative References is not on, therefore Absolute References.
06:44We'll click on cell B1, and we see that occur in the macro code up above.
06:50And now, well let's say from now on--at least in this part of the macro--we want
06:54to make sure that our references are relative.
06:57If I highlight this cell and the ones below it, the code will appear as B1:B6.
07:03We don't want to see that.
07:05We want the idea of highlighting six cells downward to appear.
07:09So on the Macros button in the View tab, we choose Relative References.
07:13That itself doesn't generate any code, as you can see.
07:16We will now highlight these six cells and the indication up above in the code
07:22means from wherever the active cell was-- it was in B1--highlight in the same
07:27layout style as if you were highlighting A1 to A6, in other words six vertical cells.
07:33We'll do a right-click and copy, and you can see that code of course.
07:38And now we are going to right-click in D1 and Paste Special > Transpose.
07:44There's couple of different ways. There we go. And you see the code that's generated there.
07:51A bit later we will show you how you can clean this up, although there is
07:54certainly nothing wrong with it.
07:56Here's the key phrase in the code.
07:58Once again, we bring out the idea.
08:00Someone who knows VBA really well could possibly type this and type it in a
08:04much shorter way, but on the other hand, we learn about VBA a lot just by
08:09watching the code being built.
08:11The line above it means, from wherever the active cell was--and it was in B1--
08:17move two columns to the right and select a single cell.
08:21That represents the actual action of right-clicking on cell D1, and here's
08:25the paste transpose.
08:27In order to see the worksheet a bit better, I'm going to double-click the word View.
08:32This simply is a way to collapse the Ribbon, down the worksheet, so I
08:37will double-click there.
08:38And then let's highlight the next six records.
08:41And by doing this, again we look at the code, what does this mean?
08:44From wherever the active cell was-- and it was in D1--we are going to move down
08:49six rows to the left two columns and pick up six more cells.
08:53We are about to see the code repeating, as we right-click in the data and copy.
09:00And then we're going to jump into D7, right-click > Paste Special and Transpose,
09:05OK, and then we'll come down and grab the next six rows.
09:09Now, sometimes when you do this, you will introduce scrolling into the code of
09:15the macro, and you can later take that out.
09:17Now that didn't happen yet, but it could.
09:18So one more right-click here and copy, or Ctrl+C if you prefer, right-click in
09:24D13, Paste Special, and Transpose.
09:27I am going to do now what I just suggested that you might do occasionally.
09:33In highlighting data--and I am in here on the worksheet--I am going to
09:37scrolling downward a bit to see things better, and you will see that that
09:40generates code in the macro.
09:43Now I am doing that on purpose to show that will happen from time to time.
09:46Later we will get rid of that.
09:47So I am going to now highlight this data here.
09:50This is the last time around on these. Right-click, copy, and then out here
09:56right-click Paste Special > Transpose. Stop recording.
10:03Now, obviously we haven't moved all the way through the data, and of course the
10:06macro is not finished. But we have the starting point for recognizing what a
10:11macro does and how to start to make some sense out of it.
10:14And if we've done this using relative mode as we have, you see the repetition
10:18that occurs here, and we can then enter the stage where we will compact this,
10:23pull out only the code that we need, and then introduce looping structures to
10:28help us get through this data quickly.
Collapse this transcript
Using loop structures in context
00:00In this workbook called Partial-2, a macro has been recorded.
00:05It's not complete, and you can probably see what's happening here.
00:08The macro has copied the first three sets of names into column D. We want to
00:14pick up and make some changes with it.
00:16But we're not finished with the macro, of course.
00:18What we really want to do now is introduce the concept of looping and decide
00:23which of the various structures that we might use here.
00:26In VBA, we hear the term "structures."
00:30Three well-known structures are a for loop, a do while loop, a do until loop.
00:37What they all have in common is that they allow us to process code repeatedly as
00:42we move through large amounts of data.
00:45Pressing Alt+F11. We're in the world of Visual Basic.
00:49We're looking at this macro here.
00:51The workbook is Partial-2. We're in Module1 here.
00:55By recording a part of this, we see some lines that we can get rid of. But the
00:59key idea here is, after selecting B1, for whatever reason sometimes we do this
01:04twice, doesn't hurt--
01:06we'd certainly get rid of that right away--
01:08starting here with Selection.Copy, I'm going to press Enter.
01:11You can do this at any time in a macro to offset, to give a visual impact and separation.
01:17We copy the record.
01:18We move two cells to the right to paste it.
01:20We did paste it with transposing.
01:22We moved down six rows.
01:25Eventually, here is another copy. It looks like we're doing the same thing again.
01:29Come down here, and although three complete cycles in this example didn't quite happen,
01:33that's okay.
01:34We're seeing repetition here.
01:36Now here and there what might throw you a little bit is if you did scrolling to
01:41see different parts of the worksheet, you will see that code.
01:45But if it had been full screen, you wouldn't have scrolled.
01:48This ultimately has no impact whatsoever on the macro recording.
01:53You can actually delete it.
01:54We got copy and copy.
01:57Over to the right two cells, and so on, back and forth.
01:59We see what's happening; the code is repeating. And if there still were some doubt,
02:03maybe you go back and do this, and go through three or four sets.
02:06But let's say that it's working properly,
02:08we started the third cycle there, and now we enter that stage where we say we
02:13obviously don't want to do this for all 2,400 rows.
02:17We've done it for a few.
02:18It looks like things are happening the way we want them to.
02:21But we don't want this repetitious code.
02:24Let's get rid of it.
02:26Because the code here that gets repeated does involve moving down the list--
02:32here is the key one here,
02:33this means move down six rows--because this takes us through the data, this is
02:38what we want to have repeated throughout the entire list--no matter how long it is.
02:44The for loop certainly has some possibilities.
02:46A quick reminder of what the for loop does.
02:50We can make up a word here to keep track of things.
02:53Counter is a frequent example.
02:55The advantage of for is when we have a distinct knowledge of how many entries
02:59we have in the list.
03:01If we know, for example, that there are always 500 names here,
03:04we'll say for counter = 1 To 500.
03:10For certain kinds of list, that's just perfect.
03:12We put the word counter down here following, after the word next, Next counter,
03:17and that will work just fine.
03:19Another point here, too:
03:20when you're typing entries like this, keywords like Next will become capitalized
03:25when you either press Enter or click on a different line.
03:28It's a good habit to get into.
03:29Just type these in lowercase.
03:31You get used to the visual impact as you click away; the keywords are counted.
03:36That doesn't happen if you misspell them. So if I type nexxt and click up here,
03:43I'm reminded I didn't get that visual reaction there.
03:46This is perfect for some situations, but not this one.
03:49We want this to be flexible.
03:50We don't know how many names are going to have here, how many cycles we need to go through.
03:54So it's better to use a do loop.
03:57This ends on the word loop, and three major variations here.
04:03They all will do the same thing.
04:04We start with the word "do."
04:06There is no noun that follows it.
04:08It's implied everything from here to loop is what we want to do, and certainly
04:12one option is to use while.
04:15It has its merits, and sometimes it's the better choice.
04:18Do while this ActiveCell, the one that we always have in our Excel screens, is
04:24not empty, and how can we say that?
04:27Not equal to, or the less than arrow followed by greater than arrow, Empty or
04:31blank, not quite so perhaps, double quote, double quote.
04:36I think this is the least preferred of the three options we're going to show you.
04:40Once again, you notice when I clicked elsewhere, the capitalization of the keywords.
04:45This is one way to do it, and by the way, this will work.
04:47I'm not saying it won't.
04:49But it's not the clearest, I think, for most people.
04:51Instead of while, until works better here, because then we can eliminate the use
04:57of the not equal to, and negative logic tends to be little bit trickier.
05:01Instead of saying do while the ActiveCell is not empty, why don't we say do
05:07until the ActiveCell = blank that way?
05:12For many people, I think that would be the preferred of those two choices that
05:15we saw. And perhaps even better, but not so obvious,
05:19it reminds me if you're learning a foreign language, you might learn a lot out
05:23of a book, but when you're in that country where the language is spoken, you
05:27pick up on idiomatic phrases like this one;
05:29who would ever guess you can say this, isempty(ActiveCell)?
05:34I guess you could say it's backward English, but we don't need anything behind it.
05:40So what are we saying now?
05:42Perform all of these statements until we see the word loop over and over
05:45and over again, until the ActiveCell is empty--
05:49one of the key building blocks in certain kinds of macros like what we're seeing here.
05:54So what we've set up here, and this is actually the heart of this macro, as we
05:58jump back into the data here,
06:00we want to do this throughout the length of this data.
06:02Here is down 2,400 rows, and this is the Colorado list.
06:06Our California list is going to be seven or eight times as big.
06:10We want it to work no matter how big the list is.
06:13Although we have some other tasks to complete here, the main purpose of this
06:18macro is to get this data into a database-type layout.
06:22Obviously, we've got empty rows over there which we need to deal with, but the
06:25key idea here is introducing the structure called a do loop, or possibly a for
06:31loop in other situations.
06:32But you're going to need either one of those in a variety of situations where
06:36macros are needed when handling large amounts of data.
Collapse this transcript
Testing a macro in Step mode
00:00A key concept in any programming language is the idea of testing and
00:05then debugging a macro.
00:07Macros, of course, despite our wishes, don't always work the way we want them
00:11to. And sometimes it's a huge leap-- and it's even a dangerous leap--to say well,
00:16I've tried my macro, or I think I have it all set up,
00:19I'll just run a macro on all my data and hope it works.
00:23In this particular workbook there is a macro already written that is set up
00:27to handle the data.
00:29Pressing Alt+F11 here. Here's the code.
00:34We want to, one by one, take each set of data, copy it into column D, transpose
00:41it, come along and do the next record, and so on, and so on, and so on.
00:43We're ready to try this, but should we really try this on all of our data? Answer:
00:49no! And particularly if your macro is manipulating lots of data and possibly
00:54deleting data--and as a general rule of thumb I would suggest that if you do
00:59have a macro that's about to delete data, back up your data first.
01:03Things go wrong sometimes, and programmers have almost a fatalism that says if
01:08something will go wrong, it will happen.
01:10It's almost like Murphy's Law.
01:11So what are we talking about here? The idea that we might want to test this
01:15macro line by line at least for a few iterations here.
01:19And so once again we want to use our split screen technique. Upper-right corner,
01:23click the Restore button, and in Visual Basic--if you're not there, press
01:28Alt+F11--we want to do the opposite, put this on the other side of the screen.
01:33And here and there, as we do this, sometimes you click that button twice. There we go.
01:37Then we have to move it around and resize it manually.
01:40And once again, this can be on either side of the screen.
01:43In Visual Basic, we have something called Step mode.
01:47We don't see this in the menu.
01:48It's on the Debug menu.
01:50But here's the idea; here is what we'd like to do.
01:52In the workbook part of our screen in the regular Excel window, we want to have
01:56the ActiveCell anywhere because we want our macro always to start at B1.
02:01That's what the code says so we have this here. And we'd like to actually step
02:05through the code and watch it work.
02:07Obviously, we don't want to do this for 2,400 rows;
02:10we'd be here forever.
02:12And so what do we want to do?
02:14The key part of the macro that still could be in doubt is this looping
02:18concept going to work.
02:20These statements are to be performed over and over and over again until the
02:25ActiveCell is empty, but are we sure we got that right?
02:28Is this going to work?
02:30And if we move down our data six rows at a time, does that mean that all six
02:34cells have to be empty?
02:35How is this going to play out?
02:37So, we should be concerned.
02:39Temporarily, here we'll make this lower screen bigger.
02:42And the idea here would be, let's insert a break in the data.
02:46It doesn't have to be 6 rows.
02:48Let me go down maybe right to here, and just put in a bunch of empty rows.
02:52I don't care how many, just a few. Insert.
02:57And if our macro that says do this until it is empty, when it comes to an empty
03:02cell, it should stop. But let's make sure. And as we do this, we want to see the
03:07code and learn more about the code in the process too.
03:10Again, we can split these windows on a variety of different ways. There we go.
03:15We've got the active cell here. Now, what do we do?
03:18We click inside the code somewhere.
03:20It could be at the top, bottom;
03:21it doesn't make any difference. Up there, it's on.
03:23And we start the process usually-- this isn't the only way--but debug is the
03:27sensible place to start.
03:29It's the only word up here that suggests what we are about to do, Debug.
03:34And not so obvious, but the way you will do this probably the next time and the
03:38next time, and thereafter, is simply start by pressing F8.
03:41But you'll notice here it says Step Into, and look what happens.
03:46It highlights the code in yellow.
03:49Now every time we press F8 the yellow highlighting will move to the next line of
03:54code in the macro. So if I press F8, it jumps here to this line.
04:00Now you'll notice that in the worksheet portion of the screen here, this hasn't
04:04happened yet, and it doesn't happen until we move beyond it. So as I press F8
04:08again, you'll see the next line highlighted, but the action would have taken
04:13place--namely, the active cell moves to B1. And sure enough, as I press F8 again,
04:20you know what's about the happen;
04:21it's going to be highlighting the next six cells, or this and the five cells
04:25below it as I press F8, and you see what's happening.
04:29Now this line here will not generate any difference, but action is being taken
04:34behind the scenes. The ActiveCell is being evaluated to see if it's empty.
04:39If it is, nothing will happen inside this loop.
04:42It will jump out of it.
04:43We press F8. Now what?
04:45We are about to do a copy. Press F8 again, and you'll see something distinctive:
04:50the so-called marquee lights that we see around a range when we copy.
04:54The next line means what? Move over two columns to the right.
04:57We stay in the same row.
04:59The row reference doesn't change, columns two to the right.
05:03We move to the right and select one cell.
05:05It's going to be D1.
05:07Next statement, we will transpose. There it is.
05:12Next statement, down six rows and two columns to the left, and when we get
05:18there, select six cells. There we go.
05:22This line here which eventually can be eliminated is a bit strange.
05:26This will simply remove those so-called marquee lights. Press F8.
05:32Now we are at the Loop command. What does this mean?
05:34Go back to the top of the Loop.
05:36Is the active cell empty? No, it's not.
05:39Let's move in and do this again.
05:40I'll speed this up a little bit, copy and paste, back here.
05:44Go to the loop, go back up top. Is it empty?
05:46No, it's not. We'll do this again.
05:48And you see what's happening in the Worksheet.
05:50There we go, back again. Is it empty?
05:52No, it's not and go back again. Loop.
05:56Now, where is the ActiveCell in our data?
05:59The active cell is B25, and you see it in the upper left-hand corner.
06:03Even though a range is selected, the active cell is B25.
06:07If part of this had data in it, it would still register as an empty cell.
06:11But when we press F8 here, we go back to the top of the loop, and now that cell
06:16is being evaluated, and is it empty? Yes, it is.
06:20So, what happens?
06:21Action moves out of the Loop.
06:24That's what you want to do in testing macros, particularly if you are dealing
06:28with large amounts of data.
06:30Don't make the mistake.
06:31I have done it in the past.
06:32Early on in my use of macros, I had a large amount of data, and one time I
06:36zipped through it, and I was copying and moving data in a really erratic way.
06:41And remember this key idea:
06:43when you run a macro, the Undo button does not include the macro actions,
06:49and you can't undo them, so it's a good idea to back up your data first and be
06:55careful with that whole concept.
06:57And I would say stay away from macros that delete data until you really need
07:01them, until you get more comfortable with the idea.
07:03Now there is one other aspect to this. Sometimes it will throw you. Sometimes
07:08you jump back into the worksheet, and then you might try this command or that
07:11command, and occasionally you find yourself saying, why can I not do this?
07:14What's happening here?
07:15The macro, in a certain sense, is still running, but is suspended. And so in the
07:20Visual Basic window, you do want to click the Reset button.
07:25Have we tested this enough?
07:26A lot of people would say yes. And so, what do we do next?
07:30Let's delete these empty rows that we put in. Right-click > Delete in this
07:36example here, and let's delete this data out here.
07:39Are we ready to run this macro?
07:45We can run it in one of two ways.
07:47From the workbook, we can press the keystroke shortcut, and from the Visual
07:51Basic window, we can also go to the Run command, click here, and notice also we
07:57might just click F5.
07:58So, either through the Command or F5, or in the workbook part of the screen
08:02here, we could press the keystroke shortcut.
08:04Press F5. Watch the micro run.
08:07It's got to go through 2,400 rows, and how long is it going to take? Not very long!
08:14Occasionally when you record a macro you might see a pause here and there, but
08:17usually it zips through the data pretty fast.
08:19On certain occasions, I've taken data like this, and just because I had the time
08:23I wanted to check it out, I doubled the data and then copied that quadrupled to
08:27see how long it would take and actually did some timings here and there.
08:31And quadrupling the data, for example, is not going to make the macro
08:34four times as long; it will be longer.
08:37But the main idea here is it handles a tremendous amount of data quickly.
08:41Now, we're not really finished with the macro yet. And do notice where
08:46the ActiveCell is now;
08:47it's in B2401, just under the data.
08:50I am going to press Ctrl+Home, and we see that we still don't have the data the
08:55way we want it. But the key idea--going through the data relatively quickly with
09:00the macro does get the data in the shape that we want.
09:03The next things involve pulling the data together.
09:06But the key idea, going back to the beginning of this particular movie, is the
09:10idea that we test out our data using just a few rows or a few columns, as the
09:17case may be. We are satisfied with it. We then put the data back in the order we
09:22want it in and run the macro in its entirety.
09:24But testing a macro is always a good idea, particularly when you are working
09:28with large amounts of data.
Collapse this transcript
Pulling the data together
00:00In this workbook called Partial-4, a macro has been run to take the names from
00:06column B and transpose them into columns D, E, F to the right, name after name,
00:11or name set after name set, all the way through the data.
00:14Now the macro isn't complete because we wouldn't want to leave the data looking
00:18the way it does to the right here--all those empty rows.
00:22When the macro finished, also the active cell is way at the bottom of the data
00:26over in column B. Down around row 2,400.
00:29But what's the next thing we want to do?
00:31We actually want to record an additional macro and then put the two macros together.
00:37And the key idea here is we want to pull this data together.
00:41There are a couple of different approaches to it, maybe more.
00:44Here's one, and not so obvious--and by the way, one of the things we'd be
00:48thinking about here, too, is if we want to get all this data together, we
00:52probably will highlight these columns.
00:55At this point, we would want to make sure that we are in Absolute Reference
00:59mode, not Relative Reference mode.
01:01So we'll make sure that we do that, too.
01:03Here's one approach, it has an advantage;
01:06there's another approach, it has more advantages.
01:09I've selected these columns, and a rather obscure command sequence--but one
01:14that has some real power to it-- begins from the Home tab, the extreme right
01:19button in the Editing group, Find and Select, and the not so obvious choice, Go To Special.
01:26In here, we can choose Blanks.
01:28Out of all those cells there, let's choose the blanks.
01:31By the way, you might be thinking, does this choose blanks all the way down a million rows?
01:35No, just in the active part of the data, we see blanks highlighted here.
01:40So I just scroll down kind of quickly here, and you'll see it's not highlighting
01:44the cells below that.
01:46There is that list at the bottom.
01:47It just goes to there.
01:48We want to get rid of those blank rows, and we can do that.
01:51I say rows, only the cells really, between columns D and I. Right-click >
01:57Delete, not the rows but all the cells here, and we want to shift all the cells
02:03below that are not blank upward. There we go.
02:06Now that's an unusual command sequence, kind of show-off-y in a certain sense.
02:11But it does have that big advantage of keeping the records in the same order,
02:16and that could be important.
02:18Maybe it's not obvious what order they are in, but if it's important to you to
02:21keep that same order--sometimes it is-- that's the advantage of this approach.
02:26I am going to undo this by pressing Ctrl+Z. Take us back here.
02:31Another approach--and I think "better" from a couple of perspectives--will be
02:35simply to select these columns and then sort the data.
02:39The active cell was in column D, and therefore if we sort by going to the Data
02:44tab and pressing AZ, we'll sort the entire list in ascending order based on the
02:49data in column D. There it is.
02:52The one advantage is this is a standard, frequently used Command;
02:56everybody use this sorting.
02:58The other advantage of course is it does put the names in alphabetical order.
03:04So of the two approaches, I think this is the preferable one.
03:07But do remember that the previous choice of selecting the blanks and getting rid
03:12of them does have that advantage of keeping the names in the same order.
03:16So let's suppose we are going to go down this path.
03:18Once again, I'll press Ctrl+Z to undo, and let's remind ourselves what we
03:23want this macro to do.
03:24At first, we want it to select these columns.
03:29So even though the macro that has already been run has left the active cell at
03:33the bottom, we can start this new macro by selecting the columns.
03:39So, we are going to record.
03:40And as we do this, it's going to make sense to see the macro code being built.
03:45Upper right-hand corner.
03:46We'll use the Restore button, make this roughly half-screen, do the same thing
03:51with the other half if it's not already there.
03:53And since we just opened this file and the previous macro was created in the
03:58Module1, if we record a new macro, it will go into a Module2.
04:03If we really want to see that, what we'll have to do here, kind of quickly, is
04:07just create a throw away macro to activate that new module.
04:10So I create a new macro, click here, don't bother naming it shortcut anything,
04:15click OK, and stop it immediately.
04:19What has that done?
04:20In the Visual Basic window above, we've got a Module 2.
04:23There is that throwaway macro.
04:26Let's get rid of it.
04:27Now as I record a macro, we'll be seeing it up here in the top of the screen.
04:32We want this macro first to jump into Absolute mode and stay there, and
04:37then select the columns.
04:39Let's record a macro.
04:40We don't need to name it or keystroke shortcut.
04:43We want to make sure it's in this workbook. Click OK.
04:47Let's go to Absolute mode, View tab > Macros arrow, no orange border, we are in Absolute mode.
04:55Let's select columns D through I, and you see the macro code above.
05:03Data tab, AZ, and you see all this sorting code up above. There it is.
05:09Macro was sorted alphabetically by last name.
05:14What else do we want to do with this?
05:15We probably want to put titles in, so let's just select these top cells here, right here.
05:22Again, looking at the code from above, and we see what's happening:
05:26those cells are highlighted.
05:28Let's simply right-click and Insert > Shift cells down, and once again take
05:35a look at the code.
05:36Next thing to do is to scroll leftward here and highlight the data in column A for our titles.
05:44Right-click, Copy, and now we are going to be right-clicking in D1, Paste
05:52Special > Transpose, OK.
05:56I think most of us probably would say, let's align the data on the left.
05:59So let's go to the Home tab and choose left alignment.
06:04And probably another thing most of us would do would be to adjust the width
06:07of the columns here.
06:08Simply drag across the columns.
06:10Now, we'd simply double-click one of the column boundaries between letters, and
06:14you see the code up above;
06:15that's the so-called autofit.
06:17Although not critical, it's more or less a convention not to leave a macro
06:21with data highlighted;
06:23simply click on a single cell. We'll click on D1.
06:26That's it.
06:28So this macro, which obviously is not a stand-alone macro, but this macro does
06:34the work of pulling the data together.
06:36We saw two alternate approaches.
06:38One got rid of blanks, one sorted the data.
06:41I think we made the case for the second approach being better.
06:44Here's our macro code.
06:46The next thing we need to do eventually is to put this together with
06:49the previous macro.
06:50And there are lines in here that will eventually disappear.
06:54Any scrolling that you need to do to see data on the screen can be gotten rid of.
06:58That's going to be dealt with as well.
07:00So the main idea here is it's a continuation of the idea.
07:03We record macros in pieces and chunks, and we put them together into a
07:07larger macro.
Collapse this transcript
Joining two macros
00:00In this workbook called Partial-5, a macro has been run to take the names from
00:06column B, the names and addresses, and put them in the columns D, E, F, etc., to the right.
00:12Another macro was then written to sort the data and pull it together, readjust
00:16the column widths, and add the titles.
00:18And it's not uncommon to write a series of macros to achieve steps that
00:22ultimately are going to be put together.
00:25It's time to join the two macros. As I press Alt+F11, here is the macro that
00:31transposes the data, row after row, 6 rows at a time;
00:36that's the major focus of what we want to do.
00:39Another macro was recorded later, and because it was done at a later time after
00:43the file was closed, it's in a different module. Makes no difference whatsoever.
00:48What we want to do is to put these macros together and join them, and in the
00:52process clean up some of what we see here.
00:55So this macro here, the macro that does the sorting--and by the way sorting
00:59action takes place here.
01:01All we need to do here is to copy this code, and since we are actually going to
01:06remove it, might as well cut it.
01:07There is this is the scissors for cut, or Ctrl+X of course.
01:11And we are going to go Module 1 and just put this at the bottom.
01:16Ctrl+V or Paste, we really don't need Module 2 any more, and we could leave it empty.
01:22If we want to get rid of a module, Module 2 is empty. I just double-clicked it.
01:26Right-click the module, remove it. We don't want to export it, so its history.
01:31Everything is in Module 1, and there is the split.
01:34We can join the two simply by getting rid of these two lines right here.
01:38The macros are joined, and we could run them together; in fact we probably should
01:43test the two of them together.
01:46It's always worth exploring the code a little bit. The thing want to be careful
01:49about and be looking for are exact addresses.
01:53Here's one right here. And this raises a potentially serious issue: if we are
01:58hoping to be able to use this macro with different amounts of data, it looks as
02:05if in the sorting process here this only sorts the data in column D through I,
02:11rows 1 through 2,401--that's how many rows we have in this data.
02:16How do we change this to make sure that it's going to work no matter how many rows we have?
02:22We need to take out the actual row reference on both sides, and this
02:26simply means sort the data from columns E through I--the entire length of those columns.
02:34Those are the kind of things you look for. And from previous movies, you remember
02:39that the choices like this when they are by themselves have meaning, but when
02:45we see them in context with offsets, those are relative references. We don't worry about them.
02:50But it is something to be concerned about when you see addresses. For example
02:53here, that's just fine; it doesn't explicitly mean A1, A6.
02:58We have got these two macros together, and we don't want to run this in Step
03:01mode. We've checked in Step mode how this works; it works just fine. And we don't
03:07necessarily have to see the code here. Let's jump back into Excel with Alt+F11.
03:11Let's delete the existing data: right-click and delete.
03:17Let's run this macros again.
03:18Active cell can be anywhere.
03:19We are going to press Ctrl+Shift+C. It doesn't take very long.
03:25The two separate macros are now together.
03:28Once again, back to Alt+F11.
03:30A few more things to clean up here eventually, but the key idea here was, anytime
03:35you record macros, the potential exists for putting the macros together. And many
03:39times it's just a copy and paste.
03:41You put them next each other, delete the final line of one and the opening one
03:45of the other, and they are together.
03:47Now a longer course in VBA will introduce other concepts where you sometimes will
03:53want to break up macros into separate little programs, and then have one macro
03:59call, or get, what another macro does.
04:02That's a more sophisticated use of it.
04:05For the example here, this makes perfect sense. We recorded two separate macros
04:10and then put them together with copy-paste techniques.
04:13We joined the two macros.
Collapse this transcript
Streamlining macros
00:00When you run a macro, often it's very fast, but do you really need to watch all
00:04that copy and paste action? And particularly if you're dealing with lots of data,
00:09and you've run the macro a few times, you have tested it out.
00:12In this workbook, there's a macro. The workbook is called Partial-6, a macro
00:17that takes the data out of column B, transposes into columns D, E, F, sorts it,
00:22puts titles on top, does the job.
00:25But after you have tested a few times-- I am going to run this again. First, I will
00:28delete the data here, and simply click anywhere on the worksheet and press
00:34Ctrl+Shift+C. Nothing wrong with this, and you want to see this at least a few
00:40times. It gives you a sense of how it's working.
00:43I am going to make sure it's making progress.
00:45If we had three or four times as many rows, it would take longer.
00:48There is this question: can we make the macro run faster?
00:52By pressing Alt+F11, we can take a look at the code.
00:55Now you would never guess that what I'm about to type here
00:58will do this, and you wouldn't necessarily know that the future exists.
01:02But at the very beginning of this macro, I am going to click in front of this first
01:06statement, press Enter, and enter a new line, application.
01:12This is as a line of VBA code.
01:15Look what happens when I press period.
01:17It's a reminder that Visual Basic, VBA, is a full-fledged programming language.
01:22What might follow this--and this is a huge list, a lot of choices here.
01:28From my experience, I know that there's a feature that will turn off or inhibit
01:32all those updates. That's what they are actually called, called screen updating.
01:37So I am going to press the letter S, and what do we see now?
01:41The options that can occur next, beginning with letter S. Here's what I
01:44want, screen updating.
01:46I don't think that's a phrase you'd necessarily be searching for if you're
01:50trying to find this feature.
01:51Screen updating means changes you make to the screen, as this macro does as we copy and paste.
01:59I could either type this in or press Tab.
02:02If we use the setting ScreenUpdating equals False--and here too we could
02:08press Tab--we will not see changes as they occur on the screen. And of course
02:13we do want to see that when we're testing a macro initially, but eventually we
02:16don't, in a lot of cases.
02:18Now what I often do in macros that deal with large amounts of data, I'll enter
02:23this or put this into the code at the beginning but not use it for a while.
02:27By simply putting a single quote in front of it, that makes it a comment.
02:32So I have a position to be used a bit later, and here and there if I make
02:36changes to macro, I might want to turn that on or off.
02:40By activating this--in another words making it not a comment, but code--I will take
02:43this off, I am going to jump back into the worksheet with Alt+F11, get rid of the
02:49data, and run this macros again.
02:54The downside of this will be we are in suspense a little bit.
02:57We won't know anything is happening until the end.
03:00So I am going to press Ctrl+Shift+C. You might see some screen flutter here. It's done.
03:06It was undoubtedly faster than what we saw before. Alt+F11 again.
03:10Now in certain macros that perhaps open different workbooks or get to certain
03:15stages, at a certain point in the macro you might want to have the statement put
03:19in reverse at a later time.
03:22If we see Application.ScreenUpdating equals True, from there on after that
03:27statement, screen actions will be visible. You might have it on just for the
03:32code that opens a workbook, for example, and then put this line in again to
03:37turn off the updating.
03:39So you have got some control of how that's being displayed or not. But it's
03:42a valuable tool in longer macros. And I remember one macro a few years ago
03:48that I'd run it was three minutes long, then I applied this, and then it took 20 seconds.
03:53So here and there, it will make a difference with longer macros.
03:56A few other aspects here that we might want to change, none of them too critical.
04:00When we copy and paste, this line of code which you might have seen earlier
04:05simply removes the marquee lights from copying. And it's not really a critical
04:11step; we can get rid of it.
04:13Now what if you say, well I'm not sure if I want to get rid of that, what can
04:17we do instead? We might press Enter here, copy this, Ctrl+C, or drag it, paste it,
04:24so on, go right here, Ctrl+V, and make one of these be a comment. And let's get
04:31rid of the other one.
04:31So what's the idea?
04:32We might run this macro, and if it works okay, we are going to come back here
04:37and get rid of this.
04:38If it appears to cause problems, we will come back here and get rid of the single
04:43quote and resurrect it.
04:44Once again, you can put empty lines wherever you want for clarity, and here
04:48and there certain lines of codes can be made more compact.
04:52Programmers often like to have their code be as concise as possible;
04:58I wouldn't say that's a noble objective, but here and there it's just easier to read.
05:02Scrolling, if you happen to see scrolling, we can get rid of these. The only reason
05:06we did scrolling in the examples here is because we needed to see certain data,
05:11and that didn't involved changing active cell or anything.
05:13So these can be eliminated. Delete them.
05:16That tightens it up a little bit.
05:17In certain kinds of actions like this, the action here was simply to
05:22horizontally align the data on the left side.
05:25That's the first part of this.
05:27The other actions are all default settings.
05:29We don't care about those; we can get rid of them.
05:33So that throws away a lot of code there.
05:35So those of the kinds of things you look for here and there.
05:38We should get rid of that space as well, too.
05:41And also with this action here, the key action here is transpose. Do I really
05:46need worry about these other actions here? So we could just get rid of this.
05:51These are default settings. Highlight this data here, simply press Delete,
05:56another Delete, that cleans that up.
05:58Then we have similar paste up here, too.
06:01Once again, not critical but the kinds of things that simplify the look of this,
06:06make it easier to read, occasionally some of these shorten operation of the macro,
06:10but usually not a whole lot, what we are doing here.
06:13So those of things we look for. Let's run this macros again, and if it appears
06:17that this doesn't make any difference whatsoever, we will come back and simply delete it.
06:21Alt+F11, delete the data, press Ctrl+Shift+C, macro is done.
06:29Let's say we don't see any problems there. We will press Alt+F11 get rid of this.
06:34So we've seen any number of different techniques for streamlining a macro.
06:38The key one in terms of macro speed is the VBA line
06:42Application.ScreenUpdating = False.
06:45We also saw some other techniques here to remove some extraneous code, make the
06:49macro look more concise.
Collapse this transcript
7. The Personal Macro Workbook
Creating a personal macro workbook for macros with wide applicability
00:00An important tool in your use of macros is what we call the personal macro
00:05workbook. And the question arises, how do you create this personal macro workbook
00:10that you need to use to store macros that could have applications in a variety
00:15of different workbooks?
00:17On my screen here, I've got a workbook that has some macros in it. The name of
00:21this workbook is called Useful Macros.
00:23Here's one of those macros, as I jump over to the partial budget sheet.
00:28I've got a macro that displays large numbers like this in millions.
00:33I am simply going to press Ctrl+Shift+M, millions with a single decimal point,
00:39makes the numbers easier to read for these kinds of situations.
00:43I've got another macro that I recorded and stored in this particular workbook
00:48that displays numbers like this, quickly, without decimals.
00:52Many of you know that the Dollar Sign button in the Number group on the Home tab
00:57applies Accounting Number format, but it does apply two decimals.
01:01You can easily push the button on the right here twice to decrease those.
01:05But what if you want to have a button that makes the display instant rather than
01:09having to use this each time?
01:14So I have got a macro that displays numbers like this in Accounting format with
01:20Dollar sign, but without decimals. Ctrl+Shift+A. I have got that.
01:25The reason I would have a macro like this is because I use that kind of feature
01:31often. Now, in both of these examples I have created the macro while using this
01:35workbook, and that's where it stored. And I have got a third one as well, Mixed
01:40Names worksheet here. Here are some formulas in column E. This is a simple,
01:45little formula here using the upper function, and you can see what it does.
01:49It simply takes the letters that are in the column D entries, makes them be
01:53uppercase. It's easier to read and they are used throughout the column here.
01:58And what am I getting at? The idea that many times when you have formulas like
02:02this, you want to keep the results to throw away the formulas. And we can do this
02:07by a manual process, but I have got a macro all set up for it here.
02:11I will just highlight the data and press Ctrl+Shift+V. And these are no longer
02:16formulas; they are just pure values.
02:19As I created these, they occur in this workbook. So I am going to press Alt+F11,
02:25and there they are: three small, short macros.
02:29It would have been a better idea had I created and stored these in personal
02:33macro workbook, meaning if I had done that, from now on, I could get to these
02:39macros no matter which workbook was open--and furthermore it would make sense
02:44to have each of these macros be represented by a button on the Quick Access Toolbar.
02:48So let's say on the one hand maybe we put these in the wrong place, but it's not
02:53a major issue. It's certainly not wrong when you're creating macros.
02:57You don't necessarily need to put them in personal macro workbook. Even if you
03:01do think there is going to be wide applicability, you can do it later, so you
03:04can do it either way.
03:05But you will notice on my screen here that there is no personal macro workbook,
03:10and that's the way it's going to be in your environment as well, until you
03:13actually create a macro to be stored in the personal macro workbook.
03:18If you are using a computer that someone else has been using Excel on, maybe off
03:22and on for a few years, you might very well see, as you enter the Visual Basic
03:27Editor, an entry over here that indicates there is a file called Personal.
03:32So how do we create this?
03:34Well, fortunately, we don't have to go through any formal creation process.
03:38I am going to press Alt+F11 and jump back into Excel.
03:43I've already mentioned a macro that displays values like this with a
03:48single decimal place.
03:49What if we want to create another macros that's almost the same, that does apply
03:55Dollar signs? In other words I would like to see Dollar signs on this top row.
03:58Well, let's highlight this data and this time create a macro to go into the
04:03personal macros workbook. And again, you might be saying, well, there is no
04:07personal macros workbook. Well, that's true as of now, but this sets the process in motion.
04:13We are not formally creating a new workbook.
04:16Let's click the box in the lower left-hand corner on the status bar, start a macro.
04:21We want to make sure right away this is going to go into the personal macro
04:25workbook. There it is.
04:28We will give it a keystroke shortcut of Ctrl+Shift+K, and I want to call this
04:35macro DollarValuesInMillions.
04:43Now where is the macro going? Into the personal macro workbook. That's where it's going to
04:47be stored. And the only difference in my display here will be the Dollar sign.
04:52So by right-clicking and going into Format cells, or pressing Ctrl+1, any number
04:56of different ways to get here,
04:58we want to be on the number tab, and we can usually start this simply by
05:02picking one of the entries from the early list here.
05:05If you have chosen Custom, pick, for example, something like this and simply end this with .0,,
05:13that means millions, and then on the front of this, let's put in a Dollar sign.
05:21Click OK and we see the results immediately.
05:24We are done with this macro. We stop recording.
05:27Now let's press Alt+F11. And now we see a new entry in the Visual Basic Editor,
05:36top here, VBA project personal.
05:38We have created the personal macro workbook simply by saying that we want our
05:44macro to be stored there.
05:45We look under Modules.
05:47There is the plus. Here is Module 1, we double-click, and there is that macro.
05:52Now here's something that will surprise you.
05:55If you're in the Excel workbook maybe would have done this right after
05:59creating this macro. Surely Alt+F11 is fast, but what if you went to the View
06:04tab and chose Macros > View Macros? And I'm looking around for macros
06:11where? Not just this workbook. We could choose either personal or all open
06:16workbooks--either way.
06:19There is the macro that just got recorded.
06:21What if we want to edit the macro?
06:23When you click Edit, you are going to be surprised.
06:26"Cannot edit a macro on a hidden workbook, Unhide the workbook using the Unhide command."
06:32Well, that's a little bit strange isn't it? And this may throw you, and if you
06:37haven't been aware of the Alt+F11 process, this might kind of stop you in your
06:41tracks and say, well, I don't know what's going on here at all.
06:43This doesn't make any sense.
06:45Alt+F11 is the way to get in there.
06:48Now you did notice the phrase "a hidden workbook." What's that all about?
06:52Every time you open Excel after having created the personal macro workbook,
06:57that workbook, personal macro workbook, will always be open, but is opened as a hidden file.
07:04It's a little bit strange to go into the View tab here to unhide.
07:10We can choose this. What can we unhide? Unhide workbook personal.
07:14Is there any reason to do this?
07:16Not really. You can click OK and say, well, now what?
07:20There is that workbook.
07:21There is no reason to really do that. So let's hide it.
07:26But it does explain the mechanism of what is happening here.
07:29The personal macro workbook, once it has been created, and it need only be created by
07:34you storing a macro there, from there on in, its always available every time you
07:40open Excel. It is available as a hidden workbook, but all we care about is when
07:45we press Alt+F11, we can make changes and additions and deletions to the macros
07:51that are found there. And these other macros that we created earlier that we are
07:56in the useful macros workbook right here--and I am down below here
08:01double-clicking on Module 1--
08:03we can copy these and put them into personal macros workbook. In fact, better yet,
08:09why don't we cut them from here using Ctrl+X or the Scissors button up here?
08:15Let's cut these from the workbook called useful macros and put them into
08:21personal macro workbook, because we want to them to be available all the time.
08:25Ctrl+V or paste right here.
08:29So in a certain sense you can say, well, it doesn't make any difference where you
08:32create your macros and store them.
08:34Well it does, ultimately, but you can move them around relatively easily.
08:38You can copy, you can paste, and what we showed here was the idea that once you
08:43create the personal macro workbook, it's available. You can't get to it directly
08:48by jumping into edit, but you can always get into with Alt+F11. And once created,
08:53you do have available, in all workbooks, these macros.
08:58And there is no strong reason for saying I want to unhide the personal macro workbook.
09:04It's open, and it will be open all the time as you work with Excel, and it's the
09:09ideal location for storing macros with wide applicability.
Collapse this transcript
Closing and updating the personal macro workbook
00:00Once you have created a personal macro workbook and have started to use it on a
00:04regular basis, you want to be sure that when you make changes to macros there
00:09that they're currently up to date.
00:12And you might notice this, too: when you enter Excel after having created the
00:15personal macro workbook, the Unhide button on the View tab is selectable.
00:21Now even if this file is closed--it's an empty file, I'll just close it--that
00:27Unhide button is still there.
00:29It's a reminder that we have a hidden workbook that's open; we're not seeing it.
00:35It's the personal macro workbook.
00:37It's always available whenever you open Excel once it has been created.
00:42And whether we have a file open here or not, we can quickly press Alt+F11 and
00:49see that the personal macro workbook is there and see the macros that are in
00:53it. Here is Module1.
00:55You do have control over the organization of macros here. And what if you make
01:00changes to the macros? If we were not altering files, necessarily--
01:03now probably we are most of the time, but let's imagine jumping back into Excel here.
01:08Maybe I'll open a recent file or create a new file with Ctrl+N. Maybe I'm
01:13building some date in here. Maybe I'm using one of my macros, maybe not.
01:18But from time to time, we have thoughts about, what what's going on here?
01:22What if we make a change to the macro, either by adding a new one or altering
01:26some of the text here?
01:28Although you will get a prompt when you exit from Excel about updating personal
01:32macro workbooks--and you would nearly always say yes--
01:35you do have the option--and it's recommended--that from time to time you go to
01:40the File menu within the Visual Basic editor and Save PERSONAL.XLSB.
01:47That's the complete file name.
01:48Just by doing this, you are sure that your personal macro workbook is up to date.
01:54If you forget to do that, again, you will get a prompt when you do a normal exit
01:58from Excel, but occasionally--you know what happens--the system crashes or something
02:02goes wrong and changes that we made here will not have been saved.
02:06There is an organizational idea here also. Three of the macros here relate
02:12to formatting issues.
02:14And although you don't necessarily have a lot of macros here, the more you have,
02:19it might make sense to categorize these and store them in a certain way.
02:24Right now, under Personal here, there is one module that's called Module1.
02:28Let's change the name of this.
02:31When you click the name Module1, you'll notice in the Properties window below
02:35this--and by the way if you don't see the Properties window, go to the View tab
02:41and activate it with this button right here, Properties window.
02:45So in the lower properties window, we see the term Module1.
02:49Let's just put in FormattingMacros.
02:51That would be a good way to describe those macros, at least three of them.
02:57Whatever you type here, by the way, do not put in space. And as soon as we press
03:01Enter, we've provided that name.
03:03If you'd like to add a module, it's from the top menu: Insert > Module. And it
03:10will have that default name Module1.
03:13Maybe we just want to call this other macros or just leave it this way for a while.
03:17Again, we do this in the Properties section of the screen here in the
03:21Properties window from Module1 to OtherMacros. Of course you can come up with
03:26your own naming scheme.
03:27And so the original Module1, now renamed FormattingMacros, has all these macros in
03:32them, and at least three of them I want to keep, but say not this one.
03:36So I want to highlight this particular macro code, cut it, and then go to
03:42OtherMacros and paste it there.
03:46So you're in charge of this, and the more macros you have that you put into the
03:50personal macro workbook, you want to have a sense that you're in charge of the
03:53organization of them, and you know what's happening.
03:56What happens when we do exit from Excel
04:00if I jump back into the Excel window with Alt+F11? And whether I've got files
04:04open or not is not the issue, for the moment. I'm ready to exit from Excel.
04:09Click the X in the upper right-hand corner. Certainly one way to do this. And here
04:13is the prompt that you want to be very sensitive to: Do you want to save the
04:17changes you made to the Personal Macro Workbook?
04:20And nearly always you do want to do that, so you probably going to click Save.
04:24But it is a reminder: you want to do a normal standard exit if you have a
04:29personal macro workbook.
04:31You don't want to just go turn off the computer and fail to do this, because
04:34it's not going to happen for you unless you've been repeatingly updating it.
04:38So you're in charge of the personal macro workbook, and it's a great tool for
04:42storing macros that you think might have wide applicability.
Collapse this transcript
Conclusion
Final thoughts
00:01If you found this course beneficial but if you want to know more about Visual
00:05Basic, the language of macros, check out Curt Frye's course on VBA In Depth.
00:10If you want to review of some of the new features in Excel 2010, you might want
00:14to check out Bob Flisser's course on Excel 2010 New Features.
00:18If you want to know more about power shortcuts, you might check out my course on
00:22Excel 2010 Power Shortcuts.
00:24Thank you for watching!
Collapse this transcript


Suggested courses to watch next:


Excel 2010 Power Shortcuts (3h 43m)
Dennis Taylor


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 98,695 instructional videos.

start free trial learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 1,899 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.


site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked