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>
 
biostatistician,

If my latest screenshot of the raw data worksheet was correct then the macro should have worked.

I would be happy to try my macro on your actual raw data.


You can upload your workbook to Box Net,

sensitive data changed

mark the workbook for sharing

and provide us with a link to your workbook.
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
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


Hi Rick,

I tried your code, and sadly it doesn't seem to work. I don't receive any errors, but it doesn't seem to be changing anything. Do you have any idea how I can tweak your code to make it work? Thanks!
 
Upvote 0
I tried your code, and sadly it doesn't seem to work. I don't receive any errors, but it doesn't seem to be changing anything. Do you have any idea how I can tweak your code to make it work?
The only way my code would not work is if the "blanks" in Column B were not truly blank. This can happen in two ways (that I can think of off the top of my head)... you have formulas in the cells of Column B (where the blanks are really "" outputted by the formula) or you have an "invisible" character in those "blank" cells (such as space character or, if you copied the data from another source, possible a non-breaking space whose ASCII code is 160). So, first question... do you have formulas in the cells of Column B? If not, what about the contents of the "blank" cells... put his formula in a cell and see if it displays 0 (meaning blanks are really blank) or some number greater than zero (meaning your blanks are not really blank).

=COUNTIF(B:B," ")+COUNTIF(B:B,CHAR(160))
 
Upvote 0
The only way my code would not work is if the "blanks" in Column B were not truly blank. This can happen in two ways (that I can think of off the top of my head)... you have formulas in the cells of Column B (where the blanks are really "" outputted by the formula) or you have an "invisible" character in those "blank" cells (such as space character or, if you copied the data from another source, possible a non-breaking space whose ASCII code is 160). So, first question... do you have formulas in the cells of Column B? If not, what about the contents of the "blank" cells... put his formula in a cell and see if it displays 0 (meaning blanks are really blank) or some number greater than zero (meaning your blanks are not really blank).

=COUNTIF(B:B," ")+COUNTIF(B:B,CHAR(160))

Hi Rick,
Thanks again for your help. I got your code to work after some manipulating. It might have been do with the formatting of the file, So I exported to CSV to strip it of all the other formatting and then back XLSM. However, I noticed that the example posted before had one small error in it which is Column A was all numbers that continued through the data set..that was originally just the cell numbers so column A with numbers shouldn't be there but in place it should be the corresponding Names, so Column A has varying rows and not a continuous set of rows filled. I have the wrong data and correct format below. If we go with the second example. How can I tweak the code accordingly? I tried to do it but then it doesn't run.

COLUMN A is Incorrect Here. It' shouldn't be numbers but names accordingly. THe following table is correct.

Excel 2007
ABCDEFGHIJKLMNOPQ
11STU
DY
Pres
ent
GenNum
ber
racesou
rce
pri
ority
dated
date
losshortdiagAdDiRe
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

<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet

The following is correct

Excel 2007
ABCDEFGHIJKLMNOPQ
1NameSTU
DY
Pres
ent
GenNum
ber
racesou
rce
pri
ority
dated
date
losshortdiagAdDiRe
2John Smith1YF30O1223432452342138120081201MM**
3
John Smith

<tbody>
</tbody>
3YM40B235235322131238023080235SSY
8E9688
9E8495
10
John Smith

<tbody>
</tbody>
5NF25M234645563453422348238282302SOY
178082
188056
192851
2081201
21920
2285011
23E8147
24E8495
254019
262724
27

<colgroup><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet
 
Last edited:
Upvote 0
Hi Hiker95,

Thanks again for all your help. So I think in the last example, in error Column A turned into a continuous row numbers when those were actually names which were varying too--thought this might be the reason why it didn't work. I tried Rick's macro which worked after I stripped all the formatting from the database. For some reason your macro kept crashing the excel file and made the computer go completely blank :confused:. I tried the reformatting it also and it still happened. Check the reply I put for Rick's code.
 
Upvote 0
If I understand what the problem might have been originally, and given the method you used to "fix" it, I think the following code should work on your original file directly without you having to export it at all... give this macro a try and let us know if it worked or not.
Code:
Sub TransposeDiagData()
  Dim LastRow As Long, Blanks As Range, Ar As Range
  LastRow = Cells(Rows.Count, "M").End(xlUp).Row
  Range("A1:A" & LastRow).Value = Range("A1:A" & LastRow).Value
  On Error GoTo NoBlanks
  Set Blanks = Range("A1:A" & LastRow).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
 
Upvote 0
biostatistician,

I copied your text display, but, when I pasted into a worksheet it seemed different/not correct?????

Is the following screenshot correct in reference to rows, and, columns?


Excel 2007
ABCDEFGHIJKLMNOPQ
1NameSTU DYPres entGenNum berracesou rcepri oritydated datelosshortdiagAdDiRe
2John Smith1YF30O1223432452342138120081201MM**
3John Smith3YM40B235235322131238023080235SSY
4E9688
5E8495
6John Smith5NF25M234645563453422348238282302SOY
78082
88056
92851
1081201
11920
1285011
13E8147
14E8495
154019
162724
17
Sheet1
 
Upvote 0
If I understand what the problem might have been originally, and given the method you used to "fix" it, I think the following code should work on your original file directly without you having to export it at all... give this macro a try and let us know if it worked or not.
Code:
Sub TransposeDiagData()
  Dim LastRow As Long, Blanks As Range, Ar As Range
  LastRow = Cells(Rows.Count, "M").End(xlUp).Row
  Range("A1:A" & LastRow).Value = Range("A1:A" & LastRow).Value
  On Error GoTo NoBlanks
  Set Blanks = Range("A1:A" & LastRow).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


OH MY GOD. That was bloody BRILLIANT!
Thank you thank you thank you so much!

The code works PERFECTLY!
Thank you so much Rick, and thank you Hiker95 and MikeG for helping me with this! I wish I realized I could've used macros earlier for this type of excel work. It didn't come to my mind until very recently!

Again, THANK YOU SO MUCH! I've been struggling for a week now!! I can finally get sleep! :)
 
Upvote 0
Thank you Hiker95! Yes that's the correct display. Rick was able to provide a code that finally worked with the original formatting of the file. I tried stripping all the formatting into a CSV and then re-exporting as XLSM which seemed to help. But regardless thank you so much for your efforts to try and help me figure this out. This has saved me so many hours of countless copy/pasting. It became almost mind-numbing.
 
Upvote 0
biostatistician,

Yes that's the correct display.

Then my ReorgData_V2 will work correctly.

Please give it a try, and, let me know your results.

Here are my results:


Excel 2007
ABCDEFGHIJKLM
1NameSTU DYPres entGenNum berracesou rcepri oritydated datelosshortdiag
2John Smith1YF30O1223432452342138120081201
3John Smith3YM40B235235322131238023080235
4John Smith5NF25M234645563453422348238282302
5
Sheet1



Excel 2007
LMNOPQRSTUVWXYZ
1shortdiagAdDiRe
28120081201MM**
38023080235SSYE9688E8495
48238282302SOY8082805628518120192085011E8147E849540192724
5
Sheet1
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,922
Members
449,094
Latest member
teemeren

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