VBA将数组添加到列表中

时间:2016-04-11 14:49:01

标签: arrays vba

Sub RangeBulkAmend()

   Set list = list.CreateInstance
     Dim c As Range
      Dim i As Long
      Dim myarr() As Variant

      For Each c In Selection

           list.Add c.value     
       Next c

            ReDim myarr(list.Count - 1)

         For i = 1 To list.Count - 1
             myarr(i) = list.Items(i)
             msg = msg & vbCrLf & myarr(i)

          Next i



          {{ListWindow.ListBox1.list = myarr}}

             Load ListWindow

             ListWindow.Show
end sub

我在编译时遇到错误,因为我尝试将我的数组传递给列表,其中包含双括号的代码,其中编译器也指向,但如果我突出显示我得到消息对象变量或使用块变量未设置任何帮助将很高兴谢谢你  提前请注意上面代码中列出的列表是我自己的自定义列表,问题是将数组发送到双括号的列表框中,检查它生成的代码,现在将其提取到列表框

1 个答案:

答案 0 :(得分:1)

如果您只想加载包含所选单元格值的列表框,则:

Sub RangeBulkAmend()

    Dim myarr() As Variant

    myarr = Selection.Value

    Load ListWindow
    ListWindow.ListBox1.List = myarr
    ListWindow.Show
End Sub

会这样做吗

或者就此而言只是跳过整个而只是分配选择。列表框的值也有效:

Sub RangeBulkAmend()

    Load ListWindow
    ListWindow.ListBox1.List = Selection.Value
    ListWindow.Show
End Sub

要批量添加到列表框中的现有列表,请尝试以下操作:

Sub RangeBulkAmend()
    Load ListWindow
    Dim myarr() As Variant
    Dim oldarr() As Variant
    Dim t&, i&

    myarr = Selection.Value

    t = ListWindow.ListBox1.ListCount

    ReDim oldarr(0 To (ListWindow.ListBox1.ListCount + UBound(myarr, 1) - 1)) As Variant
    For i = 0 To UBound(oldarr)
        If i < ListWindow.ListBox1.ListCount Then
            oldarr(i) = ListWindow.ListBox1.List(i)
        Else
            oldarr(i) = myarr(i - t + 1, 1)
        End If
    Next i

    ListWindow.ListBox1.List = oldarr
    ListWindow.Show modal
End Sub