gpt4 book ai didi

vba - 在引用另一个动态创建的控件的类中存储在集合中的动态创建的控件时出现“需要对象”错误

转载 作者:行者123 更新时间:2023-12-02 11:03:54 26 4
gpt4 key购买 nike

我正在使用旋转按钮来循环浏览一个阶段的日期。当我使用其索引值从名为 customtextboxcollection 的集合中调用一个项目时,我收到“需要对象”错误。旋转按钮和值更改的文本框都是动态创建的控件,显示在名为 UserForm1 的用户窗体上。

在单击旋转按钮之前运行在 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

为什么此引用会产生错误?引用它的正确方法是什么?

最佳答案

这不是您真正问题的答案,而是对可能更易于管理的替代方法的建议。

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

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 的一个实例每对,并将这些实例保存在一个集合中。

关于vba - 在引用另一个动态创建的控件的类中存储在集合中的动态创建的控件时出现“需要对象”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24868180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com