+ Reply to Thread
Results 1 to 4 of 4

Using Array in VBA to eliminate blank columns in a row

Hybrid View

  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,331

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

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

    Sub test()
    
    Dim iRange As Range: Set iRange = Range("D2:H2")
    Dim oRange As Range: Set oRange = Range("D3:H3")
    Dim vIArray
    Dim vOArray
    Dim i As Long
    Dim j As Long
    Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    
    ' store input range in array
    vIArray = awf.Transpose(awf.Transpose(iRange))
    ' resize output array to same dimensions as input array
    ReDim vOArray(LBound(vIArray) To UBound(vIArray))
    ' initialise index (j) for output array
    j = LBound(vOArray)
    ' loop through input array
    For i = LBound(vIArray) To UBound(vIArray)
        ' if not blank
        If vIArray(i) <> "" Then
            ' move to output array
            vOArray(j) = vIArray(i)
            ' increment output index (j)
            j = j + 1
        End If
    Next 'i
    
    ' restore output array to worksheet
    oRange = vOArray
    
    End Sub

    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

    Sub Blankless_Array()
    
        Dim myArray(), myData As Range, Cell_Data As Range, ct As Integer, i As Integer
    
        Set myData = Worksheets(1).Range("d3:h3")
    
        'Copy all data in row and paste values in row3 for array purposes
        Range("A2:h2").Select
        Selection.Copy
        Range("A3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                      :=False, Transpose:=False
        Range("A3").Select
        Application.CutCopyMode = False
    
        ReDim myArray(myData.Count)
    
        ct = 0
        For Each Cell_Data In myData
            'don't add blanks to array
            If Cell_Data <> "" Then
                myArray(ct) = Cell_Data
                ct = ct + 1
            Else:
                ct = ct    ' + 1
            End If
        Next Cell_Data
    
        'overwrite range with array data
        Range("d3:h3").Value = myArray
    
    End Sub

  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,331

    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