VBA Controls – Form Control & ActiveX Control in Excel

 

VBA Controls – Form Control & ActiveX Control in Excel




VBA Controls – Form Control & ActiveX Control in Excel


VBA Controls: VBA Form Control & ActiveX Controls in Excel

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 Excel VBA controls can be used in a Form. The following table shows some of the most commonly used GUI VBA controls.


In the VBA GUI control,

  1. Click on the developer tab
  2. Click on Insert Drop down button

You will get the following drop down panel

VBA Operators

If you hover the mouse over VBA form controls, the name of the control will appear as shown below

VBA Operators

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

VBA Operators

Setting GUI control properties

We will now set the caption of the button to something more descriptive

VBA Operators

  1. Right click on the equal button that we have just created
  2. Select properties menu
  3. You will get the following properties window

VBA Operators

  • 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.

VBA Operators

How to use ActiveX control in VBA

In this section of ActiveX control Excel, 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.

What is VBA?

Step 2) To insert “clickcommandbutton1” drag the mouse cursor to Excel sheet.

What is VBA?

Step 3) Then right click on the command button and select option “View Code”.

What is VBA?

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.”

What is VBA?

Step 5) In next step, save code file and then exit the editor. To return to the Excel file click the Excel sheet icon What is VBA? on the extreme left.

What is VBA?

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.

What is VBA?

Step 7) When design mode is off, there will be no green highlight on it. Now you can Click on the command button.

What is VBA?


Step 8) 
Click on “CommandButton1”. It will print “Guru99 VBA Tutorial” in the cell range “A1 to A10”.

What is VBA?

Prerequisite

Configure Microsoft Excel

As we see in previous tutorial, make sure your ribbon will have “Developer” icon as shown here.

VBA Operators

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).

VBA Operators

After that, click on Save button.


No comments

Theme images by johnwoodcock. Powered by Blogger.