Macro to Transpose Rows to Columns

drew1230

New Member
Joined
Apr 18, 2016
Messages
7
Hey all,

I un-versed in VBA or any type of coding, for that matter, but it looks like formulas won't for what I'm attempting to do. I'm trying to transpose a variable number of cells in a row into columns right beneath the first column. I've already pre-inserted the required number of rows beneath each of the rows I'm trying to transpose.
I'm actually working with columns O to W (8 columns total)

I'm trying to get from...

abc
12
aabbccdd

<tbody>
</tbody>














to...

abc
b
c
12
2
aabbccdd
bb
cc
dd

<tbody>
</tbody>















Gotta do this for about 5,000 rows so any help would be appreciated! I can't imagine how many days this would take to finish manually.

-Drew
 
apalomino,

Welcome to the MrExcel forum.

Here is a macro solution for you to consider that is based on the structure of your displayed flat text raw data in the active worksheet, and, will adjust to the number of raw data rows, and, columns.

Sample raw data in the active worksheet:


Excel 2007
ABCDEFGHIJKLMNO
1categoryrate descriptionRateEachRentEOW2x3xmonthlyDailyLossDSNOGLease
2Black ApronEach010020011004
3rent
4EOW
52X
63X
7Monthly
8Daily
9Loss
10DS
11NOG
12Lease
13Blue ApronEach0.700010.912500
14Rent
15EOW
162X
173X
18Monthly
19Daily
20Loss
21DS
22NOG
23Lease
24
Sheet1


And, after the macro:


Excel 2007
ABCDEFGHIJKLMNO
1categoryrate descriptionRateEachRentEOW2x3xmonthlyDailyLossDSNOGLease
2Black ApronEach0010020011004
3rent1
4EOW0
52X0
63X2
7Monthly0
8Daily0
9Loss11
10DS0
11NOG0
12Lease4
13Blue ApronEach0.70.700010.912500
14Rent0
15EOW
162X0
173X0
18Monthly1
19Daily0.9
20Loss12
21DS5
22NOG0
23Lease0
24
Sheet1


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 ReorgData()
' hiker95, 06/29/2016, ME935668
Dim Area As Range, sr As Long, lc As Long
Application.ScreenUpdating = False
With ActiveSheet
  lc = .Cells(1, .Columns.Count).End(xlToLeft).Column
  For Each Area In .Range("D2", .Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
    With Area
      sr = .Row
      Range("C" & sr).Resize(lc - 3).Value = Application.Transpose(Range(Cells(sr, 4), Cells(sr, lc)).Value)
    End With
  Next Area
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 ReorgData macro.
 
Upvote 0

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
This unfortunately did not work. It pasted the column headers from E to N into C2 to C11.

apalomino,

Are my screenshots in my reply #11 not correct?

If my screenshots are not correct, then can we see your actual screenshots of the raw data (not flat text displays), and, what the results should look like?


See reply #2 at the next link, if you want to show small screenshots, of the raw data, and, what the results should look like.

http://www.mrexcel.com/forum/about-board/508133-attachments.html#post2507729


Or, you can post your workbook/worksheets to the following free site (sensitive data changed), mark the workbook for sharing, and, provide us with a link to your workbook:

https://dropbox.com

If you are not able to provide screenshots, then:

You can post your workbook/worksheets to the following free site (sensitive data changed), mark the workbook for sharing, and, provide us with a link to your workbook:

https://dropbox.com
 
Upvote 0
drew1230,

Have you even tried my macro in my reply #4?

Hiker95, sorry I didn't see that you had posted back then, but I'm grateful that you did. I revisited this thread because I had another use case where your code was exactly what I needed. It worked wonderfully! Thank you!

I just had to adjust a "Cells(r + 1, 15).Resize(n)..." to "Cells(r + 1, 16).Resize(n)..." in order to get the transposed values in the right column.
 
Upvote 0
Hiker95, sorry I didn't see that you had posted back then, but I'm grateful that you did. I revisited this thread because I had another use case where your code was exactly what I needed. It worked wonderfully! Thank you!

I just had to adjust a "Cells(r + 1, 15).Resize(n)..." to "Cells(r + 1, 16).Resize(n)..." in order to get the transposed values in the right column.

drew1230,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0
Code:
Sub ReorgData()
' hiker95, 06/29/2016, ME935668
Dim Area As Range, sr As Long, lc As Long
Application.ScreenUpdating = False
With ActiveSheet
  lc = .Cells(1, .Columns.Count).End(xlToLeft).Column
  For Each Area In .Range("D2", .Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
    With Area
      sr = .Row
      Range("C" & sr).Resize(lc - 3).Value = Application.Transpose(Range(Cells(sr, 4), Cells(sr, lc)).Value)
    End With
  Next Area
End With
Application.ScreenUpdating = True
End Sub
Assuming the number of horizontal labels (Row 1) equals the number of vertical labels (Column B) for each Category listed in Column A, then here is another way to write your macro...
Code:
[table="width: 500"]
[tr]
	[td]Sub ReorgData_V3()
  Dim Ar As Range
  Application.ScreenUpdating = False
  For Each Ar In Range("A2:A" & Cells(Rows.Count, "B").End(xlUp).Row).SpecialCells(xlBlanks).Areas
    Ar(1).Offset(-1, 2).Resize(Ar.Count + 1) = Application.Transpose(Ar(1).Offset(-1, 3).Resize(, Ar.Count + 1))
  Next
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Hi!


It’s been some time since last time and my spreadsheet’s changed a bit. I attempted to figure this out on my own for a few hours by tinkering with the previous code, but to no avail. It’d be great if can get some help.


How can I go from:


ABCDEFGHIJ
1xabcdefghi
2y123456
3zaabbccdd

<tbody>
</tbody>

To:

ABCDEFGHIJ
1xabcdefghi
2b
3c
4d
5e
6f
7g
8h
9i
10y123456
112
123
134
145
156
16zaabbccdd
17bb
18cc
19dd

<tbody>
</tbody>

?

In this case, the cells being transposed has not been pre-inserted into rows, and the number of cells from B on is indefinite.

Thanks your able to help!
 
Upvote 0
drew1230,

Here is a new macro solution for you to consider that is based on your latest flat text displays, that uses two arrays in memory.

With your raw data in the active worksheet in range A1:J3, the results will overwrite the raw data, and, will be written in range A1:J19.

Try the following:

Code:
Sub ReorganizeData()
' hiker95, 12/11/2017, ME935668
Application.ScreenUpdating = False
Dim a As Variant, r As Long, c As Long, cc As Long, lr As Long, lc As Long, n As Long
Dim o As Variant, j As Long
With ActiveSheet
  lr = .Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
  lc = .Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
  a = .Range(.Cells(1, 1), .Cells(lr, lc))
  n = Application.CountA(.Range(.Cells(1, 1), .Cells(lr, lc))) - lr
  ReDim o(1 To n, 1 To lc)
  For r = 1 To lr
    j = j + 1
    For c = 1 To lc
      o(j, c) = a(r, c)
    Next c
    For cc = 3 To lc
      If Not a(r, cc) = vbEmpty Then
        j = j + 1: o(j, 2) = a(r, cc)
      End If
    Next cc
  Next r
  .Range(.Cells(1, 1), .Cells(lr, lc)).ClearContents
  .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns.AutoFit
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
<style type="text/css"> p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 13.0px Verdana; color: #222222; -webkit-text-stroke: #222222; background-color: #ffffff} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 13.0px Verdana; color: #222222; -webkit-text-stroke: #222222; min-height: 16.0px} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 13.0px Verdana; color: #222222; -webkit-text-stroke: #222222; background-color: #ffffff; min-height: 16.0px} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 12.0px; font: 12.0px Courier; color: #333333; -webkit-text-stroke: #222222; background-color: #f2f6f8} span.s1 {font-kerning: none} </style>Hi Hiker95,


Thanks for taking the time. My spreadsheet actually has around 5000 rows and the columns to be transposed starts at column C and on indefinitely. Sorry I wasn't clear!


The macro that you gave me a little over a year ago looks like should be able to accomplish this with a few tweaks, but I haven’t been able to figure it out. Here it is below:


Sub TransposeRowsToColumn()
' hiker95, 04/18/2016, ME935668
Dim r As Long, lr As Long, lc As Long, n As Long
Application.ScreenUpdating = False
With ActiveSheet
lr = .Cells(Rows.Count, "O").End(xlUp).Row
For r = lr To 1 Step -1
lc = .Cells(r, Columns.Count).End(xlToLeft).Column
n = lc - 15
.Rows(r + 1).Resize(n).Insert
.Cells(r + 1, 15).Resize(n).Value = Application.Transpose(.Range(.Cells(r, 16), .Cells(r, lc)).Value)
Next r
End With
Application.ScreenUpdating = True
End Sub


<style type="text/css"> p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 13.0px Verdana; color: #222222; -webkit-text-stroke: #222222} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 13.0px Verdana; color: #222222; -webkit-text-stroke: #222222; background-color: #ffffff} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 13.0px Verdana; color: #222222; -webkit-text-stroke: #222222; background-color: #ffffff; min-height: 16.0px} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 12.0px; font: 12.0px Courier; color: #333333; -webkit-text-stroke: #222222; background-color: #f2f6f8} p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 12.0px; font: 12.0px Courier; color: #333333; -webkit-text-stroke: #222222; background-color: #f2f6f8; min-height: 14.0px} span.s1 {font-kerning: none; background-color: #ffffff} span.s2 {font-kerning: none} </style>
Thanks again!
 
Upvote 0
Hi Hiker95,

Thanks for taking the time. My spreadsheet actually has around 5000 rows and the columns to be transposed starts at column C and on indefinitely. Sorry I wasn't clear!

drew1230,

Can we see what your actual raw data worksheet looks like?

And, can we see what the results (manually formatted by you) should look like?

You can post your workbook/worksheets to the following free site (sensitive data changed), mark the workbook for sharing, and, provide us with a link to your workbook:

https://dropbox.com
 
Upvote 0

Forum statistics

Threads
1,215,487
Messages
6,125,075
Members
449,205
Latest member
Healthydogs

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