Cannot iterate Hashtable in VBA (Excel)

后端 未结 2 637
Happy的楠姐
Happy的楠姐 2021-01-13 16:01

I am using Hashtable (mscorlib.dll referenced). I fill it with data, I can get any item (as long as I convert the request type to exactly same type what\'s stored in hashtab

相关标签:
2条回答
  • 2021-01-13 16:12

    Actually there is one way to browse a .NET hashtable from VBA :

    Dim hash As mscorlib.Hashtable
    Set hash = New mscorlib.Hashtable
    
    Call hash.Add("foo", "bar")
    Call hash.Add(4, 8)
    
    Dim keys As IEnumerable
    Set keys = hash.keys
    
    Dim key As Variant
    For Each key In keys
    
        Dim value As Variant
    
        value = hash.Item(key)
    Next
    

    The main issue being "casting" the return value of the property "Keys" into a IEnumerable before using it in a "for each" loop. VBA can not handle multiple interface inheritance from scratch: you have to cast to the interface the function/property belongs to before you can call it.

    0 讨论(0)
  • 2021-01-13 16:23

    VBA cannot handle DictionaryEntry type (element of hashtable) nor ICollection type (that's what .Keys method returns), so only solution was to write my own library.

    0 讨论(0)
提交回复
热议问题