+ Reply to Thread
Results 1 to 25 of 25

Code help: Copy only filtered range to ListBox

  1. #1
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Code help: Copy only filtered range to ListBox

    Hi guys,

    This is where I need and would appreciate your wisdom.

    I am soooooo close to solving this challenge i have been battling with for days; just need some guidance.

    Detail:

    I have a simple UserForm where one captures a record with some basic detail, as well as multiple line items in a ListBox.
    This all saves to a sheet (Sheet1), creating multiple rows.

    Then i have a feature where one would do a search for a record (to reprint or edit), using the same UserForm. The search lists the suggested items in ListBox3, and when one dbl click on the ListBox3 item, it filters Sheet1 accordingly, and updates the UserForm with that specific record.

    Here is my challenge:

    I added a sub updateListBox () to copy back the data of the multiple line items captured in ListBox1, back into the ListBox1. BUT, the problem is, it does not copy only the filtered data as it is supposed to. It copies all the data of my Range. It is almost as if the filter does not "trigger".

    This is what i want:

    After I have filtered my data using my filter options, only the filtered section of my range must be copied to my ListBox1.

    I have attached my sample workbook.

    Thank you very much in advance!
    Attached Files Attached Files
    Last edited by onmyway; 02-06-2015 at 04:50 AM.

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Code help: Copy only filtered range to ListBox

    Please do not start new threads with the same topic without providing a link.
    David
    (*) Reputation points appreciated.

  3. #3
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Code help: Copy only filtered range to ListBox

    Sorry David. i tried re-posting my thread in a different way, as i was not getting any responses.

  4. #4
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Code help: Copy only filtered range to ListBox

    The answer why it's not working is that you can't use Rows.Count on a non-contiguous range. It will stop at the end of first range. That's why the first selection works, but not on the rest.

    Here's a link to a dicussion about that and ways to address it. (This is why I don't like using autofilter to fill controls. I always use Range.Find.)

  5. #5
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Code help: Copy only filtered range to ListBox

    Hi David and all,

    I have made some headway, thanks to David's link to a similar discussion (https://social.msdn.microsoft.com/Fo...t?forum=isvvba).

    The code almost works, but it does not work with my named Range (RecordData), but with Cell values ("E2:J12") (http://www.mrexcel.com/forum/excel-q...-userform.html). This causes lines to be created that does not exist (duplication). i think one should add a count variable?

    This is an idea of a count method:

    Please Login or Register  to view this content.
    This is the code of my Search/Filter function (that almost works):

    Please Login or Register  to view this content.
    Herewith my latest workbook.

    Thank you all!
    Attached Files Attached Files

  6. #6
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Code help: Copy only filtered range to ListBox

    This is the code of my Search/Filter function (that almost works):
    I'm sure it's obvious to you what doesn't work. Can you explain to use now? It seems to put the values from the Listbox 1 into listbox3.

  7. #7
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Code help: Copy only filtered range to ListBox

    hi David. I made a short movie explaining the use and challenges:

    https://dl.dropboxusercontent.com/u/...A%20Help_3.mp4

    Note: You might have to right click on the movie controls, and save movie as, to view the movie.

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

    Re: Code help: Copy only filtered range to ListBox

    Not really sure how you want it
    This is only my guess
    Replace ListBox3_DblClick with
    Please Login or Register  to view this content.
    Replace updateListBox with
    Please Login or Register  to view this content.
    Last edited by jindon; 02-05-2015 at 05:29 AM.

  9. #9
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Code help: Copy only filtered range to ListBox

    Hi jindon,

    thanks for the feedback. i like the idea of not filtering the data. But, when I save my data back to my sheet after editing etc., it creates new line in my sheet. I would like to search for a record, update the UserForm with the data (as per your method), but then save it back to my sheet in the exact same range.

    Any ideas?

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

    Re: Code help: Copy only filtered range to ListBox

    Then I think it will be much simpler than what you currently have.

    No filter at all...

    You want to search what is in ListBox3 and show the result in ListBox1,

    Then you want to update the data with your change in user form.

    It it correct?

  11. #11
    Registered User
    Join Date
    02-04-2015
    Location
    South Africa
    MS-Off Ver
    10
    Posts
    4

    Re: Code help: Copy only filtered range to ListBox

    Not the best or most efficient solution, but it works:

    Please Login or Register  to view this content.
    Forgot this:

    Please Login or Register  to view this content.

    Oh - just want to say that I don't like this solution, but it does work!!
    Last edited by psyinx; 02-05-2015 at 06:25 AM.

  12. #12
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Code help: Copy only filtered range to ListBox

    Quote Originally Posted by jindon View Post
    Then I think it will be much simpler than what you currently have.

    No filter at all...

    You want to search what is in ListBox3 and show the result in ListBox1,

    Then you want to update the data with your change in user form.

    It it correct?
    That is correct

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

    Re: Code help: Copy only filtered range to ListBox

    OK, I will take a close look at your file tomorrow and post the code if you still need help.

    I will be very busy rest of the day....

  14. #14
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Code help: Copy only filtered range to ListBox

    hi psyinx

    Thanks for the help!

    I get a Sub or Function not defined for: ReDimPreserve

    Please Login or Register  to view this content.

  15. #15
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Code help: Copy only filtered range to ListBox

    hi psyinx

    Thanks for the help!

    I get a Sub or Function not defined for: ReDimPreserve

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    02-04-2015
    Location
    South Africa
    MS-Off Ver
    10
    Posts
    4

    Re: Code help: Copy only filtered range to ListBox

    Posted the redim method in original post

  17. #17
    Registered User
    Join Date
    02-04-2015
    Location
    South Africa
    MS-Off Ver
    10
    Posts
    4

    Re: Code help: Copy only filtered range to ListBox

    Posted the ReDim method in original post separate from double click event

  18. #18
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Code help: Copy only filtered range to ListBox

    Ahhhhh!

    That is the problem with working with multiple workbooks and methods and sheets.....

    thanks, works great!

  19. #19
    Registered User
    Join Date
    02-04-2015
    Location
    South Africa
    MS-Off Ver
    10
    Posts
    4

    Re: Code help: Copy only filtered range to ListBox

    Cool - just note that there are some assumptions in that method:

    1. The "loop termination" needs to be re-considered
    2. The original array size needs to be re-considered

    But it is none the less a starting point.

    Good luck!

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

    Re: Code help: Copy only filtered range to ListBox

    onmyway

    I need a brief explanation of each commandbuttons.

  21. #21
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Code help: Copy only filtered range to ListBox

    Quote Originally Posted by jindon View Post
    onmyway

    I need a brief explanation of each commandbuttons.
    hi jindon,

    there aren't many commandbuttons. the main btn is basically to save a record to the Sheet1. Most of the functionality lies in this: to search for an existing record, to find possible matches, to update the UserForm with the record.

    I made a video clip explaining my needs. I think this might help you to understand:

    https://dl.dropboxusercontent.com/u/...%20Help_3.mp4\

    You might have to Right click on the video controls and "save as" to view the clip.

    Note: I have attached 2 workbooks. 1 with psyinx method, 1 with your WIP method.
    Last edited by onmyway; 02-06-2015 at 03:28 AM.

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

    Re: Code help: Copy only filtered range to ListBox

    The try the attached
    Attached Files Attached Files

  23. #23
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Code help: Copy only filtered range to ListBox

    hi jindon,

    Great work and coding! thank you very much.

    I see however, it does not allow you to save the new lines created in the listbox. I.e., when you search for a record, edit the listbox item, and perhaps add an additional listbox item, it does not create a new row in the sheet for the new line.

    Any suggestions?

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

    Re: Code help: Copy only filtered range to ListBox

    If the item selected from listbox1, save button updates the row in sheet1 from the all the text boxes except GUID. (should not be touched)

    Other change I made was to show only unique values in ListBox3 as search text entered.

    I didn't touch other, so I don't understand what you are asking.

    What do you want to do with Add button?

  25. #25
    Forum Contributor onmyway's Avatar
    Join Date
    09-03-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2013
    Posts
    386

    Re: Code help: Copy only filtered range to ListBox

    hi jindon

    I really appreciate all your help. big thumbs-up to you!

    Most of my problems have been resolved by you and psyinx.

    I am going to close this thread, and start a new one with my new challenges. Please look out for it if you'd like to help!

+ 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. fill listbox with filtered sheet. Need Assistance with code.
    By Andrew Andromeda in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-02-2015, 06:11 AM
  2. copy paste macros
    By rrk2008 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2014, 06:42 AM
  3. Copy filtered range
    By jomili in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-16-2010, 08:47 AM
  4. How to copy a filtered range ?
    By gaftalik in forum Excel General
    Replies: 2
    Last Post: 11-25-2005, 12:56 PM
  5. [SOLVED] Copy filtered range
    By Milo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2005, 10:05 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