【问题标题】:Passing selected ComboBox indices to an array VBA将选定的组合框索引传递给数组 VBA
【发布时间】:2021-02-12 22:21:37
【问题描述】:

我有一个问题,希望你能回答。 我正在尝试编写一个包含 19 个组合框的用户窗体,所有组合框都具有相同的值(员工姓名),用于轮班时间表。每个 ComboBox 中有 5 个条目(5 个员工)。到目前为止,一切都很好。 我现在正在尝试的是,在单击用户窗体中的按钮时将选定的索引(0、1、2、3、4)传递给数组。最后我应该有一个包含 19 个条目的数组(对于每个组合框)。 我现在的问题是,我不知道索引是否成功传递给我的数组“Larray”。下面是代码

Public Sub cmd_Erstellen_Click()

Dim j As Long
Dim Larray As Variant

For j = 1 To 19

Me.Controls("ComboBox" & j).ListIndex = Larray

Next j



Unload UserForm1


End Sub

【问题讨论】:

  • (1) 分配需要反过来 (2) 如果您反转该行,您的代码将为同一个变量分配 19 个值。

标签: arrays excel vba combobox userform


【解决方案1】:

也许你想要这个?

Public Sub cmd_Erstellen_Click()

Dim j As Long
Dim Larray(1 To 19) As Variant

For j = 1 To 19
    Larray(j) = Me.Controls("ComboBox" & j).ListIndex
    Debug.Print j & ", " & Larray(j) 'show result in immediate window
Next j

Unload UserForm1

End Sub

【讨论】:

    【解决方案2】:

    请尝试这种方式:

    Public Sub cmd_Erstellen_Click()
     Dim j As Long, Larray(18) As Variant
    
     For j = 1 To 19
        Larray(j - 1) = Me.Controls("ComboBox" & j).ListIndex
     Next j
     Debug.Print Join(Larry, ",") 'it returns the array as string in Immediate Window
                                  'only to see if it is what you expect...
    
     Unload UserForm1
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2017-09-23
      • 2019-12-31
      • 2013-01-27
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多