+ Reply to Thread
Results 1 to 22 of 22

Populate Listbox with all rows of a dynamic array where elements of a single array match.

  1. #1
    Registered User
    Join Date
    08-06-2012
    Location
    Shipley, UK
    MS-Off Ver
    Excel 2010
    Posts
    69

    Populate Listbox with all rows of a dynamic array where elements of a single array match.

    I currently have a userform that, upon intialization, creates a public dynamic array based on a table range, test data currently 45 rows by 22 columns.

    On the userform there is a textbox01 that allows a user to enter text, this text is then entered into an array base on single words, i.e. textbox01 sting = "Hello how are you" would populate the array with 4 single words.

    What I am trying to do is to create a routine that populates listbox01 with only rows of the dynamic array where all element of the single array are present in the dyna,ic array

    Example

    Dynamic Array Row 15 = "One" "Two" "Three" "Four" "Five" "Six" "Seven" "Eight" "Nine" "Ten"

    Sindle array contains "Two", "Five" and "Eight", resulting in the row 15 of the array being populated into listbox01.

    At the moment my code only searches the first word entered into textbox01 which if match is match is made the listbox is populated, My Current Code is;

    Please Login or Register  to view this content.
    Last edited by Cutter; 08-06-2012 at 09:02 AM. Reason: Added code tags

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    I reckon you oughta create a 1 dimensional array that joins each row of your SDVar array into one string separated by a delimiter, then you can just filter that array repeatedly for each of the four items in turn plus the delimiter (so filter the array for item1, then filter the filtered array for item2 and so on) then the resulting array will contain all the items that match everything. then loop that array splitting the text on the delimiter and add to the listbox.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    08-06-2012
    Location
    Shipley, UK
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    how do you join a dynamic array?

    the join(SDVAR,",") kicks out an error message

    Cheers

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    you have to go row by row

    ---------- Post added at 05:53 AM ---------- Previous post was at 05:43 AM ----------

    basic idea is this
    Please Login or Register  to view this content.
    note this assumes you are matching the words against columns-i.e. one word per cell.

  5. #5
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    @ Tayque_J_Holmes

    Welcome to the forum.

    Please notice that code tags have been added to your post. The forum rules require them so please keep that in mind and add them yourself whenever showing code in any of your future posts. To see instructions for applying them, click on the Forum Rules button at top of the page and read Rule #3.
    Thanks.

  6. #6
    Registered User
    Join Date
    08-06-2012
    Location
    Shipley, UK
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    Hi there,

    I have briefly tested the code and unfortunately I am unable to get it to work, I have attached a copy of the of the excel test file I used

    Cheers
    Attached Files Attached Files

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    as I mentioned in my post the code assumes one word per cell as in your initial post. since that is not the case you need
    Please Login or Register  to view this content.
    but depending on the search data you may get additional matches (eg one of your search terms matches part of a word such as searching for 'ford' and the data contains 'afford')

  8. #8
    Registered User
    Join Date
    08-06-2012
    Location
    Shipley, UK
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    Hi,

    I have added the code and is works great, just wondering how you then split the delimited string in v1ddata
    adapted code:

    Please Login or Register  to view this content.

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    loop through v1DData and use additem on the listbox, then split each item in v1DData (using Split to reverse the Join) into a temporary array and populate the listbox using whichever columns you want.

  10. #10
    Registered User
    Join Date
    08-06-2012
    Location
    Shipley, UK
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    Sorry to be a pain but am just wondering if you could possibly post the additional code element, and still learning the vba

  11. #11
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    I've converted the code to a function and added a userform to demonstrate
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    08-06-2012
    Location
    Shipley, UK
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    Hi JP

    thank you for the updated excel doc, at first when I inputted for example "Holmes" (part of the supplier name) into the textbox and click the command button, the listbox would populate with all records, then when I added to the textbox for PK07XBB (registration) keep getting an error message "Script out of range".

    I changes the private sub routine to textbox1_change, and then entered "Holmes Ford" which seemed to work, but I noticed that as you input text into the textbox the data that populates the listbox decreases, with the first word it populates all 22 fields per row, reducing as you input the second word. I then tried "Holmes PK07XBB" and it returned nothing.

    I'm not familiar with functions, and therefore and a little lost as to what is causing the problem.

    Kind Regards

    Tayque

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    I apologize - there was an error in the code
    Please Login or Register  to view this content.
    I suggest you don't use the change event as it will trigger the code with every keystroke. note when entering the PK07 XBB you must have the space in the middle for it to match (the code will treat each part as a separate word but I figure the chances of both parts appearing separately in a different row are small?)

  14. #14
    Registered User
    Join Date
    08-06-2012
    Location
    Shipley, UK
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    Hi JP,

    that is brilliant, just one last thing, how do I populate listbox1 with only certain parts of the array, i.e., supplier, customer, etc

    Cheers

    Tayque

  15. #15
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    two options are to populate the initial array with only the columns you want (and in the order you want) or to loop through the array the function returns using AddItem instead of using List to put the whole array in at once. for the second option for instance
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    08-06-2012
    Location
    Shipley, UK
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    Sorry to be a pain, but I am now getting the error message "Could not set list property. Invalid property array index"

    Please Login or Register  to view this content.

  17. #17
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    what is 'stockdata'?

  18. #18
    Registered User
    Join Date
    08-06-2012
    Location
    Shipley, UK
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    Good Morning, apologies for the delay in coming back to you, for some reason I couldn't access the www.excelforum.com domain.

    Stockdata refers to an updating range sheets(stock_data).range("A2:V46")

  19. #19
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    how and where is that variable declared and initialized?

  20. #20
    Registered User
    Join Date
    08-06-2012
    Location
    Shipley, UK
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    Please code below:

    Please Login or Register  to view this content.

  21. #21
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    my bad-you need
    Please Login or Register  to view this content.
    as the list property is zero based. hope that works as I need to get some sleep now :-)

  22. #22
    Registered User
    Join Date
    08-06-2012
    Location
    Shipley, UK
    MS-Off Ver
    Excel 2010
    Posts
    69

    Re: Populate Listbox with all rows of a dynamic array where elements of a single array mat

    that works a treat, thank you very much for your help, night :-)

+ 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