Display collection items from array function properly

simurq

Board Regular
Joined
Nov 11, 2011
Messages
73
My function returns an array which also have a collection (Col) as its last item:

Code:
For Each Node In Resp.getElementsByTagName("condition")
   cover = Node.Attributes.getNamedItem("cover").Text
            
   If Not Node.Attributes.getNamedItem("base") Is Nothing Then
      base = Node.Attributes.getNamedItem("base").Text
   Else: base = "0"
   End If

   Col.Add Array(cover, base)
Next Node

funcResult = Array(data1, data2, data3, data4, Col)

But when I run the Sub, I get "Runtime error 1004: Application-defined or object-defined error". I understand this is due to array "temp()"s size but how can I display the temp() values properly?

Code:
Sub Button_Click()    
    Dim getData as String
    Dim temp() As Variant
    Dim C As Integer
    
    Sheet1.Range("A:A").ClearContents
    getData = Sheet1.Range("A1").Value
    temp = funcResult(getData)
    C = UBound(temp)
    
    For i = 0 To C
        Sheet1.Cells(i + 1, 1).Value = temp(i)
    Next i
    
End Sub

Thanks!

Rustam
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
The string getData isn't assigned any value. You're passing an empty string to the function.
 
Upvote 0
Yes and no... :) It is assigned a string value in my workbook - I just showed a part of code here. Sorry, I forgot to mention that here.
And corrected my original post.

Problem remains unsolved...
 
Last edited:
Upvote 0
What are data1, data2 etc.? Are they arrays as well?

Does funcResult return an array of arrays?
 
Upvote 0
No, Sir. data1, data2 etc. are just strings. Only the last item of array temp() is a collection. Therefore, when I attempt to display the items in array temp(), I get errors 104 (Application defined error) and 405 (Wrong number of arguments passed...).

Maybe this will help? The last, 9th item of array, actually consists of 4 items gotten from a collection.

 
Last edited:
Upvote 0
Maybe something like this

Code:
    [COLOR=darkblue]For[/COLOR] i = 0 [COLOR=darkblue]To[/COLOR] C - 1
        Sheet1.Cells(i + 1, 1).Value = temp(i)
    [COLOR=darkblue]Next[/COLOR] i
    [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] temp(C).Count
        Sheet1.Cells(C + i, 1).Value = temp(C)(i)
    [COLOR=darkblue]Next[/COLOR] i
 
Upvote 0
Thanks, this works OK... partially since I cannot extract the 2nd value from temp(C). The following returns an error:

Code:
    For i = 1 To temp(C).Count
        For j = 0 To temp(C).Count - 1
            Sheet1.Cells(C + i, 6).Value = temp(C)(i)(j)
            Sheet1.Cells(C + i, 7).Value = temp(C)(i)(j+1)
        Next j
    Next i
 
Upvote 0
No problem, I solved it:

Code:
    For i = 1 To temp(C).Count
        For j = 1 To 1
            Sheet1.Cells(C + i, 6).Value = temp(C)(i)(j - 1)
            Sheet1.Cells(C + i, 7).Value = temp(C)(i)(j)
        Next j
    Next i

"j = 1 To 1" helped - since it contains 2 values only, it was easy to modify the rest of the code. :)

Thank you!
 
Upvote 0
I was away for awhile. Glad you worked it out.

This may be a little more concise.
Code:
    [color=darkblue]For[/color] i = 1 [color=darkblue]To[/color] temp(C).Count
        Sheet1.Cells(C + i, 6).Value = temp(C)(i)(0)
        Sheet1.Cells(C + i, 7).Value = temp(C)(i)(1)
    [color=darkblue]Next[/color] i
 
Upvote 0
Thanks! That's indeed more concise...

So, just to sum up, to avoid sizing errors during the runtime and to display collection items from within an array properly one should:

# put a collection as the last (Nth) element of an array
# cycle through the array until the Nth - 1 element
# cycle through the rest of array elements
 
Upvote 0

Forum statistics

Threads
1,215,473
Messages
6,125,017
Members
449,203
Latest member
tungnmqn90

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top