View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default How to read a collection name index value?

You can't do it with a Collection object without looping. If, however, you
can use a Dictionary object instead of a Collection object, you can do
something like the following,

Sub BBB()

Dim D As Scripting.Dictionary
Dim WhatKey As String
Dim V As Variant
Set D = New Scripting.Dictionary

D.Add Key:="a", Item:="aaa"
D.Add Key:="b", Item:="bbb"
D.Add Key:="c", Item:="ccc"

WhatKey = "c" ' what Key do you want to find
V = Application.Match(WhatKey, D.Keys, 0) ' 1-based, not 0-based
If IsError(V) = True Then
Debug.Print "Key Not Found: " & WhatKey
Else
V = V - 1 ' change 1-based Match result to 0-based index
Debug.Print "Index Of Key: (" & WhatKey & "):" & CLng(V) & " (index is
0-based)"
V = D.Items(Application.Match(WhatKey, D.Keys, 0) - 1)
Debug.Print "Item of Key (" & WhatKey & "): " & V
End If

End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)



"G Lykos" wrote in message
...
Greetings!

You can use a collection index value, for example Gizmos(13).Name, to get
the name of item 13. Am wondering if there is an equivalent direct
mechanism to use a collection item name to get its index value, along the
lines of Gizmos("SomeName").Index. If symmetrical functions (properties,
I
guess, in VBA speak) existed, Gizmos(Gizmos("SomeName").Index).Name would
return "SomeName" - an identity property of sorts.

I suppose a brute-force approach would be to create a function doing a For
name/string comparison loop over 1 to Gizmos.count. Am hoping for
something
more direct.

Thanks,
George