Solved

Delete empty cells and merge columns

Posted on 2011-02-15
4
626 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now