Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

vba - Differences between Excel's Form Controls & ActiveX Controls

Why are there 2 types of controls available in Excel? (2 buttons, 2 combo boxes, 2 check box, etc...)

What's the difference between Forms Controls and ActiveX Controls? Which one should I use?

image

Some code samples I find online work with my controls, but others do not. How come?

How do I work with each type, and how can I tell the difference?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is [eternal] confusion surrounding the two types of controls available to Excel — exacerbated by the contrasting terminology used by different online sources.

This is only a general overview of the differences between Form Controls and ActiveX Controls (based on my old notes that helped me finally figure out the differences!) Visit the included links for more detailed information about each control including code and sample projects. ?

(Adapted from my original post here — now closed)


Overview:

  • There are two types of controls: Form controls and ActiveX controls:

    • Both types of controls can be used on worksheets but only ActiveX controls can be used on userforms.

    • Form controls are part of the Shapes collection (just like Drawing Objects), and thus are referred to like: *<code>worksheet</code>* **<code>.</code> <code>Shapes("</code>** *<code>controlname</code>* **<code>")</code>**

    • ActiveX controls are basically part of the worksheet and are therefore referred to like:
      *<code>worksheet</code>* **<code>.</code>** *<code>controlname</code>*

    • Both types of controls can be created, modified and deleted from either the worksheet, or programmatically with VBA, however, the 2 types of controls have slightly varying syntax when using VBA to refer to them.

  • Some sites discuss also discuss a Data Form. This is nothing more than a userform made specifically for data entry/manipulation of data, so it would've made more sense to call them (the more familiar sounding) "Data Entry Userform".

  • Office documentation also occasionally refers to a worksheet as a form. While this is technically correct, don't let this confuse you. Think of the word "form" as being used in a general sense:

    dictionary image: form


Two Types of Controls

  1. Form Controls

  2. ActiveX Controls

The two look, behave, and are controlled similarly, but not identically. (List here.)

For example, let's compare the two types of Combo Boxes. In some programming languages, comparable controls are referred to as a "drop-down menu" or "drop-down list". In Excel, we have a "Form Control Combo Box", and an "ActiveX Control Combo Box":

compare control types (Click image to enlarge.)

"Default name" applies to controls created manually. Controls created programmatically do not have (or require) a default name and therefore should have one assigned immediately upon creation.


differences between controls

(Source: my answer)


Control Availability

This image below shows which common controls are generally available for use in Worksheets and User Forms.

chart of controls

Other factors may affect control availability.


About ActiveX controls and related Security Concerns

An ActiveX control is an extension to the VBA Toolbox. You use ActiveX controls just as you would any of the standard built-in controls, such as the CheckBox control. When you add an ActiveX control to an application, it becomes part of the development and run-time environment and provides new functionality for your application.

  • An ActiveX control is implemented as an in-process server (typically a small object) that can be used in any OLE container. Note that the full functionality of an ActiveX control is available only when used within an OLE container designed to be aware of ActiveX controls.

  • This container type, called a control container or control object, can operate an ActiveX control by using the control’s properties and methods, and receives notifications from the ActiveX control in the form of events. The following figure demonstrates this interaction:

    example
    (Source: this and this)

See also:


Option Buttons (Radio Buttons)

In Excel, the two types of radio buttons are actually called Option Buttons. To further confuse matters:

  • the default name for the form control is OptionButton1.

  • the default name for the ActiveX control is Option Button 1.

A good way to distinguish them is by opening the control's Properties list (on the ribbon under the Development tab, or by right-clicking the control and choosing Properties, or hitting F4), because the ActiveX control has many more options that the simpler form control.

Option buttons and checkboxes can be bound together (so only one option at a time can be selected from the group) by placing them in a shared Group Box.

Select the group box control and then hold Ctrl while selecting each of the other controls that you want to group. Right-click the group box control and choose GroupingGroup.

The first two links below are separate sets of instructions for handling each type of option button.


HANDLING CONTROL EVENTS:

Form control events (Click event only)

Form control events are only able to respond to one event: the Click event. (More info here.) Note that this section doesn't apply to userforms since they use only ActiveX controls.

To add a procedure for the Click event:

  • Right-click the control and choose Assign Macro...

  • In the 'Assign Macro` Dialog:

    • Select an existing procedure, and click OK, or,

    • Create a new procedure in the VBE by clicking New..., or,

    • Record a new macro by clicking Record..., or,

    • to Remove the assigned event, delete its name from Macro Name field and click OK.

    assign events (Click image to enlarge.)

To rename, edit or delete existing macros, hit Alt+F8 to open the Macro interface:

Macros dialog


ActiveX control events

ActiveX controls have a more extensive list of events to which they can be set up to respond.

To assign events to ActiveX controls, right-click the control and choose View Code. In the VBE, you can paste in code, or choose specific events from the drop-down list at the top-right of the VBE window.

activeX events (Click image to enlarge.)

Control event handling on a userform:

Events can also be used in controls on userforms. Since only ActiveX controls can be placed a userform, you'll have the "more coding + more functionality" trade-off.

ActiveX controls are added to userforms the same way as they are added to a worksheet. Keep in mind that any events assigned to the userform itself (ie., background) will be "blocked" in any areas covered up by a control, so you may need to assign the same events to the controls as well as the userform.

For example, in order to make this userform respond to MouseMove anywhere on the form, the same event code was applied to the userform, textboxes, option buttons and the frame:

A userform with controls responding to events


VBA EXAMPLES

Add/Modify/Delete a form control option button using VBA:

Sub formControl_add()
    'create form control
    Dim ws As Worksheet: Set ws = ActiveSheet
    With ws.Shapes.AddFormControl(xlOptionButton, 25, 25, 100, 100)
        .Name = "cOptionButton1"  'name control immediately (so we can find it later)
    End With
End Sub

Sub formControl_modify()
    'modify form control's properties
    Dim ws As Worksheet: Set ws = ActiveSheet
    ws.Shapes("cOptionB

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...