+ Reply to Thread
Results 1 to 3 of 3

Odd behaviour with lists and .list

  1. #1
    Registered User
    Join Date
    11-05-2012
    Location
    Oxford
    MS-Off Ver
    Excel 2010
    Posts
    5

    Odd behaviour with lists and .list

    Hi Everyone

    I am pretty new to VBA so this might be well understood (and posted elsewhere but I did look and couldn't find it so sorry if this has been answered many times)

    Basically I am try to use two list boxes (ListBox1, ListBox2). ListBox2 is populated data derived from what is chosen from the list in ListBox1. The list in ListBox2 is variable and ranges from 1 item to 350 items dependent on the value picked up in ListBox1

    Here's the issue when there are more than one items generated for ListBox2 everything is fine and ListBox2 is populated. But if only one item is found by choosing a certain ListBox1 value then my code crashes.

    Here's the code snippet: The derived list values are placed in Row K (11), Counter is a loop counter for number of entries found for listBox2

    Please Login or Register  to view this content.
    This ok until Counter -1 = 1 i.e the expression is ".List = Range(Cells(1, 11),Cells(1, 11)).value" (There's a -1 because counter is counting in a loop adding +1 each condition so will come out of the loop one too high) giving Run-Time Error 381: Could not set the list property: Invalid Property Array Index

    However if I "pad" the value like the code snippet below it works

    Please Login or Register  to view this content.
    So the .list value is two discreet cells in the sheet rather than one. The value in Cells(1,12)="" but this seems to satisfy VBA.

    Can anyone tell me what's going on?

    Thanks, Nick
    Last edited by JosephP; 11-12-2012 at 08:40 AM. Reason: add code tags

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Odd behaviour with lists and .list

    hi ngmp, welcome to Excelforum, VB help file reference for List property:

    Use List to copy an entire two-dimensional array of values to a control. Use AddItem to load a one-dimensional array or to load an individual element.

    This code should explain what is treated as array if array is created from a sheet range:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-05-2012
    Location
    Oxford
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Odd behaviour with lists and .list

    Hi watersev

    Thank you for the welcome!

    Also thank you for the reply which turned on the light bulb in my brain - as you indicated that I was making List think I was accessing a 2-dimensional array (which is blindingly obvious now you've pointed it out!!!) but in fact I wasn't meaning to. So the answer, as you point out is to use .AddItem and ListBox2.Clear to do it. Now it works fine. Thank you so much for this insight....I feel like Homer Simpson though!

    Nick

+ 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