Complicated excel transpose

silverdollar

New Member
Joined
May 11, 2017
Messages
2
Hello,

I have a set of data that contains data using a combination of columns and rows. I would like to transpose the columns into the rows so I would be able to create a load file. I'm not quite sure how to explain this but please see the screen print below to see what I am trying to accomplish. The actual data set has over 200 rows and 200 columns, the end result would have roughly 40,000 (200*200) rows of data. If anyone has a good suggestion on creating this data set, please let me know. I would appreciate any suggestions. Thanks everyone!!

BEFORE:
Unit AUnit BUnit C
GL-1000100500
GL-10012007575
GL-20003000-100
GL-2002-4000100
GL-3000500-100200
GL-300160050300

<tbody>
</tbody>


<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}o\:* {behavior:url(#default#VML);}x\:* {behavior:url(#default#VML);}.shape {behavior:url(#default#VML);}</style><![endif]-->
AFTER:
AccountUnitAmount
GL-1000Unit A100
GL-1001Unit A200
GL-2000Unit A300
GL-2002Unit A-400
GL-3000Unit A500
GL-3001Unit A600
GL-1000Unit B50
GL-1001Unit B75
GL-2000Unit B0
GL-2002Unit B0
GL-3000Unit B-100
GL-3001Unit B50
GL-1000Unit C0
GL-1001Unit C75
GL-2000Unit C-100
GL-2002Unit C100
GL-3000Unit C200
GL-3001Unit C300

<tbody>
</tbody>
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
silverdollar,

Welcome to the MrExcel forum.

Here is a macro solution for you to consider, that will adjust to the number of raw data rows, and, columns, and, that uses two arrays in memory, and, should be very fast.

You can change the raw data worksheet name in the macro.

The macro will create a new worksheet Results.

Sample raw data:


Excel 2007
ABCDE
1Unit AUnit BUnit C
2GL-1000100500
3GL-10012007575
4GL-20003000-100
5GL-2002-4000100
6GL-3000500-100200
7GL-300160050300
8
Sheet1


And, after the macro in worksheet Results:


Excel 2007
ABC
1AccountUnitAmount
2GL-1000Unit A100
3GL-1001Unit A200
4GL-2000Unit A300
5GL-2002Unit A-400
6GL-3000Unit A500
7GL-3001Unit A600
8GL-1000Unit B50
9GL-1001Unit B75
10GL-2000Unit B0
11GL-2002Unit B0
12GL-3000Unit B-100
13GL-3001Unit B50
14GL-1000Unit C0
15GL-1001Unit C75
16GL-2000Unit C-100
17GL-2002Unit C100
18GL-3000Unit C200
19GL-3001Unit C300
20
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 ReorganizeData()
' hiker95, 05/11/2017, ME1004903
Dim w1 As Worksheet, wr As Worksheet
Dim lr As Long, lc As Long
Dim a As Variant, i As Long, c As Long, n As Long
Dim o As Variant, j As Long
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")   '<-- you can change the sheet name here
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wr = Worksheets("Results")
wr.UsedRange.Clear
With w1
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  a = .Range(.Cells(1, 1), .Cells(lr, lc))
  n = Application.Count(.Range(.Cells(2, 2), .Cells(lr, lc)))
  ReDim o(1 To n + 1, 1 To 3)
  j = j + 1: o(j, 1) = "Account": o(j, 2) = "Unit": o(j, 3) = "Amount"
End With
For c = 2 To UBound(a, 2)
  For i = 2 To UBound(a, 1)
    j = j + 1: o(j, 1) = a(i, 1): o(j, 2) = a(1, c): o(j, 3) = a(i, c)
  Next i
Next c
With wr
  .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 ReorganizeData macro.
 
Upvote 0
silverdollar,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0
Thank you hiker95!! This works perfectly.
Here is another macro you can consider. There is no speed advantage between hiker95's code and my code, but I did take a different approach with respect to the result sheet. Rather than creating or, if this is run 2 or more, simply clearing the result sheet, I let you specify an empty worksheet to output the results to... that way, you can maintain copies of multiple runs of the program without having to manually transfer the result of one run to a different worksheet before running the program again. The red highlighted rows of code control where to look for the data and where to output the results (the variable names should make it obvious which is which). Anyway, my coding approach is different as well as shorter from hiker95's, so I thought others might find it interesting to dissect if you are into that sort of thing.
Code:
[table="width: 500"]
[tr]
	[td]Sub ReorganizeData()
  Dim c As Long, LastRow As Long, LastCol As Long, Data As Variant
  Dim DataSheet As Worksheet, ResultSheet As Worksheet
  Set DataSheet = Sheets("Sheet1")
  Set ResultSheet = Sheets("Sheet2")
  LastRow = DataSheet.Cells(Rows.Count, "A").End(xlUp).Row
  LastCol = DataSheet.Cells(1, Columns.Count).End(xlToLeft).Column
  Application.ScreenUpdating = False
  DataSheet.Cells(2, LastCol + 1).Resize(LastRow - 1, LastCol - 1) = DataSheet.Range("B1").Resize(, LastCol - 1).Value
  Data = Range(DataSheet.Cells(2, "A"), DataSheet.Cells(LastRow, 2 * LastCol - 1))
  ResultSheet.Range("A1:C1") = Array("Account", "Unit", "Amount")
  For c = 2 To LastCol
    ResultSheet.Cells(2 + (c - 2) * UBound(Data), "A").Resize(UBound(Data), 3) = Application.Index(Data, Evaluate("ROW(1:" & LastRow - 1 & ")"), Split("1 " & c + LastCol - 1 & " " & c))
  Next
  ResultSheet.Columns("A").Resize(, LastCol).AutoFit
  DataSheet.Columns(LastCol + 1).Resize(, LastCol).Clear
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
I wonder if anybody could please help with a variation on the above query.
My situation is very similar to the one above, but instead of the cells containing numbers, they contain text. I tried the code shown above and although it works perfectly when the cells contain numbers, it reveals an error if I change the details in the cells to text. I have shown a couple of tables below with some sample text to show what I would like. If anybody could help with this, it would be greatly appreciated. The complete table I wish it to work on is much larger than this, but i just wanted to show an example of what I would like to achieve.

Table Before

NameColour 1Colour 2Colour 3Colour 4Colour 5Colour 6
FredRedYellowGreen
JoeBluePurple
BillYellowWhiteBlackRed
JanePurpleOrangeBlueRedGreenYellow
StevePurpleBlackRedGreenWhite
HenryGreen

<tbody>
</tbody>

Table After

NameColour IDColour
FredColour 1Red
JoeColour 1Blue
BillColour 1Yellow
JaneColour 1Purple
SteveColour 1Purple
HenryColour 1Green
FredColour 2Yellow
JoeColour 2Purple
BillColour 2White
JaneColour 2Orange
SteveColour 2Black
HenryColour 2
FredColour 3Green
JoeColour 3
BillColour 3Black
JaneColour 3Blue
SteveColour 3Red
HenryColour 3
FredColour 4
JoeColour 4
BillColour 4Red
JaneColour 4Red
SteveColour 4Green
HenryColour 4
FredColour 5
JoeColour 5
BillColour 5
JaneColour 5Green
SteveColour 5White
HenryColour 5
FredColour 6
JoeColour 6
BillColour 6
JaneColour 6Yellow
SteveColour 6
HenryColour 6

<tbody>
</tbody>

Many thanks in anticipation
Paul
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

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