Vba button control in Excel
Button control is used to perform some action on the click of the button.
Button controls are the most used control in any GUI, as without it, the GUI will be meaningless.
A Buttton control can display both Text and Images.
Most of the applications now use images instead of plain text inside the button to give a visual appeal to the end user.
VBA Button Properties
1.Text Property
This property is used to set the text to be displayed in the button control.
If the text exceeds the width of the control, then it is automatically wrapped.
2.Name Property
This property is used to set the name of the button.
This name is used in the program to refer to the button for any action.
3.Autosize
This property takes a binary value, either True or False.
This property is used to tell the program whether to resize the button accordingly.
4.BackColor Property:
This property is used to set the color of the button control.
5.Image Property
This property is used to set the image to be displayed in the button control.
Apart from properties, there are various events associated with the button control.
But, VBA Click event is the one that is almost always used.
The following illustrates the click event.
The code within this event will be executed whenever the button is clicked.
The following example illustrates this:
- Private Sub CommandButton1_Click()
- MsgBox "I am Clicked!!"
- End Sub