Vba 使用用户表单中的字符串数组填充组合框

Vba 使用用户表单中的字符串数组填充组合框,vba,excel,Vba,Excel,我试图解决组合框的一个问题,我不希望列出的选项在每次执行宏时都创建另一个double,如果AddItem属性仅用于填充组合框,就会发生这种情况。该项目将在用户表单中的下拉菜单中列出12个月,目标是让用户以打印预览模式显示该表(随附的工作表中每个月都有一个表)。我为几个月编写了一个字符串数组,然后让它在循环中用AddItem属性填充组合框。也就是说: Private Sub ComboBox1_Change() Dim strMonth(0 To 11) As String strMonth(

我试图解决组合框的一个问题,我不希望列出的选项在每次执行宏时都创建另一个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的例程下运行或在单独的例程中测试,也会发生同样的情况


我感谢您在这方面的帮助。

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

下面的操作应该与您可以测试的功能相同

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是对的,用户表单或组合框必须有另一个名称


祝你好运

试试罗宾斯的建议,因为这是最有可能的解决方案。如果这是正确的,那么它抱怨的对象一定是数组

下面的操作应该与您可以测试的功能相同

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是对的,用户表单或组合框必须有另一个名称


祝您好运

如果您正在从Userform13运行ComboBox1_Change()事件,则您将收到此错误

已显示的表格;无法显示模式


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

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

已显示的表格;无法显示模式


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

如果您检查实际的用户名和组合框名称,您可以尝试以下操作:

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

如果您检查实际的用户名和组合框名称,您可以尝试以下操作:

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

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

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

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

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


这个代码是另一个用户表单?你确定你的另一个表单实际上被称为
UserForm13
,并且它上面的组合框实际上被称为
ComboBox1
?Robin,的确-我检查了事物的名称,没有发现任何问题。Thomas-项目分为模块…这个代码在它自己的模块和userf中orm的组件根据需要调用代码。此代码在另一个用户窗体中?您确定您的另一个窗体实际上被称为
UserForm13
,并且其上的组合框实际上被称为
ComboBox1
?Robin,确实-我检查了事物的名称,没有发现任何问题。Thomas-项目被划分为模块…这的代码位于其自己的模块中,用户窗体的组件根据需要调用代码。^此建议效果很好。我不知道如何使用.List属性。谢谢!不客气。如果我已填写完您的问题,请将答案标记为已接受。谢谢。^此建议效果很好。我不知道如何使用.List属性。谢谢!不客气。如果我填好了你的问题,请将答案标记为已接受。谢谢你,非常感谢。从更有效地使用代码的角度来看,你的解决方案工作得更好……我可能会在这个项目上依靠它。再次感谢!蒂姆,非常感谢。从这个角度来看,你的解决方案工作得更好为了更有效地使用代码,我可能会在这个项目上使用它。再次感谢!Andrew,这正是我之前所做的,每次运行代码时,它都会在菜单中生成月份名称的两倍和三倍;它的设置方式是,电子表格上的一个按钮在单击时调用此子项你可以继续打印你想要的任何月表。如果你得到了重复的月表,那么在你添加值ComboBox1.clear.Andrew之前清除它,这正是我之前所做的,每次我运行代码时,它都会在菜单中生成月名的两倍和三倍;它的设置方式是在spreadshee上的一个按钮单击此sub时,t调用此sub,然后继续打印所需的任何月表。如果您得到重复的月表,请在添加值ComboBox1.clear之前清除它。