Macro For Transpose Paste Into New Rows

Biggy

New Member
Joined
Feb 14, 2014
Messages
22
Hello Excel Community.

Once again I got a problem. This one is similar previous one, but at the same time different.
Original:

ABCDEFGHIJKLM
1NameColor810121416182022Price
2T-ShirtWhiteN$10
3T-ShirtBlack$14
4
5NameColorMLXLXXLPrice
6Polo ShirtYellowN$12
7Polo ShirtOrangeN$12
8Polo ShirtBlack$16

<tbody>
</tbody>


Should be

ABCDE
13NameColorSizeAvailablePrice
14T-ShirtWhite8$10
15T-ShirtWhite10$10
16T-ShirtWhite12$10
17T-ShirtWhite14N$10
18T-ShirtWhite16$10
19T-ShirtWhite18$10
20T-ShirtWhite20$10
21T-ShirtWhite22$10
22T-ShirtBlack8$14
23T-ShirtBlack10$14
24T-ShirtBlack12$14
25T-ShirtBlack14$14
26T-ShirtBlack16$14
27T-ShirtBlack18$14
28T-ShirtBlack20$14
29T-ShirtBlack22$14
30
31
32NameColorSizeAvailablePrice
33Polo ShirtYellowMN$14
34Polo ShirtYellowL$14
35Polo ShirtYellowXL$14
36Polo ShirtYellowXXL$14
37Polo ShirtOrangeM$14
38Polo ShirtOrangeL$14
39Polo ShirtOrangeXLN$14
40Polo ShirtOrangeXXL$14
41Polo ShirtOrangeM$16
42Polo ShirtOrangeL$16
43Polo ShirtOrangeXL$16
44Polo ShirtOrangeXXL$16

<tbody>
</tbody>


Here situation ,in my opinion, is lot more difficult, because number columns in which are sizes are moving (but they always finishing with Column next to the price), as well as necessary to copy items availability info to corresponding sizes for each color.

Honestly, I don't know if it's even possible, but here are lot of super smart and knowledgeable excel users, so I give it a shot.

My guess is that most realistic possibility would be to make macro in which is possible to choose area in which to perform given actions, because I am a realist - it's not possible to automate it completely, because the way it's formatted.

Thank You so much and hopefully someone will be able to help with it.
Sincerely,
Biggy

<tbody>
</tbody>



 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Give this macro a try (it outputs the transposed data beneath the existing data to match what you showed in the layout/results example that you posted)...
Code:
[table="width: 500"]
[tr]
	[td]Sub TransposeSizeTable()
  Dim X As Long, Cnt As Long, LastRow As Long, NewStart As Long, Ar As Range
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  NewStart = LastRow + 5
  For Each Ar In Range("A1:A" & LastRow).SpecialCells(xlConstants).Areas
    Cells(NewStart, "A").Resize(, 5) = Array("Name", "Color", "Size", "Available", "Price")
    Cnt = Application.CountA(Ar(1).Offset(, 2).Resize(, 10))
    For X = 1 To Ar.Rows.Count - 1
      Cells(NewStart + X, "A").Resize(Cnt, 2).Value = Ar(1).Offset(X).Resize(, 2).Value
      Cells(NewStart + X, "C").Resize(Cnt) = Application.Transpose(Ar(1).Offset(, 12 - Cnt).Resize(, Cnt))
      Cells(NewStart + X, "E").Resize(Cnt).Value = Ar(1).Offset(X, 12).Value
      If X = 1 Then
        Cells(NewStart + X, "D").Resize(Cnt) = Application.Transpose(Ar(1).Offset(X, 12 - Cnt).Resize(, Cnt))
      Else
        Cells(NewStart + X, "D").Resize(Cnt) = Application.Transpose(Ar(1).Offset(X, 12 - Cnt).Resize(, Cnt))
      End If
      NewStart = NewStart + Cnt - 1
    Next
    NewStart = Cells(Rows.Count, "A").End(xlUp).Row + 3
  Next
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Biggy,

1. What version of Excel, and, Windows are you using?

2. Are you using a PC or a Mac?


Here is a macro solution for you to consider that re-uses two arrays in memory, that is based on the raw data structure that you have displayed.

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

Sample raw data, and, results:


Excel 2007
ABCDEFGHIJKLM
1NameColor810121416182022Price
2T-ShirtWhiteN$10
3T-ShirtBlack$14
4
5NameColorMLXLXXLPrice
6Polo ShirtYellowN$12
7Polo ShirtOrangeN$12
8Polo ShirtBlack$16
9
10
11
12
13NameColorSizeAvailablePrice
14T-ShirtWhite8$10
15T-ShirtWhite10$10
16T-ShirtWhite12$10
17T-ShirtWhite14N$10
18T-ShirtWhite16$10
19T-ShirtWhite18$10
20T-ShirtWhite20$10
21T-ShirtWhite22$10
22T-ShirtBlack8$14
23T-ShirtBlack10$14
24T-ShirtBlack12$14
25T-ShirtBlack14$14
26T-ShirtBlack16$14
27T-ShirtBlack18$14
28T-ShirtBlack20$14
29T-ShirtBlack22$14
30
31
32NameColorSizeAvailablePrice
33Polo ShirtYellowMN$12
34Polo ShirtYellowL$12
35Polo ShirtYellowXL$12
36Polo ShirtYellowXXL$12
37Polo ShirtOrangeM$12
38Polo ShirtOrangeL$12
39Polo ShirtOrangeXLN$12
40Polo ShirtOrangeXXL$12
41Polo ShirtBlackM$16
42Polo ShirtBlackL$16
43Polo ShirtBlackXL$16
44Polo ShirtBlackXXL$16
45
Original


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, 03/06/2016, ME926207
Dim a As Variant, i As Long
Dim o As Variant, j As Long
Dim lr As Long, lc As Long
Dim c As Long, sc As Long, nsr As Long, n As Long
Dim Area As Range, sr As Long, er As Long
Application.ScreenUpdating = False
With Sheets("Original")   '<-- you can change the sheet name here
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  nsr = lr + 5
  For Each Area In .Range("A1:A" & lr).SpecialCells(xlCellTypeConstants).Areas
    With Area
      sr = .Row
      er = sr + .Rows.Count - 1
      sc = Cells(sr, 2).End(xlToRight).Column
      a = Range(Cells(sr, 1), Cells(er, lc))
      n = (lc - sc) + 1
      ReDim o(1 To n * (.Rows.Count - 1), 1 To 5)
      For i = 2 To UBound(a, 1)
        For c = sc To lc - 1 Step 1
          j = j + 1
          o(j, 1) = a(i, 1): o(j, 2) = a(i, 2)
          o(j, 3) = a(1, c)
          o(j, 4) = a(i, c)
          o(j, 5) = a(i, lc)
        Next c
      Next i
      With Cells(nsr, 1).Resize(, 5)
        .Value = Array("Name", "Color", "Size", "Available", "Price")
        .Font.Bold = True
        .Interior.ThemeColor = xlThemeColorDark1
        .Interior.TintAndShade = -0.249946592608417
      End With
      Cells(nsr, 2).Resize(, 4).HorizontalAlignment = xlCenter
      Cells(nsr + 1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
      Range("B" & nsr + 1 & ":E" & UBound(o, 1)).HorizontalAlignment = xlCenter
      Range("E" & nsr + 1 & ":E" & UBound(o, 1)).NumberFormat = "$#,##0_);[Red]($#,##0)"
      nsr = nsr + UBound(o, 1) + 1
      Erase a: Erase o: j = 0: sc = 0
    End With
  Next Area
  .Columns.AutoFit
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 ReorgData macro.
 
Upvote 0
Wow, Just woooow! I can’t believe how fast I got an answer! Thank You soooo much! You are just lifesaver!

Maybe it’s possible to explain how to change it, in case there are more or less columns with a sizes or there is item Code row in front of the name row?

This would be very helpful, so I can use it also in the future in the similar situation not to ask here again.

Thank You again!

All the best,
Tony
 
Upvote 0
Thank you a lot! It worked great! I can’t believe that I got two fixes for this issue!
I am using Windows 10 with Excel 2013 on the PC.
Maybe it’s possible also for this macro to give an example or even explanation how to adapt it if there are more columns for sizes or items code # goes in front of the name? I am just trying to figure out for future similar situations so I don’t have to ask again. I like to understand not just to use it.
Thanx again a lot!!!

Sincerely,
Tony.
 
Upvote 0
Biggy,

Thanks for the feedback.

You are very welcome. Glad we could help.

Maybe it’s possible also for this macro to give an example or even explanation how to adapt it if there are more columns for sizes or items code # goes in front of the name? I am just trying to figure out for future similar situations so I don’t have to ask again.

Can we see more screenshots of your new raw data, and, results?
 
Upvote 0
Hello,
Here are new format (this also works as example, because in the future situation could change). There are added more columns for sizes and in column A now sits "item#"

Before
ABCDEFGHIJKLMNOP
1Item#NameColor8101214161820222426Price
2TSH100T-ShirtWhiteN$10,
3TSH101T-ShirtBlack$14,
4
5Item#NameColorMLXLXXL3XL4XLPrice
6PLSH100Polo ShirtYellowN$12,
7PLSH101Polo ShirtOrangeN$12,
8PLSH102Polo ShirtBlack$15,

<tbody>
</tbody>




After
ABCDEF
11Item#NameColorSizeAvailablePrice
12TSH100T-ShirtWhite8$10,
13TSH100T-ShirtWhite10$10,
14TSH100T-ShirtWhite12$10,
15TSH100T-ShirtWhite14N$10,
16TSH100T-ShirtWhite16$10,
17TSH100T-ShirtWhite18$10,
18TSH100T-ShirtWhite20$10,
19TSH100T-ShirtWhite22$10,
20TSH100T-ShirtWhite24$10,
21TSH100T-ShirtWhite26$10,
22TSH101T-ShirtBlack8$14,
23TSH101T-ShirtBlack10$14,
24TSH101T-ShirtBlack12$14,
25TSH101T-ShirtBlack14$14,
26TSH101T-ShirtBlack16$14,
27TSH101T-ShirtBlack18$14,
28TSH101T-ShirtBlack20$14,
29TSH101T-ShirtBlack22$14,
30TSH101T-ShirtBlack24$14,
31TSH101T-ShirtBlack26$14,
32
33
34Item#NameColorSizeAvailablePrice
35PLSH100Polo ShirtYellowMN$14,
36PLSH100Polo ShirtYellowL$14,
37PLSH100Polo ShirtYellowXL$14,
38PLSH100Polo ShirtYellowXXL$14,
39PLSH100Polo ShirtYellow3XL$14,
40PLSH100Polo ShirtYellow4XL$14,
41PLSH101Polo ShirtOrangeM$14,
42PLSH101Polo ShirtOrangeL$14,
43PLSH101Polo ShirtOrangeXLN$14,
44PLSH101Polo ShirtOrangeXXL$14,
45PLSH101Polo ShirtOrange3XL$14,
46PLSH101Polo ShirtOrange4XL$14,
47PLSH102Polo ShirtBlackM$15,
48PLSH102Polo ShirtBlackL$15,
49PLSH102Polo ShirtBlackXL$15,
50PLSH102Polo ShirtBlackXXL$15,
51PLSH102Polo ShirtBlack3XL$15,
52PLSH102Polo ShirtBlack4XL$15,

<tbody>
</tbody>
 
Upvote 0
Biggy,

Here is a new macro solution for you to consider that re-uses two arrays in memory, that is based on the new raw data structure that you have displayed.

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

Sample raw data, and, results:


Excel 2007
ABCDEFGHIJKLMNOP
1Item#NameColor8101214161820222426Price
2TSH100T-ShirtWhiteN$10,
3TSH101T-ShirtBlack$14,
4
5Item#NameColorMLXLXXL3XL4XLPrice
6PLSH100Polo ShirtYellowN$12,
7PLSH101Polo ShirtOrangeN$12,
8PLSH102Polo ShirtBlack$15,
9
10
11Item#NameColorSizeAvailablePrice
12TSH100T-ShirtWhite8$10,
13TSH100T-ShirtWhite10$10,
14TSH100T-ShirtWhite12$10,
15TSH100T-ShirtWhite14N$10,
16TSH100T-ShirtWhite16$10,
17TSH100T-ShirtWhite18$10,
18TSH100T-ShirtWhite20$10,
19TSH100T-ShirtWhite22$10,
20TSH100T-ShirtWhite24$10,
21TSH100T-ShirtWhite26$10,
22TSH101T-ShirtBlack8$14,
23TSH101T-ShirtBlack10$14,
24TSH101T-ShirtBlack12$14,
25TSH101T-ShirtBlack14$14,
26TSH101T-ShirtBlack16$14,
27TSH101T-ShirtBlack18$14,
28TSH101T-ShirtBlack20$14,
29TSH101T-ShirtBlack22$14,
30TSH101T-ShirtBlack24$14,
31TSH101T-ShirtBlack26$14,
32
33
34Item#NameColorSizeAvailablePrice
35PLSH100Polo ShirtYellowMN$12,
36PLSH100Polo ShirtYellowL$12,
37PLSH100Polo ShirtYellowXL$12,
38PLSH100Polo ShirtYellowXXL$12,
39PLSH100Polo ShirtYellow3XL$12,
40PLSH100Polo ShirtYellow4XL$12,
41PLSH101Polo ShirtOrangeM$12,
42PLSH101Polo ShirtOrangeL$12,
43PLSH101Polo ShirtOrangeXLN$12,
44PLSH101Polo ShirtOrangeXXL$12,
45PLSH101Polo ShirtOrange3XL$12,
46PLSH101Polo ShirtOrange4XL$12,
47PLSH102Polo ShirtBlackM$15,
48PLSH102Polo ShirtBlackL$15,
49PLSH102Polo ShirtBlackXL$15,
50PLSH102Polo ShirtBlackXXL$15,
51PLSH102Polo ShirtBlack3XL$15,
52PLSH102Polo ShirtBlack4XL$15,
53
Original


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, 03/07/2016, ME926207
Dim a As Variant, i As Long
Dim o As Variant, j As Long
Dim lr As Long, lc As Long
Dim c As Long, sc As Long, nsr As Long, n As Long
Dim Area As Range, sr As Long, er As Long
Application.ScreenUpdating = False
With Sheets("Original")   '<-- you can change the sheet name here
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  nsr = lr + 3
  For Each Area In .Range("A1:A" & lr).SpecialCells(xlCellTypeConstants).Areas
    With Area
      sr = .Row
      er = sr + .Rows.Count - 1
      sc = Cells(sr, 3).End(xlToRight).Column
      a = Range(Cells(sr, 1), Cells(er, lc))
      n = (lc - sc) + 1
      ReDim o(1 To n * (.Rows.Count - 1), 1 To 6)
      For i = 2 To UBound(a, 1)
        For c = sc To lc - 1 Step 1
          j = j + 1
          o(j, 1) = a(i, 1): o(j, 2) = a(i, 2): o(j, 3) = a(i, 3)
          o(j, 4) = a(1, c)
          o(j, 5) = a(i, c)
          o(j, 6) = a(i, lc)
        Next c
      Next i
      With Cells(nsr, 1).Resize(, 6)
        .Value = Array("Item#", "Name", "Color", "Size", "Available", "Price")
        .Interior.ThemeColor = xlThemeColorDark1
        .Interior.TintAndShade = -0.249946592608417
      End With
      Cells(nsr, 1).Resize(, 6).HorizontalAlignment = xlCenter
      Cells(nsr + 1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
      Range("C" & nsr + 1 & ":F" & UBound(o, 1)).HorizontalAlignment = xlCenter
      Range("E" & nsr + 1 & ":F" & UBound(o, 1)).NumberFormat = "General"
      nsr = nsr + UBound(o, 1) + 1
      Erase a: Erase o: j = 0: sc = 0
    End With
  Next Area
  .Columns.AutoFit
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 ReorgData_V2macro.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

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