【发布时间】:2016-10-28 23:22:17
【问题描述】:
我正在尝试解决组合框的问题,我不希望列出的选项在每次执行宏时都创建另一个 double,如果 AddItem 属性仅用于填充组合框,则会发生这种情况.该项目是在用户表单的下拉菜单中列出 12 个月,目标是让用户在打印预览模式下显示该表(随附的工作表每个月都有一个表)。我为几个月编写了一个字符串数组,然后告诉它在循环中使用 AddItem 属性填充组合框。也就是说:
Private Sub ComboBox1_Change()
Dim strMonth(0 To 11) As String
strMonth(0) = "Print April Table"
strMonth(1) = "Print May Table"
strMonth(2) = "Print June Table"
strMonth(3) = "Print July Table"
strMonth(4) = "Print August Table"
strMonth(5) = "Print September Table"
strMonth(6) = "Print October Table"
strMonth(7) = "Print November Table"
strMonth(8) = "Print December Table"
strMonth(9) = "Print January Table"
strMonth(10) = "Print February Table"
strMonth(11) = "Print March Table"
Dim mthPosition As Long
For mthPosition = LBound(strMonth) To UBound(strMonth)
UserForm13.ComboBox1.AddItem strMonth(mthPosition)
Next mthPosition
With UserForm13.ComboBox1
.Style = fmStyleDropDownList
End With
UserForm13.Show
End Sub
由于某种原因,我在 AddItem 行收到一个错误,说 VBA 找不到指定的对象,即使指定了路径...如果代码在 ComboBox 的例程下运行,也会发生同样的事情或在单独的例程中进行测试。
感谢您在这方面的帮助。
【问题讨论】:
-
此代码在另一个用户表单中?
-
您确定您的其他表单实际上称为
UserForm13,并且其上的组合框实际上称为ComboBox1? -
Robin,确实——我检查了事物的名称,没有发现任何问题。
-
Thomas - 项目分为模块...此代码在其自己的模块中,用户窗体的组件根据需要调用代码。