+ Reply to Thread
Results 1 to 21 of 21

Populate array with large filtered range

  1. #1
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Populate array with large filtered range

    Hi all.

    There has got to be an easy way to do this. I am trying to populate a large sheet with many columns and many rows into an array.

    The caveat is that the rows are filtered.

    Assuming declarations...

    Please Login or Register  to view this content.
    I would rather not loop xlCellTypeVisible and take forever to loop every row and every column value into an array.

    Is there an easier way to populate an array with filtered rows?


    Cheers

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Populate array with large filtered range

    hmmmm....I bet there is an expert here that knows. have you tried selecting the entire range while recording the movements with the macro recorder? VBA has a function in it called SPLIT() but i don't think that would because you have to have a delimiter in the arguments. what about answer #2 here?

    https://stackoverflow.com/questions/...a-range-in-vba

    or simply, to get overloaded with options:

    https://www.google.com/search?q=vba+...range+to+array


  3. #3
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Re: Populate array with large filtered range

    Thanks for the response.

    Answer Number 2 in your suggested page works for a single dimension array, which is not filtered. I need my array to collect the detail from a filtered sheet.

    Using the Macros recorder produces Copy and Paste Selection Code, which doesn't work to paste into an array.

    However, I appreciate the time taken to provide a response.

    Cheers

  4. #4
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Populate array with large filtered range

    Quote Originally Posted by Journeyman3000 View Post
    Answer Number 2 in your suggested page works for a single dimension array, which is not filtered. I need my array to collect the detail from a filtered sheet.
    so do you need a one-dim output or a 2-dim output? here is 1:
    Please Login or Register  to view this content.
    and putting the data into a 2 dimensional array should be just as easy. but based on what you asked for, I'm not sure why you would want to do that, because every data point (value) represents a column and a row already.

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

    Re: Populate array with large filtered range

    Quote Originally Posted by Journeyman3000 View Post

    I would rather not loop xlCellTypeVisible and take forever to loop every row and every column value into an array.

    Is there an easier way to populate an array with filtered rows?
    Prepare hidden sheet and copy filtered data to a hidden sheet and get data from there without loop.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,283

    Re: Populate array with large filtered range

    You can also sort the source sheet on the filter columns so that you end up with as few discrete areas as possible when filtering, then loop through the areas of the visible cells, load each area into an array and then iterate that populating your master array.
    Remember what the dormouse said
    Feed your head

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,420

    Re: Populate array with large filtered range

    I think this may be the non-looping concept jindon proposed (although he may have had different coding in mind) except that I chose to copy to the active sheet (which I am assuming it the sheet with the filtered data), load the array and then delete the copied data...
    Please Login or Register  to view this content.
    Last edited by Rick Rothstein; 08-18-2020 at 12:52 PM. Reason: Replace original code with slightly simpler code.

  8. #8
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Re: Populate array with large filtered range

    @Rick RothStein

    This solution appears to be exactly what I'm looking for.

    However, I am wondering if there is a way to do this in code (Memory) rather than creating a Temporary sheet.

    If I had to create a temporary sheet, I would have to make it invisible - or extra hidden then have to deal with the issues related to that.

    Otherwise, this is excellent. Thank you for your suggestion.

    Cheers

  9. #9
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,420

    Re: Populate array with large filtered range

    I did not create a temporary sheet... I simply copied the data into an unused area under the existing data, loaded it into an array and then deleted what I had copied. Everything is taking place on the active sheet which I assumed is where your data is located... no extra or hidden sheets are required with the code snippet I posted.
    Last edited by Rick Rothstein; 08-18-2020 at 05:50 PM.

  10. #10
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Re: Populate array with large filtered range

    I can't mess with the original data sheet. That's why I modified your code to use a temp sheet.

    I tested your code to make sure it worked and my philosophy is always to 'Make it work before I make it pretty'

  11. #11
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,420

    Re: Populate array with large filtered range

    Quote Originally Posted by Journeyman3000 View Post
    I can't mess with the original data sheet.
    Even if that "messing" takes place in an unused area of the sheet? What if we created a workbook on the fly, copied the data and then retrieved it from there and then closed that temporary workbook... would that approach be acceptable?



    Quote Originally Posted by Journeyman3000 View Post
    However, I am wondering if there is a way to do this in code (Memory) rather than creating a Temporary sheet.
    I guess we could loop through the cells of the visible cells and write the data to the array one cell at a time. Not sure how inefficient that might be though. Did you want to pursue that method?


    You might also want to wait for jindon to come back to this thread to see if he has any ideas on how to do this wholly (and efficiently) in memory.

  12. #12
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Re: Populate array with large filtered range

    Hi Rick

    I guess we could loop through the cells of the visible cells and write the data to the array one cell at a time. Not sure how inefficient that might be though. Did you want to pursue that method?
    Absolutely not. This entire exercise is to avoid looping through millions of cells.

    If I have no choice but to create a temporary sheet then I will have to live with it. I've made it work based on your input.

    My end goal is to make an array of only visible rows with non-concurrent columns (e.g. B:B, H:H,Z:Z). But for all my research I've not found an adequate solution to this problem, except by looping, which bites. Makes me cry.

    Thanks for help.

    Cheers

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

    Re: Populate array with large filtered range

    If you know the filtered criteria/column, it could be done.

  14. #14
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Re: Populate array with large filtered range

    I tried this:

    Please Login or Register  to view this content.
    But no happiness.

    My guess is I would have to loop through the temp sheet and remove the columns I don't need. more ugliness, but what else can I do.

    Cheers

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

    Re: Populate array with large filtered range

    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Re: Populate array with large filtered range

    Mate!

    Aside from the requirement to use a helper sheet, this is fantastic. I even set the numbering to see if it auto ordered the sheets, which it does.

    Excellent. Thank you for this.

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

    Re: Populate array with large filtered range

    I said
    Quote Originally Posted by jindon View Post
    If you know the filtered criteria/column, it could be done.
    Means if you know the criteria, it could all be done within a memory. No helper sheet.

  18. #18
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Re: Populate array with large filtered range

    I used the helper sheet to avoid using the original sheet as suggested

    This is the code

    Please Login or Register  to view this content.
    I tried making the varVisibleArray from the origin sheet, but I couldn't get it to work

  19. #19
    Registered User
    Join Date
    12-23-2012
    Location
    Macksville, Australia
    MS-Off Ver
    Excel 365
    Posts
    7

    Re: Populate array with large filtered range

    Quote Originally Posted by Journeyman3000 View Post
    I used the helper sheet to avoid using the original sheet as suggested

    I tried making the varVisibleArray from the origin sheet, but I couldn't get it to work
    If I have understood correctly then I don't think that you need a helper sheet and I don't think you even need to get the visible cells into an array as they can be transferred directly to the destination worksheet. My suggestion does have a double loop but it it looping within memory only and only relating to the visible rows in one column so I don't think you will find a performance problem.

    Give this a try with a copy of your workbook

    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    03-05-2013
    Location
    Sydney
    MS-Off Ver
    Excel 2010
    Posts
    301

    Re: Populate array with large filtered range

    Thank you so much Peter (And Jindon), and all other who responded to this challenge.

    This works very well, and is a ton faster than Before.

    I will now close this issue as this question is answered (so very brilliantly!)

    Thank you.

  21. #21
    Registered User
    Join Date
    12-23-2012
    Location
    Macksville, Australia
    MS-Off Ver
    Excel 365
    Posts
    7

    Re: Populate array with large filtered range

    Quote Originally Posted by Journeyman3000 View Post
    Thank you so much Peter (And Jindon), and all other who responded to this challenge.

    This works very well, and is a ton faster than Before.
    You're welcome. Glad we could 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. Paste array into filtered range
    By esbencito in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-04-2019, 11:34 PM
  2. [SOLVED] Populate Listbox from different workbook.Filtered range
    By sintek in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-15-2018, 05:44 AM
  3. [SOLVED] Populate a Listbox with Unique data of filtered range
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-23-2018, 02:35 AM
  4. Populate ListBox with Filtered Range
    By smartbuyer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-21-2015, 01:09 AM
  5. [SOLVED] Populate one array from another array and print new array as a range
    By Kaden265 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-07-2014, 07:52 AM
  6. Populate a textbox on Userform with data from a filtered range using VBA
    By newbi004 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-13-2014, 04:20 AM
  7. [SOLVED] Copying large range of data into filtered cells
    By zigojacko in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-16-2013, 04:11 AM

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