How to find the type of VBA collection?

CPPlus

New Member
Joined
Jul 21, 2016
Messages
6
Hello everyone!

We know how to iterate a Range object because of Google (A range has "Cell" children).
We know how to iterate an array of strings because of Google (It has "String" children).

But how do we iterate something if we don't know the type of it's children? How do we find it out? I thought the Object Browser would help, but it only lists properties and not children type.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
See the functions IsObject(), IsArray(), TypeName(), VarType(), and the Type Of ... Is operator.
 
Upvote 0
They are indeed useful, but they give me the type of an object that I already have. I don't even know how to begin iterating without knowing the children's type beforehand.
 
Upvote 0
Can you just explain in very simple terms what you're trying to do?
 
Last edited:
Upvote 0
I am trying to extract data from powerpivot. There is a ModelTableColumns object that i tried to iterate. Naturally its going to contain a ModelTableColumn children, so that was the easy part. Now i suppose that ModelTableColumn has children too, but whats their type? I want to see if there are children and iterate them with for each.
 
Upvote 0
Arrays are not collections. To loop through an array of strings, one must use a Variant Data type regardless of the type of the array

Code:
Dim myArray(1 To 3) As String
Dim oneString As [U]String[/U]
    
myArray(1) = "A": myArray(2) = "B": myArray(3) = "C"

For Each oneString In myArray ' this line throws a compile error <<<<<<<<<<
    MsgBox oneString
Next oneString

Code:
' this works

Dim myArray(1 To 3) As String
Dim oneString As [U]Variant[/U] 
    
myArray(1) = "A": myArray(2) = "B": myArray(3) = "C"

For Each oneString In myArray
    MsgBox oneString
Next oneString

Also when looking at the Object Browser, if you are looking at a collection and you don't know the data type of the member of that collection you could look at the .Item property of that collection object.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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