VBA Macros in Excel: How to Record, Edit and Run Them

record, vba, macros, excel

With VBA macros, you can do many things which aren’t possible using the build in Excel functions. You can save a lot of time by using macros, especially with repeating tasks. Let’s have a look at the necessary steps in order to record a VBA macro.

Preparations

Before we start to record VBA macros, you might have to do some preparations first.

Add the Developer ribbon

show, developer, ribbon, excel, vba, macro, vba macros
By default, Excel doesn’t show the Developer ribbon. You have to activate it within the Customize Ribbon menu first.

Before you start, you need to add the Developer ribbon. If you can see a Developer ribbon already, you can skip this step.

  1. Right click on any ribbon.
  2. Then click on “Customize the Ribbon…”.
  3. On the right hand side, set the tick at “Developer”.

That’s it, now you should see the Developer ribbon. Usually it’s located next to “View”.

How to insert a new VBA module manually

insert, vba, module, excel
Insert a new VBA module

If you don’t want to record a VBA macro, but start right away with programming it (or pasting the code from the internet…), you can insert a module easily by yourself:

  1. Open the VBA editor by clicking on the “Visual Basic” button on the left side of the Developer ribbon.
  2. Right click on “VBAProject(…your workbook name…)”.
  3. Click on Insert and then on “Module”.

Now you’ve created a new Excel module. You can just paste your code here or start typing.

How to record VBA macros

vba, macro, record, program, edit, excel
Steps for recording VBA macros

Let’s face is: creating VBA macros is real programming. Fortunately Excel provides an easy starting point: Record what you are doing and Excel can repeat it. In the background, the source code is automatically created. You start recording, do your steps and stop recording. The following numbers are corresponding to the picture on the right hand side:

  1. Press on “Record Macro” on the Developer ribbon. If you can’t see the Developer ribbon, right click on any button on any ribbon and click on ‘Customize the Ribbon’. Make sure that ‘Developer’ got a tick mark on the right hand side.
  2. Give a name to the macro and confirm with “OK”.
  3. Do the edits to your file which you want Excel to repeat. Click “Stop” when you are done.
  4. Modify the macro: Usually you’d want to make the source code work universally, for example, on each sheet. Therefore, you have to edit the recorded macro by opening Visual Basic. And that’s when it gets more difficult. But for now we want to concentrate on how to record a VBA macro.

Please note that you have to save your file as a macro enabled file in order to save your recorded VBA macro as well. Therefore press F12 (‘Save as’) and select ‘Excel Macro-Enabled Workbook’ as the file type.


Do you want to boost your productivity in Excel?

Get the Professor Excel ribbon!

Add more than 120 great features to Excel!


Example: How to edit a recorded VBA macro

vba, macro, example, excel, vba macro
Example: You want to copy the VBA worksheet 6 times and change the value in cell B5.

Let’s assume the following situation: You prepared an Excel sheet. Now you want to copy it and change one value. How do you start?

  1. Press “Record Macro” on the Developer ribbon. Confirm with OK.
  2. Do the action. In your case: Copy the worksheet.
    1. Therefore, right click on the worksheet name on the bottom of the screen.
    2. Click on “Move or Copy”. Set the tick at “Create a copy” and press OK.
    3. Change the value in cellB5
  3. Press the square stop icon at the bottom left corner of the screen or alternatively click on “Stop recording” on the Developer ribbon.
  4. Open the recorded code by clicking on the Visual Basic button on the left hand side of the Developer ribbon.

The recorded code should look similar like this.

Sub Macro1()
'
' Macro1 Macro
'

'
    Sheets("VBA").Select
    Sheets("VBA").Copy After:=Sheets(28)
    ActiveCell.FormulaR1C1 = "1/5/2017"
    Range("B6").Select
End Sub

Let’s first explore the code step by step.  Therefore, we’ve added comments to the code below:

Sub Macro1() 'This is the name you entered when the recording started.
' Comments - starting with the abostroph - won't be recarded as code.
' Macro1 Macro
'

'
    Sheets("VBA").Select
    'The worksheet "VBA" will be selected. You can of course change "VBA" to any other worksheet name. Only condition: The worksheet should exist in your Excel workbook.
    'This line can also be deleted when you later on edit the code.    

    Sheets("VBA").Copy After:=Sheets(28)
    'This line copies the worksheet "VBA" and puts it after the 28th worksheet in the current workbook.

    ActiveCell.FormulaR1C1 = "1/5/2017"
    'The selected cell (in our case it's cell B5) will be overwritten with by the date "1/5/2017".

    Range("B6").Select
    'The cell B6 is selected. This line can also be deleted.

End Sub

Now the easiest method is to copy this code and adapt it to your needs. In total, we want to create copies for each date given in our table. (Please note: advanced users would use loops; in order to keep it simple we copy and paste the code above several times).

Sub Macro1()
'
' Macro1 Macro
'

'
    Sheets("VBA").Select
    Sheets("VBA").Copy After:=Sheets(28)
    ActiveCell.FormulaR1C1 = "1/2/2017"
    Range("B6").Select
    
    Sheets("VBA").Select
    Sheets("VBA").Copy After:=Sheets(28)
    ActiveCell.FormulaR1C1 = "1/3/2017"
    Range("B6").Select
    
    Sheets("VBA").Select
    Sheets("VBA").Copy After:=Sheets(28)
    ActiveCell.FormulaR1C1 = "1/4/2017"
    Range("B6").Select
    
    Sheets("VBA").Select
    Sheets("VBA").Copy After:=Sheets(28)
    ActiveCell.FormulaR1C1 = "1/5/2017"
    Range("B6").Select
    
    Sheets("VBA").Select
    Sheets("VBA").Copy After:=Sheets(28)
    ActiveCell.FormulaR1C1 = "1/6/2017"
    Range("B6").Select
    
    Sheets("VBA").Select
    Sheets("VBA").Copy After:=Sheets(28)
    ActiveCell.FormulaR1C1 = "1/7/2017"
    Range("B6").Select
End Sub

How to run your VBA macro

button, excel, connect, vba macro, run, execute
Insert a button and connect it with your VBA macro. Every time you press the button, the code will be executed.

When you’re done modifying your macro, you can probably want to run it. Advice: Save your Excel file first. In case something goes wrong you can easily restore it.

There are two ways of running your VBA macro:

  1. In the Visual Basic editor: Place the cursor within the code you want to run. Click the small play button on the top.
  2. A more user friendly way would be adding a button for starting the macro: For adding a button, click on “Insert” on the Developer ribbon and choose the button on the top left corner under Form Controls. Excel then asks you which macro to assign to the button. Select your recorded VBA macro and confirm with OK.

Next steps

VBA macros are very powerful. Almost everything can be done with them. Also our Excel add-in “Professor Excel Tools” is programmed with VBA macros. If you want to practice or learn a little bit more, we recommend our other articles:

Henrik Schiffner is a freelance business consultant and software developer. He lives and works in Hamburg, Germany. Besides being an Excel enthusiast he loves photography and sports.

2 comments

  1. I usually record a macro and then edit it and paste it into a button or activeX and I noted that when I run the recorded macro it works, but if I copy and paste it into the vba editor and then run the step by step with F8 to see it do what I expected to do, some time it won’t.
    Same code, this is an example:

    Sheets("Incidents").Select
    Columns("A:U").Select
    Selection.ClearContents
    
    Sheets("Response").Select
    Columns("A:H").Select
    Selection.ClearContents
    Range("A1").Select
    
    Sheets("Resolution").Select
    Columns("A:H").Select
    Selection.ClearContents
    Range("A1").Select
    

    If run as a recorded macro, it clears the contents as expected, but if run into an active x object, “Columns(“A:H”).Select” won’t select the columns.

  2. Hi,

    I need your help to create macro. My requirement is given below.

    Eg: By using macro open google website enter Text1 on search bar, and again clear the search bar and enter Text2… so on Textn

    Note: The value of Text 1, Text2… Textn has been taken from the excel sheet.

Leave a comment

Your email address will not be published. Required fields are marked *