Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 读取动态用户表单复选框并填充数组_Excel_Vba_Userform - Fatal编程技术网

Excel 读取动态用户表单复选框并填充数组

Excel 读取动态用户表单复选框并填充数组,excel,vba,userform,Excel,Vba,Userform,我有一个动态填充的用户表单- For Each J In Temp Set Cbx = UserForm1.Controls.Add("Forms.CheckBox.1") Cbx.Caption = J Cbx.Left = 15 Cbx.Top = 15 + (17.5 * (Cntr - 1)) Cntr = Cntr + 1 Next J Cntr = 15 + (17.5 * (Cntr - 1)) + 50 UserForm1.Comman

我有一个动态填充的用户表单-

For Each J In Temp
    Set Cbx = UserForm1.Controls.Add("Forms.CheckBox.1")
    Cbx.Caption = J
    Cbx.Left = 15
    Cbx.Top = 15 + (17.5 * (Cntr - 1))
    Cntr = Cntr + 1
Next J

Cntr = 15 + (17.5 * (Cntr - 1)) + 50

UserForm1.CommandButton1.Top = Cntr - 35
我还有一个命令按钮,可以从代码段的最后一行看到

当我单击命令按钮时,我想用选中复选框的
标题
填充一个数组

Private Sub CommandButton1_Click()
    Call Summary
End Sub

Sub Summary()

    Dim Num As Integer
    Dim I As Integer
    Dim FltrTypes() As Double

    Num = UserForm1.Controls.Count - 1

    ReDim Preserve FltrTypes(0)

    For I = 0 To (Num - 1)
        If Left(UserForm1.Controls(I).Name, 8) = "CheckBox" Then
            If UserForm1.Controls(I).Value = "True" Then
                FltrTypes(I) = UserForm1.Controls(I).Caption
            End If
        End If
    Next I

End Sub
但是数组没有填充。我哪里出错了?

问题是:

If UserForm1.Controls(I).Value = "True" Then
无报价

If UserForm1.Controls(I).Value = True Then
FltrTpye
必须是
String
而不是
Double
但是有很多小错误,代码重写如下:

Dim Num As Integer
Dim I As Integer
Dim FltrTypes() As String
Dim NFltrTypes As Long

Num = UserForm1.Controls.Count - 1

Erase FltrTypes
NFltrTypes = 0
For I = 0 To (Num - 1)
    If Left(UserForm1.Controls(I).Name, 8) = "CheckBox" Then
        If UserForm1.Controls(I).Value = True Then
            ReDim Preserve FltrTypes(NFltrTypes)
            NFltrTypes = NFltrTypes + 1
            FltrTypes(NFltrTypes - 1) = UserForm1.Controls(I).Caption
        End If
    End If
Next I

ReDim Preserve FltrTypes(0)
不应该类似于
ReDim Preserve FltrTypes(Num)
?@Marcucciboy2数组仍然没有填充。调试时,它是否将任何内容填充到数组中?您是否使用“局部变量”窗口/断点进行检查?@Marcucciboy2是的,我使用的是局部变量和断点,这就是我如何看到数组FltrTypes是{0,0,0..}。是否filtertypes是双精度的而不是字符串?不确定您的标题实际上是什么类型的