- Click on the Developer tab View tab.
- Click on Insert and Click on Command Button in the ActiveX Controls section
This will create a command button within current sheet. Let’s now create a macro and assign it to this command button.
To write a macro within the command button, right click on command button and click view code
3. The Visual Basic Editor will be opened. Place you cursor between
Private Sub CommandButton1_Click()
4. For example, add the following code line:
Range(“A1”).Value = “My First Code !”
We just wrote one line code within the command button click event. So whenever you click this command button this code will be executed.
As per the written code, the command button will write a value in cell A1 as “My First Code !” as shown in the above screenshot.
You can use multiple command buttons to execute multiple repetitive tasks or create a complete form like interface within the sheet.
Thank you for reading. Happy Learning !