+ Reply to Thread
Results 1 to 11 of 11

Should .AddItem be Used or Not?

  1. #1
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Should .AddItem be Used or Not?

    Quote Originally Posted by snb View Post
    @Mordred

    Never use 'additem' to populate (i.e. >1 ) a listbox/combobox
    Why? I've seen you say that before but I don't know why.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: UserForm listbox/combobox

    See the VBEditor's helpfiles.
    See my code (especially the amount of...)
    Compare the speed of both methods



  3. #3
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: UserForm listbox/combobox

    The amount of code is a fascinating topic in my opinion. However, I don't think the number of lines of code will make run-times quicker or slower. What the code does determines the speed, am I wrong?

    The following is all I get from VBEditor helpfile:
    In a ListBox or ComboBox with a single column, the AddItem method provides an effective technique for adding an individual entry to the list. In a multicolumn ListBox or ComboBox, however, the List and Column properties offer another technique; you can load the list from a two-dimensional array.
    This doesn't tell me why, only that there is another technique to adding values.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: UserForm listbox/combobox

    On speed you can't 'think', you have to test (please feel free to do so).
    Afteer that we'll continue.
    I posted an amended version of the attachment (replaced the 'old' one).

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: UserForm listbox/combobox

    I'm trying to test the speed but either I am doing something wrong or the code runs quicker than the time value that is outputted. I have the following code being used:
    Please Login or Register  to view this content.
    It should be noted that this is my first attempt to test the speed and I'm obviously doing something wrong.

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

    Re: Should .AddItem be Used or Not?

    Try this, it's better for short durations

    See here for a description

    http://support.microsoft.com/kb/172338

    Please Login or Register  to view this content.
    Last edited by Kyle123; 08-24-2011 at 02:59 PM.

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

    Re: Should .AddItem be Used or Not?

    I've just run the code below 5 times:


    Please Login or Register  to view this content.
    The results are pretty conclusive:
    Please Login or Register  to view this content.

  8. #8
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Should .AddItem be Used or Not?

    Hi Mordred,

    If your question is "should .AddItem be used or not?" (the thread title) then the answer is it depends on the situation. AddItem is very good for putting a single value (or a couple of values) into the control, whilst List works well with larger sets of items (arrays).

    In case you're wondering, yes, you could use List to populate a single item by using a single-element array, eg
    Please Login or Register  to view this content.
    But that would be a lot slower than AddItem.

    Also bear in mind that AddItem can be used to append values when there are already some in the control (provided the existing values are not bound), whereas List cannot (directly). They're different tools for different situations.



    Using Kyle's code on my computer, it's only when there are > 5 items that List starts to provide any performance benefits over AddItem. I've appended the code/results at the bottom of the post.


    Quote Originally Posted by Mordred
    However, I don't think the number of lines of code will make run-times quicker or slower
    Correct. You recently saw a great example of this over at XVBT, where the longer version of the code was not only faster than the one-liner, it was more readable, robust and maintainable.





    Please Login or Register  to view this content.

    Please Login or Register  to view this content.
    Last edited by Colin Legg; 08-24-2011 at 05:27 PM.
    Hope that helps,

    Colin

    RAD Excel Blog

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Should .AddItem be Used or Not?

    My result:
    List : 3,840027
    Add item 5,710022

    using:
    Please Login or Register  to view this content.
    Last edited by snb; 08-24-2011 at 06:01 PM.

  10. #10
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Should .AddItem be Used or Not?

    My version of Kyle's code uses 1000 test iterations on a single listbox because the timer isn't sensitive to pick up any difference on a single iteration. To make each iteration equal, the state of the listbox needs to be the same (ie. cleared). This meant that I had to include .Clear to reset the Listbox for the AddItem method. It's a means to and end to make the tests equivalent - not a practical requirement - because the 'test environment' has a single listbox which is being hammered. I want to compare .AddItem and .List directly, so .Clear needs to accompany the .List method too in order to maintain the status quo. It doesn't matter, for timing purposes, that the .List method 'clears' the Listbox in itself - we have to keep the playing field level to make it a fair test. If you include .Clear for both so that you are directly comparing the speed of the .AddItem and .List calls alone, you might see that .AddItem is quicker for 5 items. I did, anyway. An alternative test would be to set up 2000 listboxes, iterate through 1000 of them using .List, iterate through the other 1000 using .AddItem, and compare how long they took - that way you avoid using .Clear altogether.

    But I don't want to get side-tracked on how many items it takes to make one faster than the other: the point of my last post was that they are different tools and both have their rightful place in the developer's toolkit.
    Last edited by Colin Legg; 08-25-2011 at 04:17 AM.

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

    Re: Should .AddItem be Used or Not?

    Also bear in mind that AddItem can be used to append values when there are already some in the control (provided the existing values are not bound), whereas List cannot (directly). They're different tools for different situations.
    Taking this a little further, I have a requirement to bulk add items to a list box already populated and didn't want to be hit with the .AddItem slow down, so I bodged together the following - despite the looping it is still much faster than looping .AddItem calls (for substantial additions):

    In the Class:
    Please Login or Register  to view this content.
    An example implementation:
    Please Login or Register  to view this content.
    Last edited by Kyle123; 08-25-2011 at 07:29 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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