Solved

Delete empty cells and merge columns

Posted on 2011-02-15
4
611 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
Comment Utility
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
Comment Utility
have you tried it on my attached file?
0
 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
Comment Utility

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
Comment Utility
thanks, works great and very nice article
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

763 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

9 Experts available now in Live!

Get 1:1 Help Now