Inspecting elements of a collection

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This code checks the version of Excel installed:

Code:
Option Explicit


Sub Test()


    Dim abc As Collection
    Set abc = New Collection
    
    abc.Add Module2.Version
    
    abc.Item (0)  ' FAILS HERE
    
End Sub



Code:
Option Explicit


Public Function Version() As Collection
    
    Dim Coll As Collection
    Set Coll = New Collection
    
    With ThisWorkbook
        
        Select Case Val(Application.Version)
            
            Case Is < 12
            
        
                ' Excel 97-2003
                
                Coll.Add Item:=".xls"
                Coll.Add Item:=-4143
                        
            Case Else
            
            
                'Excel 2007-2016
                
                Select Case ThisWorkbook.FileFormat
                
                    Case 51
                                    
                        Coll.Add Item:=".xlsx"
                        Coll.Add Item:=51
                
                    Case 52
                    
                        Select Case .HasVBProject
                        
                            Case True
                                            
                                Coll.Add Item:=".xlsm"
                                Coll.Add Item:=52
                                
                            Case False
                            
                                Coll.Add Item:=".xlsx"
                                Coll.Add Item:=51
                        
                        End Select
                
                    Case 56
                                        
                        Coll.Add Item:=".xls"
                        Coll.Add Item:=56
                    
                    Case Else
                    
                        Coll.Add Item:=".xlsb"
                        Coll.Add Item:=50
    
                End Select
        
        End Select
        
    End With
    
    Set Version = Coll
    
End Function

What I can't understand is when I try to inspect the elements of the collection using:

Code:
abc.Item (0)

it fails.


though this correctly shows 1:


Code:
abc.Count


What is wrong?


Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
The index starts at 1, not 0, though I'm not really sure what you expect that line to actually do.
 
Upvote 0
In addition, the Version function adds a Collection to the abc Collection, therefore:
Code:
    Debug.Print abc.Item(1).Item(1)
    Debug.Print abc.Item(1).Item(2)
    
    'or
    Debug.Print abc(1).Item(1)
    Debug.Print abc(1).Item(2)
 
Upvote 0
Thanks, just what I wanted.

I expected it return the file extension and format.
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,289
Members
449,077
Latest member
Rkmenon

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