vb Code:
  1. Option Explicit
  2.  
  3. Private Declare Sub PokeLong Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, Optional ByVal Length As Long = 4)
  4.  
  5. Private Function ItemKey(ByVal Index As Long, Coll As Collection) As String
  6.  
  7.   'optimized get collection sKey by index
  8.   'Private Declare Sub PokeLong Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, Optional ByVal Length As Long = 4)
  9.  
  10.   Dim i     As Long
  11.   Dim Ptr   As Long
  12.   Dim sKey  As String
  13.  
  14.     If Coll Is Nothing Then                             'oops!
  15.         Err.Raise 91                                    'No object
  16.       Else 'NOT COLL...
  17.         Select Case Index
  18.           Case Is < 1, Is > Coll.Count                  'oops!
  19.             Err.Raise 9                                 'Index out of range
  20.           Case Is <= Coll.Count / 2                     'walk items upwards from first
  21.             PokeLong Ptr, ByVal ObjPtr(Coll) + 24       'first Ptr
  22.             For i = 2 To Index
  23.                 PokeLong Ptr, ByVal Ptr + 24            'next Ptr
  24.             Next i
  25.           Case Else                                     'walk items downwards from last
  26.             PokeLong Ptr, ByVal ObjPtr(Coll) + 28       'last Ptr
  27.             For i = Coll.Count - 1 To Index Step -1
  28.                 PokeLong Ptr, ByVal Ptr + 20            'prev Ptr
  29.             Next i
  30.         End Select
  31.         i = StrPtr(sKey)                                'save StrPtr
  32.         PokeLong ByVal VarPtr(sKey), ByVal Ptr + 16     'replace StrPtr by that from collection sKey (which is null if there ain't no sKey)
  33.         ItemKey = sKey                                  'now copy it to function value
  34.         PokeLong ByVal VarPtr(sKey), i                  'and finally restore original StrPtr
  35.     End If
  36.  
  37. End Function
  38.  
  39. Private Function ItemIndex(ByVal Key As String, Coll As Collection, Optional ByVal Compare As VbCompareMethod = vbTextCompare) As Long
  40.  
  41.   'get collection index by key
  42.   'Private Declare Sub PokeLong Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, Optional ByVal Length As Long = 4)
  43.  
  44.   Dim Ptr   As Long
  45.   Dim sKey  As String
  46.   Dim aKey  As Long
  47.  
  48.     If Coll Is Nothing Then                             'oops!
  49.         Err.Raise 91                                    'No object
  50.       Else 'NOT COLL...
  51.         If Coll.Count Then
  52.             aKey = StrPtr(sKey)                         'save StrPtr
  53.             PokeLong Ptr, ByVal ObjPtr(Coll) + 24       'first Ptr
  54.             ItemIndex = 1                               'walk items upwards from first
  55.             Do
  56.                 PokeLong ByVal VarPtr(sKey), ByVal Ptr + 16
  57.                 If StrComp(Key, sKey, Compare) = 0 Then 'equal
  58.                     Exit Do                             'found
  59.                 End If
  60.                 ItemIndex = ItemIndex + 1               'next Index
  61.                 PokeLong Ptr, ByVal Ptr + 24            'next Ptr
  62.             Loop Until Ptr = 0                          'end of chain
  63.             PokeLong ByVal VarPtr(sKey), aKey           'restore original StrPtr
  64.         End If
  65.         If Ptr = 0 Then
  66.             ItemIndex = -1                              'key not found
  67.         End If
  68.     End If
  69.  
  70. End Function

[code courtesy of PSC]