Passing Listobject Range to array and getting error “out of range”

前提是你 提交于 2019-12-13 03:47:17

问题


In order to increase speed of my code when looping thought values of a datarange of a Listobject I want to pass the values of a column to an array and loop through the array instead of looping throught the values of the Listobject.

I got an insight here From there I elaborated this code.

'passing the list object to a variable
Dim SCtbl As ListObject
    Set SCtbl = ThisWorkbook.Worksheets("sc").ListObjects(1)

'dimensioning the array (as variant)
Dim ListofSC As Variant
    ListofSC = SCtbl.ListColumns("long_shortcut").DataBodyRange.Value
MsgBox (LBound(ListofSC))
MsgBox (UBound(ListofSC))
MsgBox (ListofSC(1))

The first message gives result 1 The second message gives result 708 (the row items of the listobject)

But when accesing the elements I get Subscript out of range in element 1.

Is actually ListofSC a normal array of dimension 1? If so why cant I access the values?

thanks.


回答1:


When you copy data from an Excel range to a Variant, Excel returns a 2-D array. When your source range is a single column you can access the elements by setting the index of the 2nd dimension of the array to 1, e.g.:

MsgBox (ListofSC(1, 1))

Hope that helps



来源:https://stackoverflow.com/questions/54111015/passing-listobject-range-to-array-and-getting-error-out-of-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!