您好,感谢您阅读:这是来自 OZGRID 的交叉帖子:https ://www.ozgrid.com/forum/index.php?thread/1227932-dynamic-created-combo-box-working-on-one -form-but-not-other-class-module-issue/ 我还没有运气,一旦在这里或那里解决就会更新。
我有两个表单,其中包含动态创建的组合框,试图引用同一个类模块。
第一个按预期工作,我在新表单上看不到我的代码问题。
类模块代码在这里:
Option Explicit
Private WithEvents m_ComboBoxEvents As MSForms.ComboBox
Public Property Set Box(RHS As MSForms.ComboBox)
Set m_ComboBoxEvents = RHS
End Property
Private Sub m_ComboBoxEvents_Change()
If REACTIONS.Visible = True Then
Debug.Print "hELlO"
'\\ WHEN COMBOBOX CHANGES ON REACTION FORM IT PASSES THE CAPTION INFORMATION FOR THE BUTTONS.
If Len(Box.Name) = 7 Then REACTIONS.Frame20.Controls("MyNCBtn" & Right(Box.Name, 1)).Caption = Box.List(Box.ListIndex, 1) ' will only work until 9
If Len(Box.Name) = 8 Then REACTIONS.Frame20.Controls("MyNCBtn" & Right(Box.Name, 2)).Caption = Box.List(Box.ListIndex, 1) ' will work until 99
End If
Debug.Print "Hi"
If UFSTDRDS.Visible = True Then
MsgBox ("Hello")
End If
End Sub
Public Property Get Box() As MSForms.ComboBox
Set Box = m_ComboBoxEvents
End Property
表格代码在这里。
Sub Not_Working()
Dim clsComboBox As New cComboBox
Dim MyCBx As MSForms.ComboBox
Dim m_colComboBoxEvents As Collection ' If I remove this line I get variable undefined error in next line, but it is not required in the working code above
Set m_colComboBoxEvents = New Collection
Loop starts here
Set MyCBx = Me.MultiPage1.Pages(2).Controls.Add("Forms.ComboBox.1", "MyNCBox" & x, 1)
Dim MyCBxfill As Variant
MyCBxfill = ws1.Range("A1").CurrentRegion
With MyCBx
.Top = 280 + ((x - 1) * 30)
.Left = 250
.Width = 350
.Height = 20
.FontSize = 8
.FontName = "Times New Roman"
.ColumnCount = UBound(MyCBxfill, 2)
'.ColumnWidths = ",0"
.List = MyCBxfill
End With
Set clsComboBox = New cComboBox
Set clsComboBox.Box = MyCBx
m_colComboBoxEvents.Add clsComboBox, CStr(m_colComboBoxEvents.Count + 1)
Loop ends here
End Sub
如果您在 ozgrid 网站上滚动,有一个名为 test.xlsb 的示例(注意有几个文本交换,但没有人提供任何解决方案),那里还有更多细节,但我认为我包含太多该帖子的详细信息。出于某种原因,这门课不适合我。
我不介意全新的方法或替代语法。我只是无法弄清楚为什么它在原始上下文而不是新上下文中有效
对现有代码的任何见解和改进将不胜感激。
问候
贾斯汀
由 Rory @ Ozgrid.com 解决,详情如下:
我没有看过你的工作簿,但是第二个代码中你的集合声明是在错误的地方。这一行:
需要在模块级别。否则,一旦你的例程完成,变量就会被清除,并且没有你的类的实例。