Solved

Delete empty cells and merge columns

Posted on 2011-02-15
4
634 Views
Last Modified: 2012-05-11
I have a rather large worksheet with over 50 columns.  I need these columns combined into one column, and manual cut and paste would take too long. And there are empty cells, I also need them to be removed. Finally all the columns needs to be merged into A. Any ideas? Thanks
file.xls
0
Comment
Question by:mmcompact
  • 2
4 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 34901834
One Excel solution is to go to column 51 and enter a formula like =SUBSTITUTE(A1 & "," & B1 & "," & C1..., ",,", ",")

you could actually create an Excel formula to create this for you, but if it's a 1-time deal, maybe some VBA would be better.

Sub MergeColumns()
  For iRow=1 to iLastRow
    strMerge = ""
    For iColumn=1 to 50
      if Cells(iRow, iColumn).Value <> "" then
        strMerge = strMerge & "," & Cells(iRow, iColumn).Value
      End If
      Cells(iRow, 51).Value = mid(strMerge,2)
    Next iColumn
  Next iRow

' optionally add extra code to delete columns 1-50
End Sub
0
 

Author Comment

by:mmcompact
ID: 34901876
have you tried it on my attached file?
0
 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
ID: 34901888

Using arrays does this quickly

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/A_2684-Using-varinat-arrays-in-VBA-for-large-scale-data.html

Your file with working code and button attached

Cheers

Dave
Sub QuickConcant()
    Dim rng1 As Range
    Dim rng2 As Range
    Dim lngRow As Long
    Dim lngCnt As Long
    Dim X
    Dim Y
    Dim ws As Worksheet
    Set rng1 = Cells.SpecialCells(xlConstants)
    ReDim Y(1 To rng1.Cells.Count, 1 To 1)
    For Each rngarea In rng1.Areas
        'The most common outcome is used for the True outcome to optimise code speed
        If rngarea.Cells.Count > 1 Then
            'If there is more than once cell then set the variant array to the dimensions of the range area
            'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks
            X = rngarea.Value2
            For lngRow = 1 To rngarea.Rows.Count
                For lngCol = 1 To rngarea.Columns.Count
                    lngCnt = lngCnt + 1
                    Y(lngCnt, 1) = X(lngRow, lngCol)
                Next lngCol
            Next lngRow
            'Dump the updated array sans leading zeroes back over the initial range
            rngarea.Value2 = X
        Else
            'caters for a single cell range area. No variant array required
            lngCnt = lngCnt + 1
            Y(lngCnt, 1) = rngarea.Value2
        End If
    Next rngarea
    Set ws = Worksheets.Add
    ws.[a1].Resize(UBound(Y, 1), 1) = Y

End Sub

Open in new window

concat.xls
0
 

Author Closing Comment

by:mmcompact
ID: 34901950
thanks, works great and very nice article
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question