Creating VBA Form/GUI controls in Excel
GUI is the acronym for Graphical User Interface. The GUI is the part of the program that the user interacts with. A GUI is made up of controls. These controls can be used in a Form. The following table shows some of the most commonly used GUI controls in VBA.
|1||Command||Used to execute code|
|2||Combo Box||Used to present a drop down list to the users|
|3||CheckBox||Used for true or false values. Multiple check boxes can have true value at the same time.|
|4||List Box||Used to present a simple list to the users|
|5||Text Box||Used to accept user input|
|6||Scroll Bar||Used for to provide a scrollbar for navigation purposes.|
|7||Spin Button||Used to increment or decrease numeric values by clicking on up or down arrows that appear in the spin button|
|8||Option Button||Used for true or false values. Only one option button can have a true value at a time.|
|9||Label||Used to display static text to the user|
|10||Image||Used to display images|
|11||Toggle Button||Button that has pressed and un-pressed states.|
In the GUI control,
- Click on the developer tab
- Click on Insert Drop down button
You will get the following drop down panel
If you hover the mouse over control, the name of the control will appear as shown below
Adding GUI controls to a spreadsheet
We will now add a command button to our workbook, for that we have to
- Click on Insert drop down
- Click on Command Button (ActiveX Control) as shown in the image above
- Drag and draw the button on the sheet
Setting GUI control properties
We will now set the caption of the button to something more descriptive
- Right click on the equal button that we have just created
- Select properties menu
- You will get the following properties window
- Set the name property to btnButton. The name property is used to identify the control in the code window. btn is the prefix for the button.
- Set the Caption property to Click Me. The text in the caption property is what the users will see on the button.
Close the window when you are done.
You will get the following results.
How to use ActiveX control in VBA
In this section, we will see how to incorporate 'commandclick' button in VBA and execute a program using the button.
Step 1) In this step, click the option "insert button" from the Active X Control. Then select the command button option from it.
Step 2) To insert "clickcommandbutton1" drag the mouse cursor to Excel sheet.
Step 3) Then right click on the command button and select option "View Code".
Step 4) Check you are on the right sheet. A code editor will open. Enter your code.
- In code, we defined range A1 to A10.
- This range defines the cell number in the Excel sheet from "A1 to A10."
- Next, we define the string "Guru99 VBA Tutorial" in code
- So, when you click on "command button" in excel. It should print "Guru99 VBA Tutorial" in the cell range "A1 to A10."
Step 6) In Excel sheet, you will see Design Mode tab is on. Make sure it is "OFF" or else your code will not work.
Step 7) When design mode is off, there will be no green highlight on it. Now you can Click on the command button.
Step 8) Click on "CommandButton1". It will print "Guru99 VBA Tutorial" in the cell range "A1 to A10".
Configure Microsoft Excel
As we see in previous tutorial, make sure your ribbon will have "Developer" icon as shown here.
Now, rename sheet1 to "VBA Operator" as shown in screen-shot below. Save the workbook in an Excel Macro-Enabled Workbook (*.xlsm) file extension. (see the image below).
After that, click on Save button.