Home > Macros > Basics > Record Record and Test an Excel MacroHow to record and test a short macro. This tutorial is for absolute beginners, and will help you get started with Excel programming |
IntroductionIf you don't know anything about Excel VBA, where should you start? In this tutorial you'll take these first steps in using Excel VBA:
Watch this short video to see the steps, and the written instructions are below the video. NOTE: There's another video below, that shows how to record and run a simple macro, to format an Excel file. |
Identify an Excel Task to AutomateIf you use Excel every day, you probably have a few tasks that you repeat daily, weekly or monthly. To get started with Excel VBA programming language, you could focus on one of those tasks, and try to automate it. In this example, you have a list of stationery orders, in a workbook named Orders.xlsx. You can download the sample Orders file, or use a file of your own. Every day, in your imaginary job, you open that Orders file and filter the list of orders, to find all the orders for binders. Then you copy the orders, and paste them into a new workbook. Here's a summary of the steps that you follow every morning:
Instead of doing that task manually every day, you could automate it, by creating an Excel macro. |
Start RecordingOnce everything is in position, you can get ready to start recording.
|
Run the Recorded MacroNow, follow these steps to run the macro, to see if it works the way you want.
If your recorded macro work as expected, great! You can close all 3 workbooks used by the macro, without saving the changes. But, if you see an error message, click the End button, and try recording the macro again. Maybe it will work this time. Or, go to the Edit a Recorded Macro section below, and see if your macro code needs a minor change or two. |
Create a Macro ShortcutTo make it easier to run a macro, you can create a keyboard shortcut for it.
|
Open the Macro WorkbookWhen you recorded the macro, you selected a workbook where you wanted to store the macro. If that workbook has been closed, follow these steps to open it again:
Find the Excel VBA CodeNext, follow these steps, to go to the recorded code.
|
Excel Visual Basic Editor
In the Code Window, you can edit the text, just as you would in Microsoft Word, or Notepad. In the Project Explorer Window, you can select an object and see any code that it contains. In the screen shot above, Module 1 is highlighted, in the VBA Project for our workbook, named MacroCopyProduct.xlsm. |
Check the Recorded CodeThe Excel Macro Recorder created some code, while we performed the steps in our process. In my example, these were the steps:
Here’s how those steps look, when written in Excel VBA by the Macro Recorder. |
Change the Recorded CodeThe Excel Macro Recorder is a great tool for getting started with Excel VBA. Sometimes you can leave the code exactly as is, and it will run fine every time you need it. Most times though, the recorded code needs to be modified, and we’ll start with a simple change. When recording the code, I selected a specific range, “A1:J50”, which is used in two lines of the code. If new rows of data are added, the code won’t include them. To accommodate for an increase in rows, we could change the 50 to 500. Then, if rows are added, they’ll be included in the filter. There are more sophisticated ways to deal with a range that changes size, but this works for now. |
Test the ChangesAfter you have changed the recorded code, follow the steps below, to close the VBE. Use the Menu:
OR, use the shortcut
Try the Macro AgainAfter the VBE is closed, run the macro again, to test the changed code.
If the revised macro worked well, you can save the workbook that stores the macro. But, if there are still problems with the macro:
|
Record Macro to Format Excel FileHere's another video that shows how to record and run a simple macro in Excel, to automate the task of formatting an Excel file every day. I made this video because one of my clients was tired of manually formatting a daily list of customer information, and asked me for help. So, I set up a workbook with fake data, that was similar to the Excel file setup that she was formatting. Next, I made this video, to show her how to record an Excel macro, while she did the daily steps. The video has a few Excel tips too, like using the F4 key to repeat the last action, and adding a button to the Quick Access Toolbar, so it's easy to run a macro. |
Get the Workbook
More TutorialsExcel VBA - Add Code to Workbook Show Messages with Excel Macros |
Last updated: March 20, 2023 4:07 PM