循环遍历vba excel中的多维数组

时间:2021-11-03 21:26:57

I'm trying to loop through a 2d array. The 1d will always be 25, the 2d will have different amounts. Quite often the members of the 1st dimensional will be empty which is the point of the isarray(sent) code. I'm getting a subscript out of range at the part which says for j = 1 to ubound(sent,2)

我正试图循环一个二维数组。 1d将始终为25,2d将具有不同的金额。通常,第一维的成员将是空的,这是isarray(已发送)代码的要点。我在j = 1到ubound(发送,2)的部分得到一个超出范围的下标

For i = 1 To 25
    If IsArray(sent(i)) Then
        For j = 1 To UBound(sent, 2)
            If concat_multi = "" Then
            concat_multi = sent(i, j)
            Else
            concat_multi = concat_multi & " & " & sent(i, j)
            End If
        Next
        ActiveCell.Offset(1) = concat_multi
        concat_multi = ""
    End If
Next

Here is a screenshot

这是一个截图

循环遍历vba excel中的多维数组

1 个解决方案

#1


5  

Look at your object browser. You need to refer to it as Sent(i) is a 1d array. So you have a 1d array wherein each element is another 1d array.

看看你的对象浏览器。您需要将其称为Sent(i)是一个1d数组。所以你有一个1d数组,其中每个元素是另一个1d数组。

rather than sent(i, j) do sent(i)(j) and initiate the loop thusly:

而不是发送(i,j)发送(i)(j)并因此启动循环:

for j = 1 To ubound(sent(i))

Technically you should probably be doing this in case you get arrays with base other than 1 (base 0 is common and default unless it is a range array).

从技术上讲,你应该这样做,以防你得到基数不是1的数组(基数0是常见的,默认情况下除非它是一个范围数组)。

For j = lbound(sent (i)) to ubound(sent(i))

#1


5  

Look at your object browser. You need to refer to it as Sent(i) is a 1d array. So you have a 1d array wherein each element is another 1d array.

看看你的对象浏览器。您需要将其称为Sent(i)是一个1d数组。所以你有一个1d数组,其中每个元素是另一个1d数组。

rather than sent(i, j) do sent(i)(j) and initiate the loop thusly:

而不是发送(i,j)发送(i)(j)并因此启动循环:

for j = 1 To ubound(sent(i))

Technically you should probably be doing this in case you get arrays with base other than 1 (base 0 is common and default unless it is a range array).

从技术上讲,你应该这样做,以防你得到基数不是1的数组(基数0是常见的,默认情况下除非它是一个范围数组)。

For j = lbound(sent (i)) to ubound(sent(i))