Posted to microsoft.public.excel.misc
|
|
Making one long colum out of many
Great post! Thanks for sharing this macro - many's the time I wanted to be
able to do this. I've created a small workbook called "Useful macros.xls" to
document this and other macros I find or create.
--
music on the web: www.esnips.com/web/yahyas-music
"Gord Dibben" wrote:
Sub OneColumnV2()
''''''''''''''''''''''''''''''''''''''''''
'Macro to copy columns of variable length'
'into 1 continous column in a new sheet '
'Modified 17 FEb 2006 by Bernie Dietrick
''''''''''''''''''''''''''''''''''''''''''
Dim iLastcol As Long
Dim iLastRow As Long
Dim jLastrow As Long
Dim ColNdx As Long
Dim ws As Worksheet
Dim myRng As Range
Dim ExcludeBlanks As Boolean
Dim myCell As Range
ExcludeBlanks = (MsgBox("Exclude Blanks", vbYesNo) = vbYes)
Set ws = ActiveSheet
iLastcol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Alldata").Delete
Application.DisplayAlerts = True
Sheets.Add.Name = "Alldata"
For ColNdx = 1 To iLastcol
iLastRow = ws.Cells(ws.Rows.Count, ColNdx).End(xlUp).Row
Set myRng = ws.Range(ws.Cells(1, ColNdx), _
ws.Cells(iLastRow, ColNdx))
If ExcludeBlanks Then
For Each myCell In myRng
If myCell.Value < "" Then
jLastrow = Sheets("Alldata").Cells(Rows.Count, 1) _
.End(xlUp).Row
myCell.Copy
Sheets("Alldata").Cells(jLastrow + 1, 1) _
.PasteSpecial xlPasteValues
End If
Next myCell
Else
myRng.Copy
jLastrow = Sheets("Alldata").Cells(Rows.Count, 1) _
.End(xlUp).Row
myCell.Copy
Sheets("Alldata").Cells(jLastrow + 1, 1) _
.PasteSpecial xlPasteValues
End If
Next
Sheets("Alldata").Rows("1:1").entirerow.Delete
ws.Activate
End Sub
Gord Dibben MS Excel MVP
On Sun, 03 Aug 2008 16:14:29 GMT, Lars-Åke Aspelin
wrote:
"I have 20 colums, each one is about 30 cells long,...."
To me that does not imply that all columns have equal length, just
that 30 is a typical length of the columns.
So column1 could hold 28 cells of data, column 2 could hold 33, and so
on.
If that is the case we will have to find another formula that takes
care of the different column lengths so that there will not be any
"gaps" in the new, long, column.
Lars-Åke
|