Vba ';所需对象';在另一个动态创建控件的类中引用存储在集合中的动态创建控件时出错

Vba ';所需对象';在另一个动态创建控件的类中引用存储在集合中的动态创建控件时出错,vba,collections,reference,compiler-errors,dynamically-generated,Vba,Collections,Reference,Compiler Errors,Dynamically Generated,我使用一个旋转按钮来循环一个阶段的日期。当我从名为customtextboxcollection的集合中调用一个项及其索引值时,我得到一个“Object Required”错误。自旋按钮和文本框(其值更改是动态创建的)都显示在名为UserForm1的UserForm上 单击旋转按钮之前,将运行用于在customtextbox集合中创建项的子项: Dim customtextboxcollection As Collection Dim spinbuttoncollection As Collec

我使用一个旋转按钮来循环一个阶段的日期。当我从名为customtextboxcollection的集合中调用一个项及其索引值时,我得到一个“Object Required”错误。自旋按钮和文本框(其值更改是动态创建的)都显示在名为UserForm1的UserForm上

单击旋转按钮之前,将运行用于在customtextbox集合中创建项的子项:

Dim customtextboxcollection As Collection
Dim spinbuttoncollection As Collection


Public Sub ComboBox1_Click() 'When a person is selected to enter hours for an employee from a combobox, it triggers the creation of the controls

Sheet1.Activate
CommandButton1.Enabled = True 'Enable the OK and Apply buttons when personnel title is selected.
UserForm1.Label2.Visible = True
UserForm1.ratebox.Visible = True
QuantityLabel.Visible = True
quantitybox.Visible = True

'The variables below are to access the table where I store saved information regarding the project phases I will add hours to.

Dim counter As Integer
counter = 6 'The index of the first row for phases
Dim phasecolumn As Integer
phasecolumn = 3 'The index of the column containing the phases
Dim checkboxnumber As Integer
checkboxnumber = 1 'This is the number needed to distinguish between the checkboxes that appear/disappear.
phasestartcolumn = 4
phaseendcolumn = 5
Dim customtextboxHandler As cCustomTextBoxHandler
Set customtextboxcollection = New Collection 'Sets the previously created collection

Dim spinbuttonHandler As cSpinButtonHandler 'This is my spin button handler class
Set spinbuttoncollection = New Collection 'Sets the previously created collection


'This Do-Loop locates a row on the table with saved information
Do
    If (Sheet3.Cells(savedpersonnelrow, savedpersonnelcolumn) = ComboBox1.Value) Then
        storagerow = savedpersonnelrow
        lastcomboboxvalue = ComboBox1.Value
        Exit Do
    End If

    savedpersonnelrow = savedpersonnelrow + 1

Loop Until (savedpersonnelrow = 82)

Sheet1.Activate

'These sections create the controls depending on the number of phases saved.

Set spin = UserForm1.Controls.Add("Forms.SpinButton.1")

        With spin
            .name = "SpinButton" & checkboxnumber
            .Left = 365
            .Top = topvalue + 6
            .Height = 15
            .Width = 40
            '.Value = Sheet3.Cells(storagerow, savedphasecolumn + checkboxnumber)
            'Sheet1.Activate


            Dim phasestart As Date
            phasestart = Sheet1.Cells(counter, phasestartcolumn).Value
            Dim phaseend As Date
            phaseend = Sheet1.Cells(counter, phaseendcolumn).Value

            spin.Min = phasestart
            spin.Max = phaseend
            spin.Orientation = fmOrientationVertical

            'Do

                '.AddItem Format(phasestart, "mmm-yy")
                'phasestart = DateAdd("m", 1, phasestart)

            'Loop Until (Month(phaseend) = Month(phasestart) And Year(phaseend) = Year(phasestart))

            Set spinbuttonHandler = New cSpinButtonHandler
            Set spinbuttonHandler.spin = spin
            spinbuttoncollection.Add spinbuttonHandler

        End With



Set ctext = UserForm1.Controls.Add("Forms.TextBox.1")

        With ctext
            .name = "CustomTextbox" & checkboxnumber
            .Left = 470
            .Top = topvalue + 6
            .Height = 15
            .Width = 40
            .Value = phasestart

            Set customtextboxHandler = New cCustomTextBoxHandler
            Set customtextboxHandler.ctext = ctext
            customtextboxcollection.Add customtextboxHandler

        End With

topvalue = topvalue + 15
counter = counter + 1
checkboxnumber = checkboxnumber + 1

Loop Until counter = 14

End Sub
在名为cSpinButtonHandler的类中,我引用了与相应的旋转按钮关联的customtextboxcollection对象:

Public WithEvents spin As MSForms.SpinButton


Private Sub spin_Click()

UserForm1.CommandButton3.Enabled = True


End Sub

Private Sub spin_SpinDown()

x = 0

Do
    x = x + 1

Loop Until spin.name = "SpinButton" & x

Dim spindate As Date
spindate = customtextboxcollection.Item(x).ctext.Value 'The error occurs here.

customtextboxcollection.Item(x).ctext.Value = DateAdd("m", -1, spindate)

End Sub

为什么此引用会生成错误?正确的参考方法是什么?

这不是对你真正问题的回答,而是对更容易管理的替代方法的建议


您可以创建一个类来处理每对控件(一个旋转和一个文本框),而不是使用两个单独的集合和两个不同的类。就每一对之间的挂钩事件而言,这将更容易处理

clsSpinText:

Option Explicit

Public WithEvents txtbox As MSForms.TextBox
Public WithEvents spinbutn As MSForms.SpinButton


Private Sub spinbutn_Change()
    'here you can refer directly to "txtbox"
End Sub

Private Sub txtbox_Change()
    'here you can refer directly to "spinbutn"
End Sub

添加控件时,每对控件创建一个
clsSpinText
实例,并将这些实例保存在一个集合中。

您可以创建一个类来处理每对控件(一个旋转和一个文本框),而不是使用两个单独的集合和两个不同的类。这会更容易处理,因为两人之间的勾结事件。谢谢你的回复,蒂姆。你能引用一个能帮助我浏览这个过程的资源吗?我已经将两个控件更改为同一个类,但仍然得到相同的错误。再次感谢您抽出时间。