Results 1 to 6 of 6

Thread: Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys RESOLVE

  1. #1

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

    Resolved Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys RESOLVE

    Esteemed Forum Participants and Lurkers:
    ===============================
    Excel 2003 VBA

    I am doing some relatively low level 'artificial intelligence', and I think the easiest way achieve my objective is to make a pair of dictionaries, and to iterate through 1 of the dictionaries checking for matching entries in the second dictionary. What I need to do in the case of a match, is to REMOVE both of the dictionary entries.

    The question is: "is it safe to REMOVE dictionary entries WHILE ITERATING through the dictionary ???"

    If this is UNwise, I need to rethink my whole approach.

    Here is a code snippet:
    Code:
        For Each aKey In PROGd.keys
            'Check for a matching key in the DISTRO Dictionary
            If DISTd.exists(aKey) Then
                'We are DONE with these keys, so delete them!
                PROGd(aKey).Remove
                DISTd(aKey).Remove
            End If
        Next aKey
    It is actually a whole lot more complex than this code, but what I am doing is looking for exceptions where 2 lists do NOT match. When I get done iterating through 1 dictionary, the remainders in both dictionaries are all of the exceptions that I need to harpoon.

    Thank you for any and all comments, suggestions, and assistance.
    Last edited by Webtest; Jul 11th, 2007 at 12:37 PM.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys ???

    Im not 100% sure on a for each loop if you remove items from the collection if it would be safe but I do know that if you did a for i = dict.items.count - 1 to 0 it would be definately safe.

    When you loop in reverse the removed items will not change the collection indexes so no "Item can not be found in collection" errors.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys ???

    if you exit the for loop immediately after the deletion it should also be safe
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys ???

    I had to do a double take on your reply westconn1 For a second I thought you were the original poster.

    Yup, if you exit out of the loop and perhaps re-enter it to reinitialize the collection loop that will work too but seem more work then just reverse looping.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys ???

    it appeared to me, though maybe incorrectly, that an entry would only be in each dictionary one time, so if deleted no reason to continue the loop, just move to the next entry
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

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

    Re: Excel VBA: Dictionary: Can I: Delete 'used entries' while iterating thru Keys ???

    This part of my application is an EXCEPTION HANDLER.

    The 2 lists I am trying to reconcile may contain 200 or more items and most of the items will match and get deleted because they are correct and will be processed correctly. At the point I am at in the code, it is only the exceptions that I am interested in locating, since these are due to various errors and need to be corrected before the business process can be completed. The nice thing about the dictionaries is that each key can only be added to the dictionary one time, which preclues errors due to duplicates.

    Thank you all for your gracious comments and assistance. I have a good handle on the solution now.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

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