New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Editing a macro

From: Excel 2010 Essential Training

Video: Editing a macro

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.

Editing a macro

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.

Show transcript

This video is part of

Image for Excel 2010 Essential Training
Excel 2010 Essential Training

80 video lessons · 124339 viewers

Bob Flisser
Author

 
Expand all | Collapse all
  1. 1m 35s
    1. Welcome
      57s
    2. Using the exercise files
      38s
  2. 19m 31s
    1. Exploring three common uses for Excel
      3m 17s
    2. Touring the interface
      3m 38s
    3. Finding the commands you need
      3m 51s
    4. Using Backstage view or the File tab
      3m 25s
    5. Maintaining file compatibility
      5m 20s
  3. 21m 23s
    1. Creating a worksheet
      5m 23s
    2. Techniques for copying and pasting
      3m 57s
    3. Entering data automatically with Auto Fill
      4m 37s
    4. Targeting large data groups
      4m 26s
    5. Changing a worksheet's structure
      3m 0s
  4. 47m 50s
    1. Understanding formulas and functions
      4m 41s
    2. Entering data in a worksheet
      3m 22s
    3. Adding numbers manually
      5m 1s
    4. Adding numbers using Sum and AutoSum
      6m 11s
    5. Adding a whole worksheet
      1m 48s
    6. Working with numbers in columns
      4m 53s
    7. Preventing errors using absolute references
      5m 57s
    8. Working with times and dates
      3m 8s
    9. Using IF
      4m 49s
    10. Using SUMIF and AVERAGEIF
      4m 15s
    11. Naming and using cell ranges
      3m 45s
  5. 33m 57s
    1. Formatting numbers and dates
      7m 6s
    2. Applying fonts, background colors, and borders
      4m 35s
    3. Adjusting columns, rows, and text
      5m 2s
    4. Using conditional formatting
      4m 6s
    5. Using custom conditional formatting
      5m 49s
    6. Adding pictures and shapes
      7m 19s
  6. 25m 27s
    1. Inserting SmartArt
      6m 54s
    2. Coordinating a look using themes
      3m 22s
    3. Applying built-in styles
      3m 16s
    4. Creating and sharing styles
      5m 33s
    5. Using templates
      4m 9s
    6. Creating and using original templates
      2m 13s
  7. 13m 23s
    1. Making the pieces fit
      4m 57s
    2. Inserting headers and footers
      3m 51s
    3. Printing and PDFs
      4m 35s
  8. 34m 3s
    1. Finding and replacing data
      3m 12s
    2. Freezing panes
      3m 0s
    3. Repeating row and column titles
      3m 34s
    4. Creating multiple custom worksheet views
      5m 18s
    5. Hiding or grouping rows and columns
      5m 31s
    6. Managing worksheets
      7m 23s
    7. Calculating formulas across worksheets
      6m 5s
  9. 36m 34s
    1. Importing and exporting data in Excel
      8m 2s
    2. Setting workbook permissions
      6m 44s
    3. Inserting and editing comments
      6m 49s
    4. Sharing a workbook
      1m 25s
    5. Tracking changes
      3m 5s
    6. Saving files in shared locations
      10m 29s
  10. 27m 30s
    1. Splitting cell data into multiple cells
      2m 22s
    2. Joining data from multiple cells
      4m 18s
    3. Basic and multi-field sorting
      6m 30s
    4. Using tables to sort and filter data
      4m 31s
    5. Inserting automatic subtotals
      3m 46s
    6. Creating lookup tables
      6m 3s
  11. 32m 56s
    1. Using auditing to diagram
      6m 3s
    2. Using evaluation in Excel
      2m 2s
    3. Working with Goal Seek
      5m 29s
    4. Using data tables in formulas
      6m 2s
    5. Using scenarios in formulas
      5m 28s
    6. Exploring the Analysis Toolpak
      7m 52s
  12. 18m 1s
    1. Discovering PivotTables
      2m 22s
    2. Creating a basic PivotTable
      2m 46s
    3. Modifying a PivotTable
      6m 57s
    4. Creating and modifying a PivotChart
      5m 56s
  13. 26m 58s
    1. Choosing chart types
      1m 55s
    2. Inserting Sparklines
      3m 54s
    3. Creating a column chart
      3m 23s
    4. Modifying a column chart
      5m 47s
    5. Creating and modifying a pie chart
      6m 45s
    6. Placing Excel charts into other Office applications
      5m 14s
  14. 21m 53s
    1. Understanding macros
      3m 5s
    2. Recording and using a simple macro
      11m 58s
    3. Editing a macro
      6m 50s
  15. 20m 33s
    1. Customizing the Quick Access toolbar
      3m 30s
    2. Customizing the Ribbon bar
      8m 44s
    3. Setting Excel options
      8m 19s
  16. 16s
    1. Goodbye
      16s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

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.

Sign up and receive emails about lynda.com and our online training library:

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

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.