+ Reply to Thread
Results 1 to 4 of 4

Using Array in VBA to eliminate blank columns in a row

  1. #1
    Registered User
    Join Date
    01-21-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2007
    Posts
    2

    Using Array in VBA to eliminate blank columns in a row

    Hi All,
    I have a table with the following:
    ..........A............B................C...............D..........E ........F...........G............H
    1...... Name.....Address..........Tel........... Fruit1....Fruit2...Fruit3....Fruit4.......Fruit5
    2...... Detail1......................Detail2.........Txt1................Txt2....................Txt3
    3...... Detail1......................Detail2.........Txt1......Txt2....Txt3

    (Dots are used as spacers to keep columns in line)

    The columns for Name, address & Tel are allowed to have blank columns.
    The columns for Fruit1 to Fruit5 need to be consolidated to the left, leaving no blank columns.
    Row 2 is the original and row 3 is how it needs to look.

    I can do this using an array formula in D3:H3
    {=IFERROR(INDEX($D$3:$H$3, SMALL(IF(($D$3:$H$3="")+ISERROR(($D$3:$H$3), "", COLUMN(($D$3:$H$3)-MIN(COLUMN(($D$3:$H$3))+1), COLUMN(A1))),"")}

    But I need to do this in VBA.
    Can anyone assist?

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,528

    Re: Using Array in VBA to eliminate blank columns in a row

    One way, though I'm sure there are better:

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    01-21-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Using Array in VBA to eliminate blank columns in a row

    Hi Trevor,

    I actually came up with a very similar answer, but I appreciate your input.

    Regards
    Neil

    Please Login or Register  to view this content.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,528

    Re: Using Array in VBA to eliminate blank columns in a row

    You're welcome. Thanks for the rep.

    For what it's worth, my solution is quicker. But, for small amounts of data, it probably won't be that significant. Timing my routine is between 4 and 6 milliseconds; yours is between 23 and 25 milliseconds. Not a big deal when you're looking at milliseconds but, if you end up processing lots of data ...

    See attached sample workbook.

    HTML Code: 


    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.
    Attached Files Attached Files

+ 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. Eliminate Duplicates from an Array / Only Original Names
    By RS15 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-21-2013, 01:31 PM
  2. [SOLVED] Help need to check mark + Eliminate blank cells
    By Anka in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 08-29-2012, 03:33 PM
  3. VBA Macro to eliminate cells with a blank value
    By paulxl in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-27-2012, 10:23 AM
  4. Eliminate Blank Cells
    By ruby1766 in forum Excel General
    Replies: 20
    Last Post: 06-18-2009, 05:24 PM
  5. eliminate the blank total in my pivot table
    By carsto in forum Excel General
    Replies: 3
    Last Post: 11-30-2006, 04:19 AM

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