user147178
user147178

Reputation: 471

loop through a multidimensional array in vba excel

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)

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

enter image description here

Upvotes: 2

Views: 11754

Answers (1)

David Zemens
David Zemens

Reputation: 53623

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.

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

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).

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

Upvotes: 5

Related Questions