+ Reply to Thread
Results 1 to 15 of 15

Remove Blank Entries From a 2-D Array

  1. #1
    Registered User
    Join Date
    10-02-2017
    Location
    New Jersey
    MS-Off Ver
    2013
    Posts
    15

    Remove Blank Entries From a 2-D Array

    Hello -

    Thank you in advance for any help regarding this topic.

    I have a 2-D array initialized in my code with variable row and column counts. Within that array there are rows which begin with an empty entry. I cannot modify the input, so I am forced to remove the rows which begin with an empty entry from the array.

    My first attempt at a solution I created a second 2-D array with the same dimensions as the first 2-D array. I then used nested for-loops to traverse the first array and used an if statement to only add to the second array if the entry was non-empty. I thought this would work, but it still had the empty rows.

    Please Login or Register  to view this content.
    Any help on this would be greatly appreciated. I'm surprised I could not find a cookie cutter function online. This just seems like an exercise in traversing an array and removing entries, but I am struggling.

    Thank you.

    EDIT: To clarify a potential question, if a row in the array begins with an empty entry then the entire row will be empty.

    EDIT2: By the way this is all being done with an Excel VBA sub with a 2-D variant array.
    Last edited by Altec103; 10-03-2017 at 06:28 AM.

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

    Re: Remove Blank Entries From a 2-D Array

    Which do you want?

    Remove or empty?

  3. #3
    Registered User
    Join Date
    10-02-2017
    Location
    New Jersey
    MS-Off Ver
    2013
    Posts
    15
    Quote Originally Posted by jindon View Post
    Which do you want?

    Remove or empty?
    The blank entries in the array are already empty. I want to remove them. Below would be a small example of a 4 by 3 array:

    3 7 1
    - - -
    - - -
    2 9 7

    (where the dashes represent blank entries. After removal the array would be 2 by 3)

    3 7 1
    2 9 7

    Note I want to remove the entire row which begins with a blank entry, not just the individual cell.
    Last edited by Altec103; 10-03-2017 at 06:27 AM.

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

    Re: Remove Blank Entries From a 2-D Array

    This will remove the row if the first element of each row is empty
    Please Login or Register  to view this content.

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Remove Blank Entries From a 2-D Array

    A simple method would be to loop the rows of the array and count the non-blank entries. That then gives you the size for the second array which you populate in a second loop like you have currently.
    Alternatively you might populate the second array in transposed fashion, using a counter for the row to fill, so that you can use Redim Preserve to resize it at the end and then transpose it back.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  6. #6
    Registered User
    Join Date
    10-02-2017
    Location
    New Jersey
    MS-Off Ver
    2013
    Posts
    15

    Re: Remove Blank Entries From a 2-D Array

    Hi Jindon -

    Thank you, this worked. However, there is one issue. This seems to produce trailing N/As at the end of the new array (one N/A row for each empty row removed). Any ideas what might be causing this?

  7. #7
    Registered User
    Join Date
    10-02-2017
    Location
    New Jersey
    MS-Off Ver
    2013
    Posts
    15

    Re: Remove Blank Entries From a 2-D Array

    Hi xlnitwit -

    Thanks for the answer. I will try the counting method as it looks like Jindon has provided an answer using the transpose method.

    Out of curiosity, any idea which method would be less computationally complex? I will be working with over 10,000 rows, so I do want to keep performance in mind.

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Remove Blank Entries From a 2-D Array

    Redim Preserve is quite an expensive operation as it creates a copy of your array each time. Rather than doing that in a loop, I would prefer to do it once at the end after calculating the actual count. What are you actually doing with the array once you have removed the blank rows?

  9. #9
    Registered User
    Join Date
    10-02-2017
    Location
    New Jersey
    MS-Off Ver
    2013
    Posts
    15

    Re: Remove Blank Entries From a 2-D Array

    I attempted to follow your instructions and created a 2nd array with new dimensions. But I am having trouble getting it filled without throwing an indices error. Below is my second attempt:

    Please Login or Register  to view this content.
    Also, what do you think of Jindon's answer? It works with the exception of the trailing N/As which I'm hoping he can debug because I wasn't able to fix. And I'm just writing the array to the sheet after removing the blanks. Maybe I can solve it there? Below is where I display the array:

    Please Login or Register  to view this content.

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

    Re: Remove Blank Entries From a 2-D Array

    Quote Originally Posted by Altec103 View Post
    Hi Jindon -

    Thank you, this worked. However, there is one issue. This seems to produce trailing N/As at the end of the new array (one N/A row for each empty row removed). Any ideas what might be causing this?
    Can not replicate the issue, if you post your full code, it would help.

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

    Re: Remove Blank Entries From a 2-D Array

    Should be
    Please Login or Register  to view this content.

  12. #12
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Remove Blank Entries From a 2-D Array

    Your issue isn't with jindon's code, it's the fact that you are sizing the range based on the original array dimensions. You should use the Ubound of the new array to size it.

    Edit: i.e. what jindon said in post #11!

  13. #13
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Remove Blank Entries From a 2-D Array

    Quote Originally Posted by Altec103 View Post
    I attempted to follow your instructions and created a 2nd array with new dimensions. But I am having trouble getting it filled without throwing an indices error.
    You need a counter variable to populate the second array
    Please Login or Register  to view this content.

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

    Re: Remove Blank Entries From a 2-D Array

    Altec103

    How are you populating initial array?

  15. #15
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Remove Blank Entries From a 2-D Array

    I will be working with over 10,000 rows, so I do want to keep performance in mind.
    This one took 0.04 sec. to filter 5030 filled rows out of 10.000 rows array.

    Please Login or Register  to view this content.
    Last edited by bakerman2; 10-03-2017 at 02:08 PM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

+ 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. Replies: 3
    Last Post: 06-15-2014, 11:31 AM
  2. [SOLVED] Concatenate If Blank - remove blank line if first cell is blank
    By ker9 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 05-27-2014, 02:14 PM
  3. Remove blank entries from data validation list
    By Icarus in forum Excel General
    Replies: 7
    Last Post: 11-08-2012, 01:56 AM
  4. Need VBA code to remove entries if there are duplicates (remove them totally)
    By BrandonFromSingapore in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-13-2012, 12:50 AM
  5. Consolidate Array to Remove Blank/Zero
    By gsurge in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-17-2011, 08:55 AM
  6. removing blank entries from an array
    By TheIrishThug in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-19-2005, 05:34 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