Monday, July 22nd, 2013

Create your first macro in Excel 2013

Learn how to type a macro in Excel 2007/2010 & 2013. Covers displaying the Developer tab and how to create your Personal Macro Workbook so you can create and save macros. Then how to create a new macro, save and run it.

Transcript:

Hello, I’m Helen Bradley. Welcome to this video tutorial. In this tutorial I’m going to show you how you can create a macro in the Visual Basic Editor in Microsoft Excel. First of all you need to make sure that this Developer tab is visible and it’s not by default. So this is Excel 2007. To make it visible you’ll click the Office button here, choose Excel Options and this is the option you want to enable Show Developer Tab in the Ribbon. With that selected the Developer tab then appears in the Ribbon. Now that’s only for Excel 2007. I’m actually going to close Excel 2007 because I’m going to work in 2010, but I just wanted to show you how you could get the Developer tab in 2007.

Now in 2010 and 2013 it’s a bit different. To get the Developer tab there you’ll choose File and then Options. And what you’re going to do is go to Customize Ribbon because only in 2010 and 2013 can you actually customize the Ribbon. And there’s an option here for Developer. So this the Developer tab and again by default it’s disabled so you want to just select it so that it is enabled and click Ok. Now this entire video was prompted by a user who asked me for a macro that would delete every row in a worksheet if there was nothing in column A.

So I have a worksheet here and I have some rows in which there is nothing at all and what we want to do is to delete them. So the first thing we need to do is to make sure that the Developer tab is visible on the Ribbon and then we’re going to actually create this macro. In fact I’m just going to copy and paste it because I just want to show you the basics of creating a macro. This might be one that you’ve been sent or it might be one you find on the web or whatever.

Now before you can actually create a macro inside the place where it’s supposed to be you have to actually have this one file created. And chances are if you’ve never created a macro before you don’t have the very file that you need. The simplest way to resolve that situation is to first before you do anything else go and click Record Macro. And you’re going to store this macro in what is called the personal macro workbook.

If you don’t have it Microsoft Excel creates it for you so that’s why we’re going to the trouble of recording just anything so that we can get this personal macro workbook created. And once it’s created then in future we’ll be able to store macros in it and these macros will always be available to every single worksheet.

Whenever Excel is opened it opens this file and everything is fine and dandy so I’m just going to click Ok. And I’m just going to type a couple of letters in there, press Enter. That’s a macro. That’s all it does and click Stop Recording. I don’t actually want the macro and I don’t actually want the contents of that cell. All I want is this special file that is called personal.xls. So I’m going to click Macros now and you can see here is the macro that we just created in this workbook personal.xls.

It’s personal.xlsb whatever it is for your particular version of Excel. And this is a file that Excel takes care of opening every time you come and open Excel so these macros are always going to be available. Now I want to add a new macro. And the macro I want to add is called remove rows. That’s just what I called it. So I’m going to type the word removerows. Now macro names have to be one whole word. You can’t use spaces but you can use underscores. I’m just going to type removerows.

And now because I don’t have that macro Excel is offering me the option of creating it. But I want to create this in my personal.xlsb workbook so I want to make sure that I select that first. Macro name personal.xlsb workbook, click Create. And what that does is it automatically opens the Visual Basic Editor for me so I don’t have to do all the work. And it also places me right in the middle of this code area which is exactly where I put my macro code. So I have it in a file so I’m just going to copy and paste it. In fact I’m going to do a little bit of the wrong thing so I can show you the result.

I’ve gone and got my file and I’ve copied the macro code to the clipboard and I’m going to paste it in. And you can see that what I did was I bought in the sub and end sub statements. And you can’t have two sets of sub and end sub statements. So the first thing I’m going to do is get rid of the extra statements. And it appears here that my lines have been cut in pieces so I’m just going to make sure that these comments are back in single lines.

So here is my removerows macro. And this has also got cut in two so let’s just neaten things up a little bit. And this is a macro that removes rows in a worksheet where the cell in column is blank. It doesn’t require you to make a selection before running it. However as Undo doesn’t work after running this macro it’s a good idea to back up your worksheet first. Well, that’s good. And I’m just going to while I’m here clean up this macro that I recorded earlier that I didn’t really want to keep but I just created so that I would get this personal.xlsb workbook.

Now that I’ve finished inside the Visual Basic Editor, I’ve created my macro I’m just going to choose File, Close and return to Microsoft Excel. Now the macro is stored not in this particular file but in this special workbook. I want to test it so the first thing I’ll do is to save my file just in case everything goes haywire. The other thing I’m going to do is just for my own purposes is format these particular rows the ones that I want to get rid of with a color. What I want to do is when I get rid of these rows the very first time I run it I just want to make sure that the macro is behaving correctly.

So if it were to do something funny I’ve marked up in orange exactly what I don’t want to see at the end of this macro. So if it were to do something like this, so let’s just go and do Delete Cells, I don’t want it to delete cells. I want it to delete a whole row so if it were to end up with something like this happening I know that my macro isn’t working. So I’m just setting myself up to check to make sure that everything is working correctly when I do run my macro. And if it works perfectly on this worksheet then I’m just going to assume that it’s going to work perfectly every other time in future.

So here we have our worksheet. Now it’s time to run the macro. Now you don’t have to use the Developer tab to do it although you can. But you can use the View tab. We’re going to Macros and here are our macros and the macros are in personal.xlsb. And the one we want to run is called removerows so I’m just going to click to run it. All the orange highlighting is gone and everything that is left are just rows that have data in column A.

So that’s how you would create a macro yourself if you found it on the web or you downloaded it from somewhere or were given it in the Visual Basic Editor in Excel. Once the personal.xlsb workbook has been created the first time you don’t have to go through that record macro step. That’s only to create that file the very first time. From now on Excel is going to take care of that file. If you’re prompted to save it when you exit Excel, say yes because you do want to save all the data that you’ve created in it. I’m Helen Bradley. Thank you for joining me for this video tutorial. Look out for more of my tutorials on this YouTube channel and visit projectwoman.com for tips, tricks and tutorials on a whole range of Office programs.

Helen Bradley

Tuesday, April 24th, 2007

Excel’s personal.xls

There’s a special file in Excel called personal.xls which is opened automatically every time Excel opens. This makes it a great place to put your Excel macros as they’re then accessible to any open workbook.

Unfortunately, a personal.xls workbook is not created until you actually do it yourself so you may not have one. The simplest way to create one is to record a simple macro because then Excel does it for you.

To do this, open Excel and choose Tools > Macro > Record New Macro and type a macro name. From the Store Macro In dropdown list choose Personal macro workbook and click Ok. Record a step or two—it doesn’t have to be an actual macro but it just has to be something and click the Stop Recording button or choose Tool > Macro > Stop Recording. Once you do this your personal.xls macro workbook will be created – ridiculously simple in fact.

If you’re prompted to save the file when you close Excel answer Yes to do so. Excel will save it in a location that ensures it will be opened automatically every time you open Excel. In future, store all macros that you want to be accessible to all workbooks in this file and you won’t ever have to load them specially.

Helen Bradley