CommandButton is used to run or execute a macro or procedure. It performs a task or an action when a user clicks on a command button. Command Button can be used on the WorkSheet. Please find more details about ActiveX CommandButton Control in the following chapter. You can see how we are adding or deleting command button on the Worksheet.
VBA ActiveX CommandButton Control on the Worksheet
Please find more details about VBA ActiveX Command Button Control on the Worksheet.
- Go To Developer Tab and then click Insert from the Controls group.
- Click on the Command Button from the ActiveX Controls group.
- Drag a Command Button on the Worksheet.
- Right click on the Command Button, before that Design Mode should be turned ON.
- Click on properties from the available list.
- On the left side find ‘Caption’ from the available properties of a Command Button .
- On the right side mention as ‘Click’. Please find the below screen shot for the same.
- Now, Double click on the Command Button (Click). Or right click on command button and click view code.
- Now you can see the following code in the VBA Editor window.
- Now add the following code in between above procedure.
- Now, Press ‘F5’ to run the code. You can see the following Output. It is shown in the following Screen Shot.
Private Sub CommandButton21_Click() End Sub
Code:
Private Sub CommandButton21_Click() MsgBox "Hi, Welcome to Analysistabs" End Sub
output:
Delete CommandButton Control on the Worksheet using VBA
Please find the below code, it will show you how to delete or remove a command button on the worksheet. In the below example, its deleting the command button named ‘CommandButton1’ which is on the worksheet named ‘Sheet11’. Here Delete is the property of Shape object.
Sub Delete_CommandButton() Sheets("Sheet11").Shapes("CommandButton1").Delete End Sub
A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.
LIMITED TIME OFFER
Advanced Project Planning Templates
Excel Templates
120+ Project Management Templates Pack
Excel | PowerPoint | Word
ULTIMATE RESOURCE MANAGEMENT TEMPLATE
Excel Template
50+ Essential Project Management Templates
Excel | PowerPoint | Word
Project Portfolio Management Templates
Excel | PowerPoint Templates
50+ Excel Project Management Templates
Excel Templates
Share This Story, Choose Your Platform!
4 Comments
Leave A Comment
You must be logged in to post a comment.
i want to copy file1 into sheet1 of the master file and file2 into sheet2 of the master file using command button macro
vba code. please how to write the code for that.
Thank you.
how to control a comment button with another one command button.
expl.
there is two command button in a sheet called
command button-1 and command button-2
command button-1 contains the following macro
Range(“A1”).Select
so I want to activate the command button-1 by pressing the command button-2
please give me a solution
Hi Kalidas,
Command button is having two properties to handle your requirement.
Method 1. Enabled property of Command button
Example:
Commandbutton1.Enabled=True ‘This will allow user to click on the button to execute the code inside the command button event.
Commandbutton1.Enabled=False ‘It will not execute the code even after clicking on it.
Method 2: Visible Property of the Command button
Commandbutton1.Visible=True ‘it will display the commandbutton
Commandbutton1.Visible=False’it will hide the commandbutton
Hope this helps!
Thanks-PNRao!
How can i use toggle button to delete rows and columns ?