Lesson 2 : Record Your First Macro And Run It

How to record your macro

The Macro Recorder is a very useful tool when you want to automate a specific task, but don't know how to program it in Excel VBA. 

It works like any audio/video recorder. 
When you start the macro recorder anything you do in Excel is recorded as a new macro.

Task 
To automate multiplication in excel
In column D, multiply column C values by 2

How to record a macro?

Follow the steps shown in the animation below -




Instructions :
  1. On the Developer tab, click Record Macro
  2. Enter a name for the macro and assign shortcut key
  3. Click OK to start recording
  4. Perform the actions that you want to record
  5. To stop recording, go to Developer tab >> click Stop Recording


Optional Settings: You need not enter shortcut key and description to run your macro. 


After stopping macro recording, open Visul Basic Editor (ALT +  F11) to view the recorded code.

The recorded code for the above task is as follows :
Sub Macro1()
'
' Macro1 Macro
'
    Range("D3").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]*2"
    Range("D3").Select
    Selection.AutoFill Destination:=Range("D3:D12")
    Range("D3:D12").Select
End Sub



How to run excel macros?

  1. Press Alt+F8 to open the “Macro” dialog box.
  2. Select the desired macro from the “Macro Name” list and click the “Run” button.

Benefits of using Macro Recorder
  1. You can program a specific task without writing a single line of code yourself
  2. You can learn the VBA language by studying the code written by the Macro recorder
  3. You can assign a keyboard shortcut to your recorded macro
Disadvantages of using Macro Recorder
  1. It does not create very efficient code. It is because most of these steps are not required when writing efficient VBA code.
  2. It creates codes for a static range (fixed range). In real-life scenarios, we generally need to write a program for dynamic range.


Insert a button to run macro

Follow the steps shown in the animation below -


















Instructions :
  1. On Developer tab, click the Insert button and select the first option under the Form Controls
  2. Draw a button >> the Assign Macro dialog box pops up
  3. Select a macro you want to assign to the button
  4. Click OK

Format the macro button : Right click on the button >> Edit Text

How to modify the recorded macro

Macro recorder does not create very efficient code. You can remove the words like: Select, Scroll.
Rule of Thumb : You can remove the words like: Select, Scroll in your recorded macro code.
Example :



Previous: Getting Started with Excel VBA          Next : 35 Macros For VBA Beginners 

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has close to 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like retail and commercial banking, Telecom, HR and Automotive.


While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

1 Response to "Lesson 2 : Record Your First Macro And Run It"

  1. Great way to learn VBA As a beginner . thank you

    ReplyDelete

Next → ← Prev