【问题标题】:VBA obtaining checkbox values from userformVBA从用户窗体获取复选框值
【发布时间】:2017-07-21 17:14:51
【问题描述】:

我无法从用户窗体的复选框中获取值。我遇到的问题是用户窗体根据工作表中的值创建可变数量的复选框。代码:

Private Sub UserForm_Initialize()

Dim LastRow As Long
Dim i As Long
Dim Teller As Long
Dim chkBox As MSForms.CheckBox

Teller = 1
LastRow = Worksheets("Sheet").Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To LastRow
    If Worksheets("Sheet").Cells(i, 1).Value = Worksheets("Sheet").Range("S1").Value Then
        Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & Teller)
        chkBox.Caption = Worksheets("Sheet").Cells(i, 9).Value
        chkBox.Left = 5
        chkBox.Top = 25 + ((Teller - 1) * 20)
        Teller = Teller + 1
    End If
Next i    
End Sub

所以这会创建许多名为 CheckBox_1、CheckBox_2 等的复选框。 问题是当我尝试在模块中获取 CheckBox_1 的值时,尚未创建 CheckBox_1,所以我无法使用它。

Dim x as String
With UserForm4
     .Show
     x = .CheckBox_1
     MsgBox (x)
     End
End With

【问题讨论】:

    标签: excel vba checkbox


    【解决方案1】:

    您需要遍历 .Controls 文本框不是表单上的属性。

    【讨论】:

    • 不会必然必须循环通过控件 - 如果 OP 知道他们想要第一个控件,x = .Controls("CheckBox_1").Value 将起作用。 (但显然x = .Controls("CheckBox_" & i).Value 如果确实需要循环,则可以使用。)
    猜你喜欢
    • 2012-09-04
    • 1970-01-01
    • 2015-01-17
    • 2023-03-10
    • 2018-09-13
    • 2017-04-03
    • 2020-10-09
    • 2014-11-10
    • 1970-01-01
    相关资源
    最近更新 更多