Transposing data SETS from vertical to horizontal

tutuzdad

New Member
Joined
Mar 16, 2015
Messages
14
I desperately need to transpose multiple SETS of data so that the now vertical columns are horizontal. I know about transposing but not when it comes to sets. For simplicity sake let’s say I have a long list of multiple addresses with the headers, NAME, ADDR, CITY, STATE, ZIP. Transposing these headers into a horizontal table is fairly simple. It’s getting the 5 rows of names, addresses, cities, states and zips under the correct headers that has me confounded. I’m using 2010. I’m pretty familiar with excel. Ideally a solution that I can reapply to future lists since this is something I do regularly and my output is always formatted this same annoying way. I don’t do VB but if we have to and you can simply it step-by-step for me I’d really appreciate it.
 
tutuzdad,

If I now understand you correctly.

Here is a macro solution for you to consider that should adjust to the varying number of rows, and, columns in your raw data worksheet.

Sample raw data in worksheet Sheet1 (you can change the raw data worksheet name in the macro):


Excel 2007
ABCDE
1NameFredCarlNorman
2Addr101 Main St.21 First St.200 Paved St.
3CityAnchorageClifordAustin
4StateAKNDTX
5Zip995015801673301
6
Sheet1


After the macro (using two arrays in memory) in a new worksheet Results:


Excel 2007
ABCDEF
1NameAddrCityStateZip
2Fred101 Main St.AnchorageAK99501
3Carl21 First St.ClifordND58016
4Norman200 Paved St.AustinTX73301
5
Results


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 TransposeData()
' hiker95, 03/18/2015, ME842652
Dim w1 As Worksheet, wr As Worksheet
Dim a As Variant, o As Variant
Dim i As Long, j As Long, c As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   ''<-- change the raw data worksheet name here
a = w1.Cells(1, 1).CurrentRegion
ReDim o(1 To UBound(a, 2), 1 To UBound(a, 1))
For c = 1 To UBound(a, 2)
  j = j + 1
  For i = 1 To UBound(a, 1)
    o(j, i) = a(i, c)
  Next i
Next c
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wr = Sheets("Results")
With wr
  .UsedRange.Clear
  .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns(1).Resize(, UBound(o, 2)).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 TransposeData macro.
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
tutuzdad,

Using some of the raw data displayed by Lee., and, the above macro.


Excel 2007
ABCDEF
1first_nameAleshiaEvanFranceUlysses
2last_nameTomkiewiczZigomalasAndradeMcwalters
3company_nameAlan D Rosenburg Cpa PcCap Gemini AmericaElliott, John W EsqMcmahan, Ben L
4address14 Taylor St5 Binney St8 Moor Place505 Exeter Rd
5citySt. Stephens WardAbbey WardEast Southbourne and Tuckton WHawerby cum Beesby
6countyKentBuckinghamshireBournemouthLincolnshire
7postalCT2 7PPHP11 2AXBH6 3BEDN36 5RP
8phone101835-70359701937-86471501347-36822201912-771311
9phone201944-36996701714-73766801935-82163601302-601380
10emailatomkiewicz@hotmail.comevan.zigomalas@gmail.comfrance.andrade@hotmail.comulysses@hotmail.com
11
Sheet1


And, the results in 2 screenshots to fit the MrExcel display area:


Excel 2007
ABCDE
1first_namelast_namecompany_nameaddresscity
2AleshiaTomkiewiczAlan D Rosenburg Cpa Pc14 Taylor StSt. Stephens Ward
3EvanZigomalasCap Gemini America5 Binney StAbbey Ward
4FranceAndradeElliott, John W Esq8 Moor PlaceEast Southbourne and Tuckton W
5UlyssesMcwaltersMcmahan, Ben L505 Exeter RdHawerby cum Beesby
6
Results



Excel 2007
FGHIJK
1countypostalphone1phone2email
2KentCT2 7PP01835-70359701944-369967atomkiewicz@hotmail.com
3BuckinghamshireHP11 2AX01937-86471501714-737668evan.zigomalas@gmail.com
4BournemouthBH6 3BE01347-36822201935-821636france.andrade@hotmail.com
5LincolnshireDN36 5RP01912-77131101302-601380ulysses@hotmail.com
6
Results
 
Upvote 0
tutuzdad, If I now understand you correctly. Here is a macro solution for you to consider that should adjust to the varying number of rows, and, columns in your raw data worksheet. Sample raw data in worksheet Sheet1 (you can change the raw data worksheet name in the macro): Excel 2007
ABCDE
1NameFredCarlNorman
2Addr101 Main St.21 First St.200 Paved St.
3CityAnchorageClifordAustin
4StateAKNDTX
5Zip995015801673301
6

<colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /><col /><col /><col /></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

After the macro (using two arrays in memory) in a new worksheet Results: Excel 2007
ABCDEF
1NameAddrCityStateZip
2Fred101 Main St.AnchorageAK99501
3Carl21 First St.ClifordND58016
4Norman200 Paved St.AustinTX73301
5

<colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /><col /><col /><col /><col /></colgroup><thead>
</thead><tbody>
</tbody>
Results

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 TransposeData() ' hiker95, 03/18/2015, ME842652 Dim w1 As Worksheet, wr As Worksheet Dim a As Variant, o As Variant Dim i As Long, j As Long, c As Long Application.ScreenUpdating = False Set w1 = Sheets("Sheet1")   ''<-- change the raw data worksheet name here a = w1.Cells(1, 1).CurrentRegion ReDim o(1 To UBound(a, 2), 1 To UBound(a, 1)) For c = 1 To UBound(a, 2)   j = j + 1   For i = 1 To UBound(a, 1)     o(j, i) = a(i, c)   Next i Next c If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results" Set wr = Sheets("Results") With wr   .UsedRange.Clear   .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o   .Columns(1).Resize(, UBound(o, 2)).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 TransposeData macro.
Thank you. I see you have put a lot of work and thought into your answer and I greatly appreciate it. Due to my own inability to make this easier by uploading my worksheet I can only describe it and hope it makes sense. I only have two columns in my raw data. In your first example above called "Sheet 1", imagine column A exactly the way you have it, however, A6 begins the recurring headers anew, (A6)NAME, (A7)ADDR, (A8)CITY, (A9)STATE, (A10) ZIP, and so forth. These continue to repeat through about A3000. The sets of addresses fall the same way in column B next to the headings in column A and on every 6th row begins a new address set of a name, followed by an address, followed by a city, etc. THOSE are what I need to transpose to a table configuration.
 
Upvote 0
I guess you could also say that I need to transpose 5 rows in the column B list to my 5 column table and then the next 5, and the next 5, and so on.
 
Upvote 0
tutuzdad,

NEW sample raw data in worksheet Sheet1 (you can change the raw data worksheet name in the macro):


Excel 2007
ABC
1NameFred
2Addr101 Main St.
3CityAnchorage
4StateAK
5Zip99501
6NameCarl
7Addr21 First St.
8CityCliford
9StateND
10Zip58016
11NameNorman
12Addr200 Paved St.
13CityAustin
14StateTX
15Zip73301
16
Sheet1


After the macro (using two arrays in memory) in a new worksheet Results:


Excel 2007
ABCDEF
1NAMEADDRCITYSTATEZIP
2Fred101 Main St.AnchorageAK99501
3Carl21 First St.ClifordND58016
4Norman200 Paved St.AustinTX73301
5
Results


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 TransposeData_V2()
' hiker95, 03/18/2015, ME842652
Dim w1 As Worksheet, wr As Worksheet
Dim a As Variant, o As Variant
Dim i As Long, j As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   ''<-- change the raw data worksheet name here
a = w1.Cells(1, 1).CurrentRegion
ReDim o(1 To UBound(a, 1) / 5, 1 To 5)
For i = 1 To UBound(a, 1) Step 5
  j = j + 1
  o(j, 1) = a(i, 2)
  o(j, 2) = a(i + 1, 2)
  o(j, 3) = a(i + 2, 2)
  o(j, 4) = a(i + 3, 2)
  o(j, 5) = a(i + 4, 2)
Next i
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wr = Sheets("Results")
With wr
  .UsedRange.Clear
  .Cells(1, 1).Resize(, 5).Value = Array("NAME", "ADDR", "CITY", "STATE", "ZIP")
  .Cells(2, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns(1).Resize(, UBound(o, 2)).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 TransposeData_V2 macro.
 
Upvote 0
THAT IS IT EXACTLY! I knew there was a genius on here somewhere. Now if I may trouble you one time further, if you can just tell me where to tweak this macro recipe so that it accommodates by ACTUAL headers (which I hesitated to burden you with) I can try to apply the tweak what you have given me myself so you won't have to do all that typing. There are 19 of them (these are the repeating headers from column A whose columns will be populated with 19 unique values in column B): City Date1 Date2 Date3 Date4 Number Nature Address State ZIP Contact Area Received by How received Agency Off Geo ID Clear
 
Upvote 0
If I knew how to insert a code like you have I would have sent you the whole macro back with at least "some" of the changes already done to save you some time.
 
Upvote 0
tutuzdad,

THAT IS IT EXACTLY!

Thanks for the feedback.

You are very welcome. Glad I could help.

Now if I may trouble you one time further, if you can just tell me where to tweak this macro recipe so that it accommodates by ACTUAL headers


One last try:

So, your actual raw data consists of sets of 19 rows, 2 columns, columns A, and B?
 
Upvote 0
tutuzdad, Thanks for the feedback. You are very welcome. Glad I could help. One last try: So, your actual raw data consists of sets of 19 rows, 2 columns, columns A, and B?
That is correct. In my example I dumbed it down to only 5 and called it an address list because it's the same concept and I felt really bad asking someone to spend a great deal of time formatting all of the REAL stuff for me. But now that the macro is written and the basic problem is solved, I thought maybe I can simply replace a few lines with my real 19 row headers and modify it to suit my needs (which I will gladly do if you can just point out which lines need it).
 
Upvote 0
tutuzdad,

New sample raw data in worksheet Sheet1 (you can change the raw data worksheet name in the macro):


Excel 2007
ABC
1A1
2B2
3C3
4D4
5E5
6F6
7G7
8H8
9I9
10J10
11K11
12L12
13M13
14N14
15O15
16P16
17Q17
18R18
19S19
20A20
21B21
22C22
23D23
24E24
25F25
26G26
27H27
28I28
29J29
30K30
31L31
32M32
33N33
34O34
35P35
36Q36
37R37
38S38
39A39
40B40
41C41
42D42
43E43
44F44
45G45
46H46
47I47
48J48
49K49
50L50
51M51
52N52
53O53
54P54
55Q55
56R56
57S57
58
Sheet1


After the macro (using two arrays in memory) in a new worksheet Results:


Excel 2007
ABCDEFGHIJKLMNOPQRST
1ABCDEFGHIJKLMNOPQRS
212345678910111213141516171819
320212223242526272829303132333435363738
439404142434445464748495051525354555657
5
Results


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 TransposeData_V3()
' hiker95, 03/18/2015, ME842652
Dim w1 As Worksheet, wr As Worksheet
Dim a As Variant, o As Variant
Dim i As Long, j As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   ''<-- change the raw data worksheet name here
a = w1.Cells(1, 1).CurrentRegion
ReDim o(1 To UBound(a, 1) / 19, 1 To 19)
For i = 1 To UBound(a, 1) Step 19
  j = j + 1
  o(j, 1) = a(i, 2)
  o(j, 2) = a(i + 1, 2)
  o(j, 3) = a(i + 2, 2)
  o(j, 4) = a(i + 3, 2)
  o(j, 5) = a(i + 4, 2)
  o(j, 6) = a(i + 5, 2)
  o(j, 7) = a(i + 6, 2)
  o(j, 8) = a(i + 7, 2)
  o(j, 9) = a(i + 8, 2)
  o(j, 10) = a(i + 9, 2)
  o(j, 11) = a(i + 10, 2)
  o(j, 12) = a(i + 11, 2)
  o(j, 13) = a(i + 12, 2)
  o(j, 14) = a(i + 13, 2)
  o(j, 15) = a(i + 14, 2)
  o(j, 16) = a(i + 15, 2)
  o(j, 17) = a(i + 16, 2)
  o(j, 18) = a(i + 17, 2)
  o(j, 19) = a(i + 18, 2)
Next i
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wr = Sheets("Results")
With wr
  .UsedRange.Clear
  .Cells(1, 1).Resize(, 19).Value = Application.Transpose(w1.Range("A1:A19"))
  .Cells(2, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns(1).Resize(, UBound(o, 2)).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 TransposeData_V3 macro.
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,029
Members
449,092
Latest member
ikke

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