使用复选框创建数组

问题描述 投票:0回答:1

我试图这样做的背景。我正在创建一个工作程序,这是执行此过程的众多方法之一,用户选择他们已经执行的测试,使用我制作的用户表单进行大约50次可能的测试。从选择开始,它根据选中的复选框运行每个用户表单,而无需选择其他测试。

即如果他们选择1,2,5那么程序将最终加载UserForm 1 - > UserForm2 - > NOT UserForm 3 - > NOT UserForm 4 - > UserForm

这个想法是用户选择他们想要的选项,然后根据选择的选项创建一个0或0的数组,如果他们没有选择选项或(1,2,3,4 ...)如果他们这样做,这个数字取决于选中的复选框。即CheckBox1 = 1 CheckBox2 = 2等。

从这个数组我想我将能够使用带有for循环的excel中的.find功能选择正确的用户表单。

但是当我在下面运行我的代码时遇到问题,

Sub List_Create()

Dim tests(5) As Integer

        If CheckBox1.Value = True Then
        tests(0) = 1
        Else: tests(0) = 0
        End If
        If CheckBox2.Value = True Then
        tests(1) = 2
        Else: tests(1) = 0
        End If
        If CheckBox3.Value = True Then
        tests(2) = 3
        Else: tests(2)= 0
        End If
        If CheckBox4.Value = True Then
        tests(3) = 4
        Else: tests(3) = 0
        End If
        If CheckBox5.Value = True Then
        tests(4) = 5
        Else: tests(4) = 0
        End If

End Sub

我得到了

运行时424需要错误对象。

调试时哪个是在线If CheckBox1.Value = True Then

我的代码出了什么问题?这可能吗,我不确定它是不是?

arrays excel vba checkbox
1个回答
2
投票

您需要显式引用userform来评估复选框值。

 If userformX.CheckBox1.Value = True Then
© www.soinside.com 2019 - 2024. All rights reserved.