【问题标题】:Populate a combo box with a string array in a user form在用户表单中使用字符串数组填充组合框
【发布时间】: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 - 项目分为模块...此代码在其自己的模块中,用户窗体的组件根据需要调用代码。

标签: vba excel


【解决方案1】:

为此不需要数组(尽管这可能不是导致错误的原因:

Dim m As Long, s As String

For m = 1 To 12
    s = "Print " & MonthName(((m + 2) Mod 12) + 1) & " Table"
    UserForm13.ComboBox1.AddItem s
Next m

【讨论】:

  • 蒂姆,非常感谢。从更有效地使用代码的角度来看,您的解决方案效果更好......我可能会在这个项目上依赖它。再次感谢!
【解决方案2】:

如果您检查实际的用户表单和组合框名称,您可以试试这个:

Private Sub ComboBox1_Change()

Dim strMonth(1 To 12) As String

strMonth(1) = "Print April Table"
strMonth(2) = "Print May Table"
strMonth(3) = "Print June Table"
strMonth(4) = "Print July Table"
strMonth(5) = "Print August Table"
strMonth(6) = "Print September Table"
strMonth(7) = "Print October Table"
strMonth(8) = "Print November Table"
strMonth(9) = "Print December Table"
strMonth(10) = "Print January Table"
strMonth(11) = "Print February Table"
strMonth(12) = "Print March Table"

With UserForm13
    With .ComboBox1
        .Clear
        .List = strMonth
        .Style = fmStyleDropDownList
    End With
    .Show
End With
Unload UserForm13

End Sub

【讨论】:

  • ^这个建议效果很好。我不知道如何使用 .List 属性。谢谢!
  • 不客气。如果我完成了您的问题,请将答案标记为已接受。谢谢
【解决方案3】:

尝试 Robins 的建议,因为它是最有可能的解决方案,如果正确,那么它所抱怨的对象必须是数组。

following 应该和你可以测试的函数做同样的事情。

Private Sub ComboBox1_Change()
   With UserForm13.ComboBox1
      .AddItem "Print April Table"
      .AddItem "Print May Table"
      .AddItem "Print June Table"
      .AddItem "Print July Table"
      .AddItem "Print August Table"
      .AddItem "Print September Table"
      .AddItem "Print October Table"
      .AddItem "Print November Table"
      .AddItem "Print December Table"
      .AddItem "Print January Table"
      .AddItem "Print February Table"
      .AddItem "Print March Table"
      .Style = fmStyleDropDownList
   End With
   UserForm13.Show
End Sub

如果在“With UserForm13.ComboBox1”行出错,那么 Robin 是对的,用户表单或组合框必须有另一个名称。

祝你好运

【讨论】:

  • Andrew,这正是我之前所拥有的,每次运行代码时,它都会在菜单中生成双倍和三倍的月份名称;它的设置方式是电子表格上的一个按钮在单击时调用此子,然后您继续打印您想要的任何月度表。
  • 如果出现重复项,请在添加值 ComboBox1.Clear 之前将其清除。
【解决方案4】:

如果您从 Userform13 运行 ComboBox1_Change() 事件,那么您将收到此错误

表格已经显示;无法模态显示

除非您在 Userform13 的属性页面上将 Userform13's ShowModal 设置为 false。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 2016-05-21
    • 2020-01-30
    • 2015-05-10
    • 1970-01-01
    相关资源
    最近更新 更多