Getting Started with Excel VBA (X) User Forms development

VBA user form refers to the user interface with the UI, at run time will pop up a separate window, similar to the same (this statement is not rigorous an executable program that runs in the windows system, because the executable program may also It is the only command window without the UI). More specific point, which is a window interface, there is likely to contain text fields, check boxes, radio buttons, drop-down list, as fill in a form on the page the same. A little more specific, this is as shown below:

User Form Example

1. User Interface Design

As shown above, UI design part is actually very simple, Microsoft features, direct drag drag can be achieved. A control for setting each attribute (or referred to as elements, i.e. into the form of various buttons, input boxes, etc.), may be performed after select it in the lower left corner of the default "attribute" list box , item names may be provided comprising a button, foreground color, background color, text displays, width, height, font color, font size, the distance from the left side, like a distance from the top, it is very simple. If the "Properties" box does not appear, you can select "Properties window" on the top menu bar of the "view", it will appear.

2. User interaction

Button 2.1 event

Double-click directly in the form of a button into them, you can enter the event developed interface buttons, the default is to click event, as shown below

Pay attention to the figure above the red line part of the upper left corner shows the name of the button in the upper right corner shows the event of the button, here is the click event Click, both of which are drop-down selection box, click on each individual down to their right other methods can be seen that triangle further controls and controls each corresponding follows

If you selected other events, the VBE will automatically give you a signature generating function of this event, which is based on the button's name and the event name to name, so this should be a function name can not be altered (I have not tried to change)

Other buttons similar events, not go into details.

2.2 Gets the value of the control

Because a lot of controls, it is not one by one example, cite only a few examples as a tool, but also other similar, usually what it called in the properties list, to be able to get its value by name

Text Box

Suppose a text box named tbx, through tbx.Valueor tbx.Texthave access to the content into the text box

Checkboxes

Assume checkboxes named cbx, through cbx.Valuecan get the value of the radio button, if it is selected, the value is True, otherwise False. By cbx.Captionobtained this text corresponding checkboxes

single button

Assuming that the radio button named obtn, through obtn.Valuecan get the value of the radio button, if it is selected, the value is True, otherwise False. By cbx.Captionobtained this text corresponding checkboxes

3. All controls over calendar

For example, a form, there are a plurality of check boxes, one by one to be determined whether it is selected, this case may be considered epoch over all controls, to obtain control by determining whether the checkbox types.

Look at the following code:

orderStr = ""
For Each ctrls In Me.Controls
    If TypeName(ctrls) = "CheckBox" Then
        If ctrls.Value = True Then
            orderStr = orderStr & "||" & ctrls.Caption
        End If
    End If
Next ctrls

Me.ControlsIt is a fixed wording, Meon behalf of the current form, and Controlsthen all controls on the form. A TypeNamefunction to determine the type of each control, and can perform different operations for different controls the

4. dynamically generate control

I.e., no longer use artificial way to set the drag pull control, but to dynamically add control to a form in accordance with the condition code to VBA.

Look at the code:

For Each order In orderArr
    Set newCbk = form_combinedModel.Controls.Add("Forms.CheckBox.1")
    With newCbk
        .Left = 30
        .Top = y
        .Width = 80
        .Height = 18
        .Caption = order
    End With
    y = y + gap
    panelH = panelH + gap
Next order

Here orderArris an array, you can use For Eachhistory all over it. Focus on the second line, here form_combinedModelis a form of the name by which the .Controls.Addmethod can add new controls. The parameters of this method is fixed, what type of control needs to be added to use the corresponding parameter, the sample code is added to the check box, corresponds Forms.CheckBox.1, this parameter can here be found.

Furthermore, it should be noted that the newly added controls is an object, so it is necessary to use in front of the variable Setkeyword. The following sample code Withstatements are used to set the new properties of the control, where its position is provided (from the left, the pitch), the width, height, display text


Other articles in this series of tutorials

Excel VBA entry (zero)
Excel VBA entry (a) data type
Excel VBA entry (ii) arrays and dictionaries
Excel VBA entry (c) flow control conditions are selected 1-
Excel VBA entry (four) process control loop controls 2-
Excel VBA Getting (e) Excel object manipulation
Excel VBA entry (f) Procedures and functions
Excel VBA entry (seven) comments, macro buttons and error handling
(eight) cell borders Excel VBA Getting
Excel VBA entry (ix) operating worksheet
Excel VBA entry ( j) user Forms development

Guess you like

Origin www.cnblogs.com/wuzhiblog/p/vba_ten.html