Transpose Macro (Vertical to Horizontal) for a varying Number of rows to varying number of columns.

biostatistician

New Member
Joined
Oct 24, 2014
Messages
11
Hi all,
New this forum. I'm not a VB expert but did some work with VB years ago back in graduate school so I'm not too familiar anymore.

I'm basically trying to take Column G, and transpose it accordingly. For example, STUDY #2 has 3 extra rows in Column G which I want to transpose to ROW K and onward.
I want to do the same for Study 3 and 4 and so on for almost 3000 STUDY #'s. But the number of rows vary. Is there a code to do this? I know it will be conditional and I need the code to realize it's finished transposing for that STUDY # once the next STUDY appears. I've posted an example of what I'm looking for below. Any help with this would be greatly appreciated!

WHAT I HAVE:

A B C D E F G H I J K L
STUDYPresentGenNumberraceshortdiagAdDiRe
1 YF30O8120081201MM**
2 YM20J8341083412OO**
30501
E8888
E8498
3 YM40B8023080235SSY
E9688
E8495
4 YM50B8151081514SS**
8832
E9174
E8498
71894
9556
5NF25M8238282302SOY
8082
8056
2851
81201
920
85011
E8147
E8495
4019
2724
WHAT I NEED:
A BCDEFGHIJKLMNOPQRST
STUDYPresentGenNumberraceshortdiagAdDiRe
1 YF30O8120081201MM**
2 YM20J8341083412OO**30501E8888E8498
3 YM40B8023080235SSYE9688E8495
4 YM50B8151081514SS**8832E9174E8498718949556
5NF25M8238282302SOY8082805628518120192085011E8147E849540192724

<tbody>
</tbody>
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try this:-
NB:- This code will delete the Blank rows !!!
Code:
[COLOR="Navy"]Sub[/COLOR] MG24Oct57
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Rng2 [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Lst [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Lst = Range("G" & Rows.Count).End(xlUp).Row
[COLOR="Navy"]Set[/COLOR] Rng2 = Range("F2").Resize(Lst).SpecialCells(xlCellTypeBlanks)
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng2.Areas
    Dn.Offset(, 1).Copy
    Dn(1).Offset(-1, 5).PasteSpecial Transpose:=True
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]If[/COLOR] Not Rng2 [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] Rng2.EntireRow.Delete
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:-
NB:- This code will delete the Blank rows !!!
Code:
[COLOR=Navy]Sub[/COLOR] MG24Oct57
[COLOR=Navy]Dim[/COLOR] Rng [COLOR=Navy]As[/COLOR] Range, Dn [COLOR=Navy]As[/COLOR] Range
[COLOR=Navy]Dim[/COLOR] Rng2 [COLOR=Navy]As[/COLOR] Range
[COLOR=Navy]Dim[/COLOR] Lst [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
Lst = Range("G" & Rows.Count).End(xlUp).Row
[COLOR=Navy]Set[/COLOR] Rng2 = Range("F2").Resize(Lst).SpecialCells(xlCellTypeBlanks)
[COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] Dn [COLOR=Navy]In[/COLOR] Rng2.Areas
    Dn.Offset(, 1).Copy
    Dn(1).Offset(-1, 5).PasteSpecial Transpose:=True
[COLOR=Navy]Next[/COLOR] Dn
[COLOR=Navy]If[/COLOR] Not Rng2 [COLOR=Navy]Is[/COLOR] Nothing [COLOR=Navy]Then[/COLOR] Rng2.EntireRow.Delete
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick


Hi Mick,

Thanks for the code. unfortunately the code doesn't run and the debug highlights this line:

Sub MG24Oct57()
Dim Rng As Range, Dn As Range
Dim Rng2 As Range
Dim Lst As Long
Lst = Range("G" & Rows.Count).End(xlUp).Row
Set Rng2 = Range("F2").Resize(Lst).SpecialCells(xlCellTypeBlanks)
For Each Dn In Rng2.Areas
Dn.Offset(, 1).Copy
Dn(1).Offset(-1, 5).PasteSpecial Transpose:=True
Next Dn
If Not Rng2 Is Nothing Then Rng2.EntireRow.Delete
End Sub

It says no cells were found.

Thanks again Mick!
 
Upvote 0
biostatistician,

This is a duplicate post?

I have change the format of the titles in row 1 to fit the results correctly in the MrExcel display area.


Sample raw data:


Excel 2007
ABCDEFGHIJK
1STU DYPres entGenNum berraceshortdiagAdDiRe
21YF30O8120081201MM**
32YM20J8341083412OO**
430501
5E8888
6E8498
73YM40B8023080235SSY
8E9688
9E8495
104YM50B8151081514SS**
118832
12E9174
13E8498
1471894
159556
165NF25M8238282302SOY
178082
188056
192851
2081201
21920
2285011
23E8147
24E8495
254019
262724
27
Sheet1


After the macro:


Excel 2007
ABCDEFGHIJKLMNOPQRST
1STU DYPres entGenNum berraceshortdiagAdDiRe
21YF30O8120081201MM**
32YM20J8341083412OO**30501E8888E8498
43YM40B8023080235SSYE9688E8495
54YM50B8151081514SS**8832E9174E8498718949556
65NF25M8238282302SOY8082805628518120192085011E8147E849540192724
7
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).

Code:
Sub ReorgData()
' hiker95, 10/24/2014, ME913833
Dim r As Long, lr As Long, n As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "G").End(xlUp).Row
On Error Resume Next
With Range("F2:F" & lr)
  .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C"
  .Value = .Value
End With
On Error GoTo 0
For r = 2 To lr
  n = Application.CountIf(Columns(6), Cells(r, 6).Value)
  If n > 1 Then
    Cells(r, 11).Resize(, n - 1).Value = Application.Transpose(Range("G" & r + 1 & ":G" & r + 1 + n - 1).Value)
    Cells(r + 1, 6).Resize(n - 1, 2).ClearContents
  End If
  r = r + n - 1
Next r
Columns.AutoFit
On Error Resume Next
Range("A2:A" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
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

Then run the ReorgData macro.
 
Upvote 0


Thanks Mike for the file! Ah, okay it works for the file you sent me. Don't hate me but I think I figured out what was wrong. I had identifying information on that excel so I forgot I deleted some columns for the post. Obviously that would create an error. There are 5 columns between E and G. Column G in this case is M, and Column F is L. Again it would be column M I'm interested in transposing. I tried working with the code to change G-->M and F-->L, and I set the offset still at 5. But that didn't work. What should I tweak in the code? THanks again for your help Mike and Hiker95!




ABCDEFGHIJKLMNOP
1STU
DY
Pres
ent
GenNum
ber
racesourceprioritydateddatelosshortdiagAdDiRe
21YF30O1223432452342138120081201MM**
32YM20J315323252353258341083412OO**
430501
5E8888
6E8498
73YM40B235235322131238023080235SSY
8E9688
9E8495
104YM50B5312213352318151081514SS**
118832
12E9174
13E8498
1471894
159556
165NF25M234645563453422348238282302SOY
178082
188056
192851
2081201
21920
2285011
23E8147
24E8495
254019
262724
27

<thead>
</thead><tbody>
</tbody>
 
Upvote 0
biostatistician,

If the following is a screenshot of the new raw data:


Excel 2007
ABCDEFGHIJKLMNOPQ
11STU DYPres entGenNum berracesou rcepri oritydated datelosshortdiagAdDiRe
221YF30O1223432452342138120081201MM**
332YM20J315323252353258341083412OO**
4430501
55E8888
66E8498
773YM40B235235322131238023080235SSY
88E9688
99E8495
10104YM50B5312213352318151081514SS**
11118832
1212E9174
1313E8498
141471894
15159556
16165NF25M234645563453422348238282302SOY
17178082
18188056
19192851
202081201
2121920
222285011
2323E8147
2424E8495
25254019
26262724
27
Sheet1


Then after the new macro (in two screenshots to fit the display area):


Excel 2007
ABCDEFGHIJKLM
11STU DYPres entGenNum berracesou rcepri oritydated datelosshortdiag
221YF30O1223432452342138120081201
332YM20J315323252353258341083412
473YM40B235235322131238023080235
5104YM50B5312213352318151081514
6165NF25M234645563453422348238282302
7
Sheet1



Excel 2007
LMNOPQRSTUVWXYZAA
1shortdiagAdDiRe
28120081201MM**
38341083412OO**30501E8888E8498
48023080235SSYE9688E8495
58151081514SS**8832E9174E8498718949556
68238282302SOY8082805628518120192085011E8147E849540192724
7
Sheet1




Code:
Sub ReorgData_V2()
' hiker95, 10/25/2014, ME913833
Dim r As Long, lr As Long, n As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "M").End(xlUp).Row
On Error Resume Next
With Range("L2:L" & lr)
  .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C"
  .Value = .Value
End With
On Error GoTo 0
For r = 2 To lr
  n = Application.CountIf(Columns(12), Cells(r, 12).Value)
  If n > 1 Then
    Cells(r, 17).Resize(, n - 1).Value = Application.Transpose(Range("M" & r + 1 & ":M" & r + 1 + n - 1).Value)
    Cells(r + 1, 12).Resize(n - 1, 2).ClearContents
  End If
  r = r + n - 1
Next r
Columns.AutoFit
On Error Resume Next
Range("B2:B" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
Application.ScreenUpdating = True
End Sub



Then un
 
Upvote 0
biostatistician,

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).

Code:
Sub ReorgData_V2()
' hiker95, 10/25/2014, ME913833
Dim r As Long, lr As Long, n As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "M").End(xlUp).Row
On Error Resume Next
With Range("L2:L" & lr)
  .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C"
  .Value = .Value
End With
On Error GoTo 0
For r = 2 To lr
  n = Application.CountIf(Columns(12), Cells(r, 12).Value)
  If n > 1 Then
    Cells(r, 17).Resize(, n - 1).Value = Application.Transpose(Range("M" & r + 1 & ":M" & r + 1 + n - 1).Value)
    Cells(r + 1, 12).Resize(n - 1, 2).ClearContents
  End If
  r = r + n - 1
Next r
Columns.AutoFit
On Error Resume Next
Range("B2:B" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
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

Then run the ReorgData_V2 macro.
 
Upvote 0
biostatistician,

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).

Code:
Sub ReorgData_V2()
' hiker95, 10/25/2014, ME913833
Dim r As Long, lr As Long, n As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "M").End(xlUp).Row
On Error Resume Next
With Range("L2:L" & lr)
  .SpecialCells(xlCellTypeBlanks).Formula = "=R[-1]C"
  .Value = .Value
End With
On Error GoTo 0
For r = 2 To lr
  n = Application.CountIf(Columns(12), Cells(r, 12).Value)
  If n > 1 Then
    Cells(r, 17).Resize(, n - 1).Value = Application.Transpose(Range("M" & r + 1 & ":M" & r + 1 + n - 1).Value)
    Cells(r + 1, 12).Resize(n - 1, 2).ClearContents
  End If
  r = r + n - 1
Next r
Columns.AutoFit
On Error Resume Next
Range("B2:B" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
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

Then run the ReorgData_V2 macro.





Hiker95,
So i plugged in the code, and didn't get a debug error. But....Excel crashes every time I hit run! No idea why. I waited about 15 minutes hoping it was still running, but nothing. Any idea why this may be the case? I tried it on a very small subset of data as well and the same thing happens with only 50 rows. I tried it with about 10,000 rows too and same thing.
 
Upvote 0
Here is another macro that you can try...
Code:
Sub TransposeDiagData()
  Dim Blanks As Range, Ar As Range
  On Error GoTo NoBlanks
  Set Blanks = Range("B1:B" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlBlanks)
  For Each Ar In Intersect(Blanks.EntireRow, Columns("M")).Areas
    Intersect(Ar(1).Offset(-1).EntireRow, Columns("Q")).Resize(, Ar.Count) = Application.Transpose(Ar)
  Next
  Blanks.EntireRow.Delete
NoBlanks:
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,467
Messages
6,124,984
Members
449,201
Latest member
Lunzwe73

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