Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
This workbook contains a macro that retrieves data from a separate file, copies it, and deposits it at the bottom of the current file. It just runs as a one-shot deal. It doesn't make any decisions. It doesn't ask for any additional information. If you do the exercise in the movie on recording a macro, you already know what its procedure is. What we will do in this movie is we will add three lines of VBA code to the macro that will ask your name then enter it at the top of the worksheet, so that the next person who runs the macro will know who used it last.
Before we run the macro there are a couple things we have to do. Over here, if you have this yellow bar that says Security Warning, that's just Excel's way of telling you that, hey, there are macros in here that could be dangerous. Do you really want to run them? So click Enable Content. If you have that yellow bar and you don't enable content, you can't use the macros. Second thing we need to do is we need to open up the workbook that contains the data we are going to retrieve. So you could either press Ctrl+O to Open, or go to your File tab and choose Open.
And in the Chapter 13 folder of the Exercise Files, let's open up this file here called overseas data. And you see it's the same structure as the other ones. We have an Order number, the Region, Month, Size and Quantity. So let's switch back to the other one. I'll press Ctrl+F6, or if you prefer you can go to the View tab and switch windows. So let's go and edit the macro. Go to the Developer tab, and there are a few ways we can do this. Over here in the left we have the button for the Visual Basic Editor.
We also have a Macros button. Click that Macros button. And this shows you the macro that we wrote. It's called getData. And to edit it, just click this Edit button. It brings us into what looks like another computer program, and really it is, and you see up here it's the Microsoft VB Editor. This VB Editor comes with Microsoft Office. You don't have to buy it separately, but you do have to install it. So if you don't have the VB Editor, you'll have to go and install it from your original disks. Now let's take a look at the code.
I am just going to maximize this window here so we can see what's going on. A couple of things is the macro starts off with this keyword Sub and it ends with End Sub. Sub stands for sub-procedure. All the code that you do in the macro has to be between the opening Sub and the ending Sub. If you put any code before or after the sub-procedure, it'll give you an error. The first few lines here are in green. And we see they start off with an apostrophe or call it a single quotation mark. These are comments. They are kind of like yellow sticky notes.
It's information for you or anyone else who's going to read the code, but Excel doesn't try to execute it. Here is the code itself. And I am not going to go into every single line of detail, but you can see it's pretty much human readable. Range A1 Select, you can probably figure out that it means to go to cell A1. And that's what we did for the first task of that macro. So let's add those lines of code. Click at the end here where it says ActiveCell.Select. Just click to put your cursor and hit the Enter key once or twice.
Blank lines are perfectly okay. So we want Excel to ask us what our name is. Now when it asks us our name, it'll display a dialog box. When we type in our name and click OK, our name has to get stored in a variable. So the variable that we will create will simply be called "your name." So type yourName equals and now we have to display that dialog box. So type in InputBox. Be careful. InputBox has to be a one word.
Open up your parenthesis. Open up your double quote, and let's type in, What is your name? Close the double quote, close the parentheses. Now after you type your name and click OK, Excel has to decide where to put that information. So press Enter and we are going to say range and then open up the parentheses. Open up the double quote and say a3. This is not case sensitive. Close the quotation marks, close the parentheses, type .Value.
By the way notice as you're typing the word value, Excel recognizes that it's a legitimate keyword. So that's how you know you're doing it right. So you can type a or hit the tab key, and it will continue. So let's say range("a3").Value = and then open up the quotation mark and let's say Data retrieved by, type a space, type the closing quote. And now what we wanted to say is "Data retrieved by Bob" or whatever your name is. So we will use an ampersand to append the data.
Now we need to append that variable that contains your name, which we did in the line above, which is yourName. One more thing. Press Enter. Notice when I press Enter, the word range on the previous line went from lowercase to uppercase. That's a little bit of the VBA syntax checking. When you see it's changing case on you like that, that's also a way that you know that your syntax is right. And now we'll type range, open up the parenthesis, open up the double quotes, a1, close the double quote, close the parentheses .Select.
And now when I type se it recognizes the word select. You see there is up that little flying green brick next to it. That means it's something that's actually going to do. And I'll just press the Tab key and let Excel fill in the rest. And just click anywhere else here. And you'll notice that when you do, if you type that range in lowercase, it will turn to uppercase. That's all we have to do. Now we want to get back to Excel. And you don't have to save what you did here, because this macro is stored in the workbook. So there is no need to save the macro separately from saving the workbook.
Now one or two things you could do. You could leave this VBA Editor open and go back to Excel, or you can close it. I just want it out of the way. So I am going to click the Close button there. Now we'll run the macro. Click in here and you could use the shortcut to run the macro, Ctrl+Shift+G in this case. Or you could go up to the macros button here and run the macro. So you have two ways of doing it. So I'll click Run. So it puts it in. It asks, what is your name? Type my name is Bob, click OK and there it is.
It deposits "Data retrieved by Bob," and now you can see it put the cursor in cell A1. If you scroll down, you can see down in the bottom there is all the data from the Overseas region. So you can see that even though we made just a small modification, you could see that VBA allows you to gather information from the user while the macro is running. It allows you to output data to any cell in the worksheet. And it also allows you to concatenate a premade phrase with the information that you entered. So hopefully you'll find this useful for your own projects.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.