Passing Listobject Range to array and getting erro

2019-08-31 04:30发布

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条回答
▲ chillily
2楼-- · 2019-08-31 05:24

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

查看更多
登录 后发表回答