简体   繁体   English

在VBA Excel中以编程方式将OptionButtons添加到Userform

[英]Adding OptionButtons to the Userform programatically in VBA Excel

I am very new to VBA programming. 我对VBA编程很新。 My scenario is I will get a list of String values I need these values to be displayed to the user using radio buttons on a small window so that whenever the user selects any value by clicking on the radio button I should be able to get that value in the VBA code. 我的场景是我将获得一个字符串值列表我需要使用小窗口上的单选按钮向用户显示这些值,以便每当用户通过单击单选按钮选择任何值时,我应该能够获得该值在VBA代码中。 I searched for adding options button in the user form in the internet I got some solution which use GUI method of creating option buttons. 我在互联网上搜索用户表单中的添加选项按钮我得到了一些使用GUI方法创建选项按钮的解决方案。 But I need it done through program. 但我需要通过程序完成它。 I found a helpful thread in stackoverflow ( How can I dynamically add a radio button on a form using VBA ) I used this but still I am unable to get any label or button on the user form, a plain userform will be displayed. 我在stackoverflow中找到了一个有用的线程( 如何使用VBA在表单上动态添加一个单选按钮 )我使用了这个,但我仍然无法在用户表单上获得任何标签或按钮,将显示一个普通的用户表单。 So anybody please give information regarding this. 所以任何人都请提供相关信息。

The code is : 代码是:

Sub Button1_Click()
    lResult As Variant    ' this is a array which contains string vaues to be dispayed as radio button.

    ' Some operatin is done here to get the list of values in lResult

    Dim rad As Variant
    Set rad = UserForm1.Controls.Add("Forms.OptionButton.1", "radioFoo", True)
    rad.Caption = "bar"
    rad.Left = 10
    rad.Width = 10
    rad.Top = 10
End Sub

UserForm1 is the userform which I created using Insert option in VBA menu bar. UserForm1是我在VBA菜单栏中使用“插入”选项创建的用户窗体。 I tried to add a single button on the userform. 我试图在userform上添加一个按钮。 I did not use initialize function on userform. 我没有在userform上使用initialize函数。 There is button on excel sheet Button1 I am calling this function on clicking that button. excel工作表上有按钮Button1我点击该按钮调用此功能。

Thank you 谢谢

If you have a form named UserForm1 that contains a button named CommandButton1 如果您有一个名为UserForm1的表单,其中包含一个名为CommandButton1

用户窗体



You can set the Initialize method for your UserForm to programmatically create a group of radio buttons 您可以为UserForm设置Initialize方法,以编程方式创建一组单选按钮

Private Sub UserForm_Initialize()
    Dim OptionList(1 To 3) As String
    Dim btn As CommandButton
    Set btn = UserForm1.CommandButton1
    Dim opt As Control
    Dim s As Variant
    Dim i As Integer

    OptionList(1) = "Option 1"
    OptionList(2) = "Option 2"
    OptionList(3) = "Option 3"

    For Each s In OptionList
        Set opt = UserForm1.Controls.Add("Forms.OptionButton.1", "radioBtn" & i, True)
        opt.Caption = s
        opt.Top = opt.Height * i
        opt.GroupName = "Options"

        UserForm1.Width = opt.Width
        UserForm1.Height = opt.Height * (i + 2)

        i = i + 1
    Next

    btn.Caption = "Submit"
    btn.Top = UserForm1.Height - btn.Height + (0.5 * opt.Height)
    btn.Left = (UserForm1.Width * 0.5) - (btn.Width * 0.5)

    UserForm1.Height = UserForm1.Height + btn.Height + (0.5 * opt.Height)
End Sub

Private Sub CommandButton1_Click()
    Dim i As Integer

    For i = 0 To UserForm1.Controls.Count - 1
        If UserForm1.Controls(i) Then
            SelectedOption = UserForm1.Controls(i).Caption
        End If
    Next

    UserForm1.Hide
End Sub



If you want to pull your list from a sheet you can change 如果您想从工作表中提取列表,则可以更改

Dim OptionList(1 To 3) As String

OptionList(1) = "Option 1"
OptionList(2) = "Option 2"
OptionList(3) = "Option 3"

to pull from a range like this 从这样的范围拉出来

Dim OptionList() as Variant
OptionList = Range("A1:A3")


In your "button_onclick()" procedure stored in a module add this code: 在存储在模块中的“button_onclick()”过程中添加以下代码:

'This is set by the code in UserForm1
Public SelectedOption As String

Sub Button1_OnClick()
    UserForm1.Show
    MsgBox SelectedOption
End Sub



Which gets you this result: 哪个得到了这个结果:

在此输入图像描述

And when you click submit a message box will pop up showing you which option was selected 当您单击“提交”时,将弹出一个消息框,显示您选择了哪个选项

在此输入图像描述

Remember in using option buttons, your option buttons need to share the same GroupName. 请记住,在使用选项按钮时,您的选项按钮需要共享相同的GroupName。
Your control Name is only there for you to refer it back for changing/reading. 您的控件名称仅供您参考,以便更改/阅读。
Your Caption is a string that appear on your userform to the users. 您的标题是在用户表单上显示给用户的字符串。
Your GroupName is a string that allows Excel to recognize the option buttons are linked together. GroupName是一个字符串,允许Excel识别链接在一起的选项按钮。

So, if opt1's GroupName is "1" while opt2's GroupName is "2", then you will be able to select both since they are in different Groups. 因此,如果opt1的GroupName为“1”而opt2的GroupName为“2”,那么您将能够选择它们,因为它们位于不同的组中。

Private Sub UserForm_Initialize()
    Dim opt1 As Control, opt2 As Control

    Set opt1 = UserForm1.Controls.Add("Forms.OptionButton.1", , True)
    With opt1
        .Name = "radioFoo"
        .GroupName = "1"
        .Caption = "Option 1"
    End With

    Set opt2 = UserForm1.Controls.Add("Forms.OptionButton.1", , True)
    With opt2
        .Name = "radioFoo2"
        .GroupName = "1"
        .Caption = "Option 2"
        .Left = 100
    End With

End Sub


EDIT: 编辑:
From seeing your edited post and your comment... 从查看您编辑的帖子和评论...
No, you don't need to have UserForm_Initialize() method. 不,您不需要具有UserForm_Initialize()方法。

It is an Excel-VBA functionality called Event . 它是一个名为Event的Excel-VBA功能。
What it's used for is specifying the userform to do something when userform is initialized (first started). 它的用途是指定userform在初始化userform(首次启动)时执行某些操作。
Similarly from your code, Button1_Click() is an event as well. 与您的代码类似,Button1_Click()也是一个事件。
Since you are telling Excel to do the following at the event where Button1 is clicked by the user... 由于您要告诉Excel在用户单击Button1的事件中执行以下操作...

Anyways, let me briefly explain to you what option buttons do. 无论如何,让我简单地向您解释一下按钮的功能。
A group of option buttons forces the user to select only one option out of options given by the program. 选项按钮强制用户从程序给出的选项中仅选择一个选项。
And an option button in VBA only allows you to create one option. VBA中的选项按钮仅允许您创建一个选项。 So, if you want to create 2 options, you must create 2 option buttons. 因此,如果要创建2个选项,则必须创建2个选项按钮。
But there is just one problem: what if you want to create 2 groups of option buttons so that the user can select 2 separate options? 但是只有一个问题:如果您想创建2组选项按钮,以便用户可以选择2个单独的选项,该怎么办? For example, food and drinks? 例如,食物和饮料?
VBA presents us a property of an option button called GroupName . VBA向我们展示了一个名为GroupName的选项按钮的属性。 GroupName allows VBA to distinguish between separate groups of option buttons. GroupName允许VBA区分不同的选项按钮组。
Therefore, in every option button you create, it is essential that you initialize its GroupName value. 因此,在您创建的每个选项按钮中,必须初始化其GroupName值。 If you see any implementation of option button without GroupName, you are playing with fire. 如果您看到没有GroupName的选项按钮的任何实现,那么您正在玩火。

So, let's finally take a look at your code: 那么,让我们来看看你的代码:

Sub Button1_Click()
    ' Some operatin is done here to get the list of values in lResult

    Dim rad1 As Control, rad2 As Control
    Set rad1 = UserForm1.Controls.Add("Forms.OptionButton.1", "radioFoo1", True)
    rad1.Caption = "bar"
    rad1.Left = 10
    rad1.Width = 10
    rad1.Top = 10
    rad1.GroupName = "Group1"

    Set rad2 = UserForm1.Controls.Add("Forms.OptionButton.1", "radioFoo2", True)
    rad2.Caption = "foo"
    rad2.Left = 10
    rad2.Width = 10
    rad2.Top = 50
    rad2.GroupName = "Group1"
End Sub

Just one thing: 就一件事:
- As I've implicitly mentioned before, an option button with only one option does not mean anything. - 正如我之前暗示过的那样,只有一个选项的选项按钮并不意味着什么。 If you are looking for on/off kind of functionality, you might as well go with checkbox. 如果您正在寻找开/关类型的功能,您可以选择复选框。
So, I've created another option button defining it to be in the same group as the first option button you've created (rad1). 因此,我创建了另一个选项按钮,将其定义为与您创建的第一个选项按钮(rad1)位于同一组中。

Hope it helps. 希望能帮助到你。

Cheers, 干杯,
kpark kpark

Be sure to select the best answer when the question/problem has been answered/solved. 当问题/问题得到解答/解决时,请务必选择最佳答案。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM