+ Reply to Thread
Results 1 to 11 of 11

Extract specific elements from 2d array and add to new 2d array

  1. #1
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Extract specific elements from 2d array and add to new 2d array

    Hello.
    Looking for a way to extract elements from a two dimensional array, which meet a specified criteria, and copy to a new two dimensional array.

    Or...
    remove all elements from array if they do not meet the specified criteria.


    Please Login or Register  to view this content.
    Last edited by kosherboy; 10-13-2021 at 12:09 AM.

  2. #2
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,377

    Re: Extract specific elements from 2d array and add to new 2d array

    So what are the criteria

    This is an example of how you clear specific items of an array.
    Not sure what you really want to do with cells that meet the criteria. In this case I just gave them an empty value and put everything back in the sheet.


    Please Login or Register  to view this content.
    Last edited by JEC.; 10-12-2021 at 04:21 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Extract specific elements from 2d array and add to new 2d array

    could be anything, let's say Arr(i, 1) = 1:

    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Extract specific elements from 2d array and add to new 2d array

    Not sure what you really want to do with cells that meet the criteria. In this case I just gave them an empty value and put everything back in the sheet.

    Exactly as you have it, copy the new or modified array to a worksheet. I found this to be the fastest way to paste.
    I have a question though, you didn't entirely remove the elements from the array, this results in many skipped rows. Looking to paste new array as is. So let's say 7 records meet the criteria out of the 244 records, therefore would like to paste the new array (of 7 records) into Range ("A2:H8")

    In addition, your code is only removing part of each record, not the entire record.
    Last edited by kosherboy; 10-12-2021 at 04:48 PM.

  5. #5
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,377

    Re: Extract specific elements from 2d array and add to new 2d array

    But your array is larger than 7 records. How do you know where to put the records, which meet the criteria, in the new array?
    It seems like you are talking about rows.

    Do you only want to loop through column A?

    Please Login or Register  to view this content.
    Last edited by JEC.; 10-12-2021 at 05:07 PM.

  6. #6
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Extract specific elements from 2d array and add to new 2d array

    I'm trying to achieve the following:
    Extract data from 8 columns with dynamic number of rows and copy to array that has 8 different "columns".
    The reason I don't already specify the criteria, when copying the range, is because it takes longer to iterate through an excel range than it does on an array.
    I then want to iterate through the array and copy the records (which meet the criteria) to a new array (or remove irrelevant records) and paste that array to a new sheet.
    If I were to skip the arrays and just deal with excel range then process is considerably slow.

  7. #7
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,377

    Re: Extract specific elements from 2d array and add to new 2d array

    Still not very clear to me unfortunately. You could give the macro in post 5 a try.
    If that's still the wrong result, you should post some sample data.

  8. #8
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,001

    Re: Extract specific elements from 2d array and add to new 2d array

    Quote Originally Posted by kosherboy View Post
    I'm trying to achieve the following:
    and paste that array to a new sheet.
    If the goal is to paste the result to a range then it should work:
    Please Login or Register  to view this content.
    the NewArr could have empty rows at the bottom but because I set the range only to x row then only the non empty part will return to the range

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Extract specific elements from 2d array and add to new 2d array

    Quote Originally Posted by kosherboy View Post
    Extract data from 8 columns […] and copy the records (which meet the criteria) to a new sheet.
    Just using a fast enough advanced filter, done with two codelines …

  10. #10
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Extract specific elements from 2d array and add to new 2d array

    Quote Originally Posted by Akuini View Post
    If the goal is to paste the result to a range then it should work:
    .................
    the NewArr could have empty rows at the bottom but because I set the range only to x row then only the non empty part will return to the range
    Very well written, and exactly what I was looking for!
    Thank you!
    Last edited by kosherboy; 10-13-2021 at 12:12 AM.

  11. #11
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,001

    Re: Extract specific elements from 2d array and add to new 2d array

    You're welcome, glad to help & thanks for the feedback.

+ 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. find array length got using split feature and valdate array elements
    By malathi1972 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-21-2018, 04:51 AM
  2. VBA Subordinate and Function That Replace Specific Elements in An Array
    By HanadiS in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-28-2014, 05:34 PM
  3. Array Storage Problem: Array Elements Deleted on 'End' Command
    By AidenS in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-15-2014, 10:38 AM
  4. [SOLVED] Removing specific elements from an Array based on matching elements in a different Array
    By jamesdeuce in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-24-2014, 10:35 PM
  5. help assigning elements of dictionary to array and then pasting array into worksheet
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-05-2013, 10:37 AM
  6. [SOLVED] Populate Listbox with all rows of a dynamic array where elements of a single array match.
    By Tayque_J_Holmes in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-07-2012, 04:54 AM
  7. Refer to specific elements in an array
    By MCCCLXXXV in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-13-2009, 02:31 PM

Tags for this Thread

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