Results 1 to 5 of 5

Thread: EXCEL: What is proper "Type" for Dictionary Key [RESOLVED - Variant!]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Resolved EXCEL: What is proper "Type" for Dictionary Key [RESOLVED - Variant!]

    Esteemed Forum Participants and Lurkers:
    ===============================
    Excel 2003, Microsoft Runtime Scripting Library

    I can't figure out what the proper "Type" is for Dictionary Keys. The following code runs without Option Explicit, but how do I "Dim" a key variable so that I can turn Explicit back on?
    Code:
    Sub junk()
        'Dictionary requires Microsoft Scripting Runtime in the "Tools > References" List
        Dim aList As Dictionary
        Dim aKey ' As ????????  '<< What is proper Type?
        Set aList = New Scripting.Dictionary
        
        aList.Add "Magic", "Treasure"
        
        For Each aKey In aList.Keys
            Debug.Print aKey, aList.Item(aKey)
        Next
    End Sub
    Thank you for any and all suggestions, comments, and assistance.
    Last edited by Webtest; Oct 26th, 2005 at 11:07 AM. Reason: [RESOLVED]
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: EXCEL: What is proper "Type" for Dictionary Key ???

    As Object
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: EXCEL: What is proper "Type" for Dictionary Key ???

    Thanks DKenny ... but SORRY!!!

    With "Dim aKey as Object":

    "For Each aKey In aList.Keys" generates "Run-time error '424': Object required."

    We both have something more to learn!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: EXCEL: What is proper "Type" for Dictionary Key ???

    I think I figured out how to coerce VBA into revealing the some of the nature of its automatic assignments when "Explicit" is off.

    * Turn off "Explicit"
    * Put a breakpoint at the end of the code or some other appropriate point
    * Turn on the "Locals" window
    * Run the code

    When the breakpoint is reached and the program pauses, look in the locals window. In this case, I found:
    Code:
    Expression  Value       Type
    ==========  ==========  ==========
    aKey        "Magic"     Variant/String
    Variant works fine as the data type for aKey with Explicit turned on. I'm wondering if this will work for even nastier data types?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  5. #5
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: EXCEL: What is proper "Type" for Dictionary Key [RESOLVED - Variant!]

    As a variant or Object. Depending on how your using the info later, you can convert to the proper data type: Cstr(var), Clng(Var), etc....

    VB Code:
    1. Option Explicit
    2.  
    3. Sub junk()
    4.     'No reference required this method
    5.     Dim aList As Object
    6.     Dim aKey As Variant
    7.     Set aList = CreateObject("Scripting.Dictionary")
    8.    
    9.     aList.Add "Magic", "Treasure"
    10.    
    11.     For Each aKey In aList.Keys
    12.         Debug.Print aKey, aList.Item(aKey)
    13.     Next
    14. End Sub
    Justin Labenne
    www.jlxl.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width