2016-04-11 119 views
0
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 

我对编译错误,因为我尝试我的数组传递给一个列表的代码用双括号是编译器在哪里点太多,但如果我强调我得到的消息对象变量或用块变量没有设置任何帮助将很乐意欣赏谢谢你提前 请注意在上面的代码引用的列表是我自己的自定义列表问题是发送数组到列表框中的双花括号检查它的代码它产生的东西现在将其提取到列表框VBA添加数组列表

+2

你可以先加载阵列跳过一个循环'myArr,该= selection.Value'然后通过其循环加载您的列表,使用'对于i = LBOUND(myArr,该)到UBOUND(M yarr)'。这不是答案,而是一个建议。 –

+1

在这里暗刺...在你调用Load ListWindow后设置列表。控制还不存在。 – Jeremy

+0

看起来你正在使用1作为数组的UBound(这不是VBA默认值)。所以你可能想在你的VBA模块中添加'Option Base 1'(在所有子目录之前/之前)。 – Ralph

回答

1

如果只是您打算加载具有所选单元格值的列表框,则:

Sub RangeBulkAmend() 

    Dim myarr() As Variant 

    myarr = Selection.Value 

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

愿意这样做吗

或就此简单地跳过整,只是分配selection.Value到列表框也可以工作:

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