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
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Just an idea as a routine, even tho i would prefer a function but didn't thought about how to make it in a simple as this

Sub TransposeSys()
Dim i, j As Long
Dim Rng1, Rng2 As Range

Set Rng1 = Sheet1.Range("A1:C3")
Set Rng2 = Sheet1.Range("K9")
For i = 1 To Rng1.Rows.Count
For j = 1 To Rng1.Columns.Count
Rng2.Offset(j - 1, i - 1) = Rng1.Cells(i, j)
Next j
Next i
End Sub

Rng1 is the range to be transposed and rng2 is the top left range of the destiny range
 
Upvote 0
Just an idea as a routine, even tho i would prefer a function but didn't thought about how to make it in a simple as this

Sub TransposeSys()
Dim i, j As Long
Dim Rng1, Rng2 As Range

Set Rng1 = Sheet1.Range("A1:C3")
Set Rng2 = Sheet1.Range("K9")
For i = 1 To Rng1.Rows.Count
For j = 1 To Rng1.Columns.Count
Rng2.Offset(j - 1, i - 1) = Rng1.Cells(i, j)
Next j
Next i
End Sub

Rng1 is the range to be transposed and rng2 is the top left range of the destiny range


Hi XonArgi,

I ran the code but the output is not contained in just one column, which is what I need. Each new set of transposed cells appears 3 columns to the right of the previous.

Instead of
abca
b
c
121
2
aabbccddaa
bb
cc
dd

<tbody>
</tbody>

I get
abca1aa
b2bb
ccc
12dd
aabbccdd

<tbody>
</tbody>
 
Upvote 0
drew1230,

Welcome to the MrExcel forum.

Here is a macro solution for you to consider.

Sample raw data in the active worksheet:


Excel 2007
OPQRSTUVWX
1abcdefghi
2123456
3aabbccdd
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Sheet1


And, after the macro:


Excel 2007
OPQRSTUVWX
1abcdefghi
2b
3c
4d
5e
6f
7g
8h
9i
10123456
112
123
134
145
156
16aabbccdd
17bb
18cc
19dd
20
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 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

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 TransposeRowsToColumn macro.
 
Upvote 0
Is this that you're looking for then?

Sub TransposeSys()
Dim i As Long
Dim Rng1 As Range

Set Rng1 = Sheet1.Range("G5:L5")

With Rng1
For i = 1 To .Columns.Count
Rng1.Cells(1, 1).Offset(i - 1, 0) = Rng1.Cells(1, i)
Next i
End With
End Sub

Rng1 is the row in wich you will get the values to use for first column
 
Upvote 0
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)

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

So, just to be clear:
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)


Try this:
Code:
Sub a935668a()
Dim i As Long, j As Long
Application.ScreenUpdating = False
For i = Range("O" & Rows.count).End(xlUp).row To 1 Step -1
  If Range("O" & i) <> "" Then
  j = Rows(i).Find("*", SearchDirection:=xlPrevious).Column - 14
  Cells(i, "O").Resize(j, 1).Value = Application.Transpose(Cells(i, "O").Resize(1, j).Value)
  End If
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
So, just to be clear:
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)


Try this:
Code:
Sub a935668a()
Dim i As Long, j As Long
Application.ScreenUpdating = False
For i = Range("O" & Rows.count).End(xlUp).row To 1 Step -1
  If Range("O" & i) <> "" Then
  j = Rows(i).Find("*", SearchDirection:=xlPrevious).Column - 14
  Cells(i, "O").Resize(j, 1).Value = Application.Transpose(Cells(i, "O").Resize(1, j).Value)
  End If
Next
Application.ScreenUpdating = True
End Sub


Yes! This worked like a charm. Thanks Akuini.

And thanks to everyone else for your time and efforts.
 
Upvote 0
Hi. I recently came upon this thread and was hoping to apply this same Macro but haven't had much success. i'm trying to transpose the rows into columns. i have already inserted the number of columns needed for each row. there are over 17,000 rows and each category has a different value for rates.

categoryrate descriptionRateEachRentEOW2x3xmonthlyDailyLossDSNOGLease
Black ApronEach010020011004
rent
EOW
2X
3X
Monthly
Daily
Loss
DS
NOG
Lease
Blue ApronEach.70.8001.912500
Rent
EOW
2X
3X
Monthly
Daily
Loss
DS
NOG
Lease

<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

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