+ Reply to Thread
Results 1 to 21 of 21

VBA Scripting.Dictionary Code Late Binding Error

  1. #1
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    VBA Scripting.Dictionary Code Late Binding Error

    Hi,

    I am learning how to use Scripting.Dictionary currently.

    This is the request I was trying to solve (File Attached)

    http://www.excelforum.com/excel-prog...-mail-ids.html

    And I came up with this code.

    Please Login or Register  to view this content.
    The Code works fine when early bound, however when I made it Late Bound, it fails on the For Counter = 1 To Dict.Count line.

    Dict.Count somehow shows no value.

    Can any one tell me what I am doing wrong, and also would like some feedback on the code and improvement suggestions.
    Attached Files Attached Files
    Last edited by NeedForExcel; 07-18-2015 at 07:44 AM.
    Cheers!
    Deep Dave

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA Scripting.Dictionary Code Late Binding Error

    I can't see your code, I'm on my phone, generally though, you should be looping through a dictionary using its keys not by index.

    You can get the keys with they keys method then use this to iterate. Your code will also fail if 2 cells have the same valie

  3. #3
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: VBA Scripting.Dictionary Code Late Binding Error

    Hi Kyle..

    I was trying to loop using the keys method only, however I could not achieve what I was trying to do..

    Probably the way I'v designed the code, maybe using Index was required, not sure.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: VBA Scripting.Dictionary Code Late Binding Error

    Try
    Please Login or Register  to view this content.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA Scripting.Dictionary Code Late Binding Error

    But that's a bit nasty inside a loop, you're returning the full items array in each iteration.

    You can't loop by index through a dictiinary (dictionaries technically aren't ordered so looping by index makes little sense).

    Jindons code returns all the items as an array and then accesses by index

  6. #6
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: VBA Scripting.Dictionary Code Late Binding Error

    @Jindon - That works.. But I do not understand why would this line Cells(Counter + 1, "C").Value = Dict.Items(Counter - 1) work when early bound and not when late bound.. Any specific reason?

    @Kyle - So isn't the code I'v used a good approach to using dictionaries? If no, could you suggest the right way?

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: VBA Scripting.Dictionary Code Late Binding Error

    As Kyle mentioned .Items() returns Array of item so you can refer it by index like .Items()(0)

    I would use array like I did for generate csv file in you attachment.

    You code doing extra loop that is no need
    Please Login or Register  to view this content.

  8. #8
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: VBA Scripting.Dictionary Code Late Binding Error

    Here's a site I like for dictionaries.
    Maybe you are already familiar with it.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

  9. #9
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: VBA Scripting.Dictionary Code Late Binding Error

    Thank you skywriter.

    I will check it out..

    @ Jindon, Just 1 last question for you..

    In the original post, what does If E <> "" Then .Item(E) = Empty really do?

    Also, I noticed the use of #1, would like to know its significance aswell..
    Last edited by NeedForExcel; 07-20-2015 at 12:22 AM.

  10. #10
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: VBA Scripting.Dictionary Code Late Binding Error

    I could probably help you with this and jindon will correct me if I'm wrong.

    If you go to that website I linked for you, then you will find out one of the interesting things about dictionaries is that depending on the way you enter items into it you can use it to create a unique list.
    With e he's looping through the list of email addresses, and he's checking whether they are empty or not <>"".
    By using the .item(e) which is this (.item(key)=actual item), he's adding an empty item to the dictionary with the key being (e), e is the email address. If you use this method and a key already exists the dictionary just ignores it instead of giving you an error. He is using the keys (e) to build a unique list of email addresses and since he's not really going to use the items just the keys he adds nothing as an item. This also is allowed by a dictionary, it causes no error. Then with the join and .keys he's dumping those email addresses into the file he opened. Pretty sure on that part, but I've never done it, it's just a deduction from the Microsoft site I sent you a link to below and the OP request on that post.
    See 6.2.2 method .Item()=, on the page I linked you to in an earlier post.

    I hope I explained that clearly.

    The number #1 is part of the syntax required with the open statement.
    You can find some info on that here.

    Good Luck.
    Last edited by skywriter; 07-20-2015 at 01:12 AM.

  11. #11
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: VBA Scripting.Dictionary Code Late Binding Error

    Thank you skywriter for a detailed explanation. It was fantastic..

    You clarified many things which were as doubts in my head..

    However, this still confuses me - If e <> "" Then .Item(e) = Empty

    I mean, why does the code set the value of .Item(e) to empty when e indeed has a value (<> "")

    Should it not be the other way round? It is very counter intuitive..

  12. #12
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: VBA Scripting.Dictionary Code Late Binding Error

    You're already using e as the key so there's no need to store it as the item too - you can retrieve all the email addresses by iterating through the keys.
    Remember what the dormouse said
    Feed your head

  13. #13
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: VBA Scripting.Dictionary Code Late Binding Error

    Quote Originally Posted by romperstomper View Post
    You're already using e as the key so there's no need to store it as the item too - you can retrieve all the email addresses by iterating through the keys.
    Now I get it..

    So basically, In .Item(e) 'e' is the Key, and if that key is not blank, we are setting the Value Property of that particular Key to Empty, which really does not matter as we aren't using the value anywhere..

    Am I right?

  14. #14
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: VBA Scripting.Dictionary Code Late Binding Error

    So a dictionary has items and keys and the way to access the item is through the key. I went back and read up a little more on dictionaries to refresh myself. I misspoke in my earlier post when I said the dictionary ignores the duplicate key, this is actually a method to replace the item associated with the key.
    When you have an existing item in the dictionary if you want to replace it you access it through the key.

    So If I add an item using this method .item(key)= "Test" and I want to access the item which is the word "Test" I have to access it through the key. Now if I want to replace the item I use the same method .item(key)="New Test".

    This particular method for adding an item to a dictionary will not cause an error if the key already exists, it will just replace the item with the new item.

    We aren't interested in what the items are because we only want to make sure we have a unique set of keys, in the post jindon was involved in he had a list of duplicate email addresses and he wanted to create a unique list. He does that by adding items, yes I know the item is empty, but we can add an empty item because when we are done we are only interested in using the keys, which are the email addresses.

    So we have a list of email addresses.
    joe@yahoo
    bill@yahoo
    fred@yahoo
    joe@yahoo
    bill@yahoo

    and we only want a list of unique email addresses
    So we loop the email addresses and use them as keys while adding items to the dictionary. We aren't interested in the items, they don't matter, we want the keys.
    So we loop through the list of email addresses and add
    .item(bill@yahoo)=empty
    bill@yahoo is the key the item is empty.
    when we get to bill@yahoo again then what we are doing is replacing the item associated with the key(bill@yahoo) with a new item, but it's also empty, but what we really did is keep from ending up with a duplicate email address because keys in a dictionary are unique. It's a trick that allows you to loop through a list of names, numbers, dates, whatever and only end up with a unique list.
    If you go to that site that I sent you then you will see that if you use a different method to add a new key and item to a dictionary and end up trying to add another item with a key that already exists you get an error. With the method jindon used you get no error and end up with a list of unique keys. Then you can extract the keys and there you go, a unique list.
    Last edited by skywriter; 07-20-2015 at 02:06 AM.

  15. #15
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA Scripting.Dictionary Code Late Binding Error

    You can't have an entry without a key in a dictionary, so since you only have one set of values and you can read the keys you don't need to repeat it. It also makes to code a bit more efficient since you're not doing 2 worksheet calls for each element

  16. #16
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: VBA Scripting.Dictionary Code Late Binding Error

    Perfect!

    I was actually referring to the same site previously, so I could understand what exactly you meant..

    Thank you for the clarification, I was a BIG help to me..

    Marking the thread as solved..

  17. #17
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA Scripting.Dictionary Code Late Binding Error

    Oops there were a lot of posts since I refreshed the page....

  18. #18
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: VBA Scripting.Dictionary Code Late Binding Error

    Haha! No problem..

  19. #19
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: VBA Scripting.Dictionary Code Late Binding Error

    Sorry guys, just one more question..

    So, are these 2 lines identical?

    If E <> "" Then .Item(E) = Empty

    If E <> "" Then .Item(E) = ""

    Without any Advantages/Dis-Advantages?
    Last edited by NeedForExcel; 07-20-2015 at 02:33 AM.

  20. #20
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA Scripting.Dictionary Code Late Binding Error

    Technically, no they're not. Empty means there's nothing there "" means there's a zero length string there. It's unlikely to make a huge amount of difference, but empty will be more efficient as well as explicit

  21. #21
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: VBA Scripting.Dictionary Code Late Binding Error

    Alright great!

    Thank you for the clarification Kyle..

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Scripting Dictionary Type mismatch Run-Time error 13
    By Seve in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-11-2014, 03:47 AM
  2. Scripting.Dictionary .. Error
    By ks1102 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-09-2014, 06:08 AM
  3. [SOLVED] Learning differences between Early Binding & Late Binding
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 06-28-2013, 01:16 AM
  4. [SOLVED] Seeking knowledge on Early Binding, Late Binding
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-17-2013, 10:45 PM
  5. [SOLVED] Run-time error using late binding
    By DaBookshah in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-20-2006, 03:35 AM
  6. [SOLVED] Help converting code to late binding
    By XP in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-16-2006, 01:55 PM
  7. [SOLVED] Late Binding examples of binding excel application
    By HeatherO in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 03-17-2005, 01:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1