Solved

Delete empty cells and merge columns

Posted on 2011-02-15
4
657 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

691 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