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.
Insert a button to run macro
Follow the steps shown in the animation below -
Instructions :
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.
Previous: Getting Started with Excel VBA Next : 35 Macros For VBA Beginners
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
How to record a macro?
Follow the steps shown in the animation below -
Instructions :
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 :
How to run excel macros?
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 :
- On the Developer tab, click Record Macro
- Enter a name for the macro and assign shortcut key
- Click OK to start recording
- Perform the actions that you want to record
- 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?
- Press Alt+F8 to open the “Macro” dialog box.
- Select the desired macro from the “Macro Name” list and click the “Run” button.
Benefits of using Macro Recorder
- You can program a specific task without writing a single line of code yourself
- You can learn the VBA language by studying the code written by the Macro recorder
- You can assign a keyboard shortcut to your recorded macro
Disadvantages of using Macro Recorder
- It does not create very efficient code. It is because most of these steps are not required when writing efficient VBA code.
- 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 :
- On Developer tab, click the Insert button and select the first option under the Form Controls
- Draw a button >> the Assign Macro dialog box pops up
- Select a macro you want to assign to the button
- 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 :
Great way to learn VBA As a beginner . thank you
ReplyDelete