Help with transposing 2000 rows of data into one column

Tabanag

New Member
Joined
May 18, 2016
Messages
13
I would like to transpose over 2000 rows of data into one column. Each row contains 300 columns of data, as an example:

Column1 Column2 Column3 Column4 .... Column300
T1000 T10006 T10200 T600
WU100 WE300
BT300 BT600 BT800

Results should show:
T1000
T10006
T10200
T600
WU100
WE300
BT300
BT600
BT800

Maybe a macro to transpose the first row, and then a repeat command?
Would appreciate any help please!!!
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
assumes a header row.
Code:
Sub t()
Dim lr As Long, i As Long
lr = ActiveSheet.Cells.Find("*", , xlFormulas, xlPart, xlByRows, xlPrevious).Row
ActiveSheet.Columns(1).Insert
    For i = 2 To lr
        With ActiveSheet
            .Range(.Cells(i, 2), .Cells(i, Columns.Count).End(xlToLeft)).Copy
            .Cells(Rows.Count, 1).End(xlUp)(2).PasteSpecial Transpose:=True
            .Range(.Cells(i, 2), .Cells(i, Columns.Count).End(xlToLeft)).Delete
        End With
    Next
    Application.CutCopyMode = False
End Sub

Be sure you make a back up copy of your file before running this code.
 
Last edited:
Upvote 0
Welcome to the Forum!

This macro assumes your data are in contiguous columns and start in A1. The macro adds a new col A and lists all data (including blank cells) vertically in the new col A.
Code:
Sub RowsToColumn()
'Assumes data start in A1
Dim R As Range, nR As Long, i As Long, ct As Long
Application.ScreenUpdating = False
Columns("A").Insert
Set R = Range("B1").CurrentRegion
nR = 1
For i = 1 To R.Rows.Count
    ct = R.Rows(i).Cells.Count
    Range("A" & nR, "A" & nR + ct - 1).Value = Application.Transpose(R.Rows(i))
    nR = Range("A" & Rows.Count).End(xlUp).Row + 1
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
JoeMo,

Nice approach - one for my archives.


Tabanag,

Welcome to the MrExcel forum.

I assumed that your displayed raw data was in one worksheet, and, that the results would be in another.

Here is another macro solution for you to consider, based on my above assumption, and, it will adjust the varying number of raw data rows, and, columns.

I assume that both worksheets already exist. You can change the worksheet names in the macro.

Sample raw data in worksheet Sheet1:


Excel 2007
ABCDE
1T1000T10006T10200T600
2WU100WE300
3BT300BT600BT800
4
Sheet1


And, after the macro in worksheet Sheet2:


Excel 2007
A
1T1000
2T10006
3T10200
4T600
5WU100
6WE300
7BT300
8BT600
9BT800
10
Sheet2


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub Tabanag()
' hiker95, 05/19/2016, ME942301
Dim w1 As Worksheet, w2 As Worksheet
Dim r As Long, lr As Long, c As Long, lc As Long, nr As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   '<-- you can change the sheet name here
Set w2 = Sheets("Sheet2")   '<-- you can change the sheet name here
w2.Columns(1).ClearContents
With w1
  lr = .Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
  lc = .Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
  For r = 1 To lr
    If r = 1 Then
      nr = 1
    Else
      nr = w2.Cells(w2.Rows.Count, "A").End(xlUp).Row + 1
    End If
    w2.Cells(nr, 1).Resize(lc).Value = Application.Transpose(w1.Range(.Cells(r, 1), .Cells(r, lc)).Value)
  Next r
End With
With w2
  .Columns(1).AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the Tabanag macro.
 
Last edited:
Upvote 0
JoeMo,

Nice approach - one for my archives.


Tabanag,

Welcome to the MrExcel forum.

I assumed that your displayed raw data was in one worksheet, and, that the results would be in another.

Here is another macro solution for you to consider, based on my above assumption, and, it will adjust the varying number of raw data rows, and, columns.

I assume that both worksheets already exist. You can change the worksheet names in the macro.


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub Tabanag()
' hiker95, 05/19/2016, ME942301
Dim w1 As Worksheet, w2 As Worksheet
Dim r As Long, lr As Long, c As Long, lc As Long, nr As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   '<-- you can change the sheet name here
Set w2 = Sheets("Sheet2")   '<-- you can change the sheet name here
w2.Columns(1).ClearContents
With w1
  lr = .Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
  lc = .Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
  For r = 1 To lr
    If r = 1 Then
      nr = 1
    Else
      nr = w2.Cells(w2.Rows.Count, "A").End(xlUp).Row + 1
    End If
    w2.Cells(nr, 1).Resize(lc).Value = Application.Transpose(w1.Range(.Cells(r, 1), .Cells(r, lc)).Value)
  Next r
End With
With w2
  .Columns(1).AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the Tabanag macro.


Dear hiker95,

This is Perfect.. Exactly what I needed! You have got to be a genius. Your detailed explanations and instructions were very helpful especially for a newbie like me. I am in AWE. Thank you so much for taking the time to respond so quickly and write this all down. You saved me tons of work!

Although I come here a lot, this was my first post. So happy I decided to join this forum!!!

Thank you so much again!
Tabanag
 
Upvote 0
assumes a header row.
Code:
Sub t()
Dim lr As Long, i As Long
lr = ActiveSheet.Cells.Find("*", , xlFormulas, xlPart, xlByRows, xlPrevious).Row
ActiveSheet.Columns(1).Insert
    For i = 2 To lr
        With ActiveSheet
            .Range(.Cells(i, 2), .Cells(i, Columns.Count).End(xlToLeft)).Copy
            .Cells(Rows.Count, 1).End(xlUp)(2).PasteSpecial Transpose:=True
            .Range(.Cells(i, 2), .Cells(i, Columns.Count).End(xlToLeft)).Delete
        End With
    Next
    Application.CutCopyMode = False
End Sub

Be sure you make a back up copy of your file before running this code.


Hi JLGWhiz,

You are all geniuses! I wasn't very clear about my data not having a header row, but aside from that, the macro worked ! It listed the items in one column just as I wanted. Thank you so much for your help!

Wow! So impressed with this forum!

Mahalo!
Tabanag
 
Upvote 0
Welcome to the Forum!

This macro assumes your data are in contiguous columns and start in A1. The macro adds a new col A and lists all data (including blank cells) vertically in the new col A.
Code:
Sub RowsToColumn()
'Assumes data start in A1
Dim R As Range, nR As Long, i As Long, ct As Long
Application.ScreenUpdating = False
Columns("A").Insert
Set R = Range("B1").CurrentRegion
nR = 1
For i = 1 To R.Rows.Count
    ct = R.Rows(i).Cells.Count
    Range("A" & nR, "A" & nR + ct - 1).Value = Application.Transpose(R.Rows(i))
    nR = Range("A" & Rows.Count).End(xlUp).Row + 1
Next i
Application.ScreenUpdating = True
End Sub


Hi JoeMo!

This macro solution worked perfectly! I got other macro solutions and you are all just awesome! Thank you so much for responding so quickly and graciously welcoming me into your group. So happy there is such a forum I can go to.

I wish I could also be an excel guru!

Thank you again!
Tabanag
 
Upvote 0
Tabanag,

Thanks for the feedback.

You are very welcome. Glad we could help.

And, come back anytime.
 
Upvote 0
On a reduced sample (9 columns by 2000 rows), the following code measures as 11 times faster than JoeMo's code and 6 times faster than hiker95's code...
Code:
Sub RowsToSingleLongColumn()
  Dim r As Long, c As Long, x As Long, Data As Variant, Result As Variant
  Data = Range("A1").CurrentRegion
  ReDim Result(1 To UBound(Data, 1) * UBound(Data, 2), 1 To 1)
  Columns("A").Insert
  For r = 1 To UBound(Data, 1)
    For c = 1 To UBound(Data, 2)
      If Len(Data(r, c)) Then
        x = x + 1
        Result(x, 1) = Data(r, c)
      Else
        Exit For
      End If
    Next
  Next
  Range("A1").Resize(UBound(Result)) = Result
End Sub
To put this in perspective, though, all codes executed in well under 1 second (although I do not know what affect on timing, other than they will go up, will occur when the columns are expanded out to 300)...

JoeMo: 0.66 seconds

hiker95: 0.36 seconds

mine: 0.06 seconds
 
Last edited:
Upvote 0
How do you time the code Rick? Do you simply create a time stamp via VBA at the start and end of the code, or do you have some other way?

Cheers

pvr928
 
Upvote 0

Forum statistics

Threads
1,215,268
Messages
6,123,966
Members
449,137
Latest member
yeti1016

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top