Transpose Large Data Help

devint

New Member
Joined
Dec 17, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Morning,

Hoping someone might have knowledge that may help me. I have to run a data pull every month. It comes in a certain format, and I need to transpose it into another format. I understand the transpose part, but only for a single set of data. My data is about 6000 rows long.

This is the format the data comes in:

G80012
ASHGREY
S​
M​
L​
XL​
2XL​
3XL​
4XL​
5XL​
RegularPrice
$2.24​
$2.24​
$2.24​
$2.24​
$4.43​
$6.03​
$5.98​
$5.99​
SpecialPrice
$1.92​
$1.92​
$1.92​
$1.92​
Total-Inventory
27322​
4868​
16127​
2190​
78​
0​
10​
11​
AZALEA
S​
M​
L​
XL​
2XL​
3XL​
4XL​
5XL​
RegularPrice
$2.32​
$2.32​
$2.32​
$2.32​
$4.91​
$6.34​
$6.28​
$6.28​
SpecialPrice
$2.03​
$2.03​
$2.03​
$2.03​
Total-Inventory
1646​
2000​
1355​
436​
427​
70​
0​
0​
BLACK
S​
M​
L​
XL​
2XL​
3XL​
4XL​
5XL​
RegularPrice
$2.32​
$2.32​
$2.32​
$2.32​
$4.91​
$6.34​
$6.28​
$6.28​
SpecialPrice
$2.03​
$2.03​
$2.03​
$2.03​
Total-Inventory
35935​
56924​
67178​
28203​
10723​
5075​
144​
1006​

My ideal end result would look like this:

Style​
Color​
Size​
RegularPrice​
SpecialPrice​
Total-Inventory​
G80012​
ASHGREY​
S​
$2.24​
$1.92​
27322​
G80012​
ASHGREY​
M​
$2.24​
$1.92​
4868​
G80012​
ASHGREY​
L​
$2.24​
$1.92​
16127​
G80012​
ASHGREY​
XL​
$2.24​
$1.92​
2190​
G80012​
ASHGREY​
2XL​
$4.43​
78​
G80012​
ASHGREY​
3XL​
$6.03​
0​
G80012​
ASHGREY​
4XL​
$5.98​
10​
G80012​
ASHGREY​
5XL​
$5.99​
11​
G80012​
AZALEA​
S​
$2.32​
$2.03​
1646​
G80012​
AZALEA​
M​
$2.32​
$2.03​
2000​
G80012​
AZALEA​
L​
$2.32​
$2.03​
1355​
G80012​
AZALEA​
XL​
$2.32​
$2.03​
436​
G80012​
AZALEA​
2XL​
$4.91​
427​
G80012​
AZALEA​
3XL​
$6.34​
70​
G80012​
AZALEA​
4XL​
$6.28​
0​
G80012​
AZALEA​
5XL​
$6.28​
0​
G80012​
BLACK​
S​
$2.32​
$2.03​
35935​
G80012​
BLACK​
M​
$2.32​
$2.03​
56924​
G80012​
BLACK​
L​
$2.32​
$2.03​
67178​
G80012​
BLACK​
XL​
$2.32​
$2.03​
28203​
G80012​
BLACK​
2XL​
$4.91​
10723​
G80012​
BLACK​
3XL​
$6.34​
5075​
G80012​
BLACK​
4XL​
$6.28​
144​
G80012​
BLACK​
5XL​
$6.28​
1006​

I can do a transpose and manually copy/paste each 'color', but with 150 colors this would take me a long time. Was hoping there was a formula or macro that can achieve this with a few strokes.

This is beyond my knowledge of excel, so hoping I can find help here.

Thanks,
 

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.
Welcome to the MrExcel board!

For the future, I suggest that you investigate XL2BB for providing sample data & expected results.

See if this macro is headed in the right direction. Test with a copy of your data.

VBA Code:
Sub Rearrange()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, r As Long, uba1 As Long, uba2 As Long
  Dim sStyle As String, sColor As String
  
  a = Range("A1").CurrentRegion.Value
  uba1 = UBound(a, 1)
  uba2 = UBound(a, 2)
  ReDim b(1 To (uba2 - 1) * (uba1 - 1) / 4, 1 To 6)
  sStyle = a(1, 1)
  For i = 2 To uba1 Step 4
    sColor = a(i, 1)
    For j = 2 To uba2
      r = r + 1
      b(r, 1) = sStyle
      b(r, 2) = sColor
      For k = 0 To 3
        b(r, 3 + k) = a(i + k, j)
      Next k
    Next j
  Next i
  Cells(1, uba2 + 2).Resize(, 6).Value = Split("Style|Color|Size|RegularPrice|SpecialPrice|Total-Inventory", "|")
  Cells(2, uba2 + 2).Resize(UBound(b, 1), 6).Value = b
End Sub

Here is my sample data and results

devint.xlsm
ABCDEFGHIJKLMNOP
1G80012StyleColorSizeRegularPriceSpecialPriceTotal-Inventory
2ASHGREYSMLXL2XL3XL4XL5XLG80012ASHGREYS$2.24$1.9227322
3RegularPrice$2.24$2.24$2.24$2.24$4.43$6.03$5.98$5.99G80012ASHGREYM$2.24$1.924868
4SpecialPrice$1.92$1.92$1.92$1.92G80012ASHGREYL$2.24$1.9216127
5Total-Inventory2732248681612721907801011G80012ASHGREYXL$2.24$1.922190
6AZALEASMLXL2XL3XL4XL5XLG80012ASHGREY2XL$4.4378
7RegularPrice$2.32$2.32$2.32$2.32$4.91$6.34$6.28$6.28G80012ASHGREY3XL$6.030
8SpecialPrice$2.03$2.03$2.03$2.03G80012ASHGREY4XL$5.9810
9Total-Inventory1646200013554364277000G80012ASHGREY5XL$5.9911
10BLACKSMLXL2XL3XL4XL5XLG80012AZALEAS$2.32$2.031646
11RegularPrice$2.32$2.32$2.32$2.32$4.91$6.34$6.28$6.28G80012AZALEAM$2.32$2.032000
12SpecialPrice$2.03$2.03$2.03$2.03G80012AZALEAL$2.32$2.031355
13Total-Inventory359355692467178282031072350751441006G80012AZALEAXL$2.32$2.03436
14G80012AZALEA2XL$4.91427
15G80012AZALEA3XL$6.3470
16G80012AZALEA4XL$6.280
17G80012AZALEA5XL$6.280
18G80012BLACKS$2.32$2.0335935
19G80012BLACKM$2.32$2.0356924
20G80012BLACKL$2.32$2.0367178
21G80012BLACKXL$2.32$2.0328203
22G80012BLACK2XL$4.9110723
23G80012BLACK3XL$6.345075
24G80012BLACK4XL$6.28144
25G80012BLACK5XL$6.281006
Sheet1
 
Upvote 0
Hi Peter,

Thank you for the response, tip, and the VBA code! Your code works well to get what I am looking for. However, every now and then I get a runtime error, and also get this runtime error when I try to run more than 200 lines (I have about 3000 lines).

1608310421677.png


According to the debugger, this line is the issue.

VBA Code:
        [COLOR=rgb(184, 49, 47)]b(r, 3 + k) = a(i + k, j)[/COLOR]

What is causing this to only work sometimes, and not others?

Thanks again for your help, it is very much appreciated!
 
Upvote 0
Actually, I believe I found the problem.

Some colors listed do not include the SpecialPrice row in the data set. Is there some way to alter the VBA code to account for that and leave that part blank in the new dataset?
 
Upvote 0
Here is an example of the data set. Highlighted in yellow is a color without a special price row.

Also, sorry for the multiple responses - no edit button that I can see :(

Test.xlsm
ABCDEFGHI
1G800
2ASHGREYSMLXL2XL3XL4XL5XL
3RegularPrice2.242.242.242.244.436.035.985.99
4SpecialPrice1.921.921.921.92
5Total-Inventory2732248681612721907801011
6AZALEASMLXL2XL3XL4XL5XL
7RegularPrice2.322.322.322.324.916.346.286.28
8SpecialPrice2.032.032.032.03
9Total-Inventory1646200013554364277000
10BLACKSMLXL2XL3XL4XL5XL
11RegularPrice2.322.322.322.324.916.346.286.28
12SpecialPrice2.032.032.032.03
13Total-Inventory359355692467178282031072350751441006
14CAROLINABLUESMLXL2XL3XL4XL5XL
15RegularPrice2.322.322.322.324.916.346.286.28
16SpecialPrice2.032.032.032.03
17Total-Inventory27833594735804363417955600
18DAISYSMLXL2XL3XL4XL5XL
19RegularPrice2.322.322.322.324.916.346.286.28
20Total-Inventory2788610266447099971126258490
21DARKCHOCOLATESMLXL2XL3XL4XL5XL
22RegularPrice2.562.562.562.564.465.995.995.99
23SpecialPrice2.030.752.032.03
24Total-Inventory005100000
25DARKHEATHERSMLXL2XL3XL4XL5XL
26RegularPrice2.322.322.322.324.916.346.286.28
27SpecialPrice2.032.032.032.03
28Total-Inventory22425924313362328626330031
29ELECTRICGREENSMLXL2XL3XL4XL5XL
30RegularPrice2.322.322.322.324.916.346.286.28
31SpecialPrice2.032.032.032.03
32Total-Inventory2080621523198421209834097963130
Sheet1
 
Upvote 0
With your sample here is an alternative solution using Power Query

Book3
ABCDEF
1Data.G80012Data.ASHGREYAttributeRegularPriceSpecialPriceTotal-Inventory
2G80012ASHGREY2XL$4.4378
3G80012ASHGREY3XL$6.030
4G80012ASHGREY4XL$5.9810
5G80012ASHGREY5XL$5.9911
6G80012ASHGREYL$2.24$1.9216127
7G80012ASHGREYM$2.24$1.924868
8G80012ASHGREYS$2.24$1.9227322
9G80012ASHGREYXL$2.24$1.922190
10G80012AZALEA2XL$4.91427
11G80012AZALEA3XL$6.3470
12G80012AZALEA4XL$6.280
13G80012AZALEA5XL$6.280
14G80012AZALEAL$2.32$2.031355
15G80012AZALEAM$2.32$2.032000
16G80012AZALEAS$2.32$2.031646
17G80012AZALEAXL$2.32$2.03436
18G80012BLACK2XL$4.9110723
19G80012BLACK3XL$6.345075
20G80012BLACK4XL$6.28144
21G80012BLACK5XL$6.281006
22G80012BLACKL$2.32$2.0367178
23G80012BLACKM$2.32$2.0356924
24G80012BLACKS$2.32$2.0335935
25G80012BLACKXL$2.32$2.0328203
Table1


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"G80012", type text}, {"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}, {"Column8", type text}}),
    #"Demoted Headers" = Table.DemoteHeaders(#"Changed Type"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Demoted Headers",{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}, {"Column8", type text}, {"Column9", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Style", each if Text.StartsWith([Column1],"G") then [Column1] else null),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Style", "Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9"}),
    #"Filled Down" = Table.FillDown(#"Reordered Columns",{"Style"}),
    #"Removed Top Rows" = Table.Skip(#"Filled Down",1),
    #"Added Custom1" = Table.AddColumn(#"Removed Top Rows", "Color", each if Text.StartsWith([Column2], "S") then [Column1] else null),
    #"Filled Down1" = Table.FillDown(#"Added Custom1",{"Color"}),
    #"Reordered Columns1" = Table.ReorderColumns(#"Filled Down1",{"Style", "Color", "Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Reordered Columns1", [PromoteAllScalars=true]),
    #"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers",{{"G80012", type text}, {"ASHGREY", type text}, {"ASHGREY_1", type text}, {"S", type text}, {"M", type text}, {"L", type text}, {"XL", type text}, {"2XL", type text}, {"3XL", type text}, {"4XL", type text}, {"5XL", type text}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type2", each ([S] <> "S")),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"ASHGREY_1"}, {{"Data", each _, type table [G80012=nullable text, ASHGREY=nullable text, ASHGREY_1=nullable text, S=nullable text, M=nullable text, L=nullable text, XL=nullable text, 2XL=nullable text, 3XL=nullable text, 4XL=nullable text, 5XL=nullable text]}}),
    #"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "Data", {"G80012", "ASHGREY", "S", "M", "L", "XL", "2XL", "3XL", "4XL", "5XL"}, {"Data.G80012", "Data.ASHGREY", "Data.S", "Data.M", "Data.L", "Data.XL", "Data.2XL", "Data.3XL", "Data.4XL", "Data.5XL"}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Expanded Data", {"ASHGREY_1", "Data.G80012", "Data.ASHGREY"}, "Attribute", "Value"),
    #"Pivoted Column" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[ASHGREY_1]), "ASHGREY_1", "Value"),
    #"Replaced Value" = Table.ReplaceValue(#"Pivoted Column","Data.","",Replacer.ReplaceText,{"Attribute"})
in
    #"Replaced Value"
 
Upvote 0
With your Highlighted Table, here is the Mcode for Power Query
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"G800", type text}, {"G801", type any}, {"G802", type any}, {"G803", type any}, {"G804", type any}, {"G805", type any}, {"G806", type any}, {"G807", type any}, {"G808", type any}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if[G801]= "S" then[G800] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([G801] <> "S")),
    #"Reordered Columns" = Table.ReorderColumns(#"Filtered Rows",{"Custom", "G800", "G801", "G802", "G803", "G804", "G805", "G806", "G807", "G808"}),
    #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Custom", "Color"}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Renamed Columns", {"Color", "G800"}, "Attribute", "Value"),
    #"Pivoted Column" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[G800]), "G800", "Value", List.Sum)
in
    #"Pivoted Column"

OOPs! forgot to bring in sizes. Will rerun and post again.

Book3
ABCDE
1ColorAttributeRegularPriceSpecialPriceTotal-Inventory
2ASHGREYG8012.241.9227322
3ASHGREYG8022.241.924868
4ASHGREYG8032.241.9216127
5ASHGREYG8042.241.922190
6ASHGREYG8054.4378
7ASHGREYG8066.030
8ASHGREYG8075.9810
9ASHGREYG8085.9911
10AZALEAG8012.322.031646
11AZALEAG8022.322.032000
12AZALEAG8032.322.031355
13AZALEAG8042.322.03436
14AZALEAG8054.91427
15AZALEAG8066.3470
16AZALEAG8076.280
17AZALEAG8086.280
18BLACKG8012.322.0335935
19BLACKG8022.322.0356924
20BLACKG8032.322.0367178
21BLACKG8042.322.0328203
22BLACKG8054.9110723
23BLACKG8066.345075
24BLACKG8076.28144
25BLACKG8086.281006
26CAROLINABLUEG8012.322.0327833
27CAROLINABLUEG8022.322.035947
28CAROLINABLUEG8032.322.033580
29CAROLINABLUEG8042.322.034363
30CAROLINABLUEG8054.914179
31CAROLINABLUEG8066.34556
32CAROLINABLUEG8076.280
33CAROLINABLUEG8086.280
34DAISYG8012.3227886
35DAISYG8022.3210266
36DAISYG8032.324470
37DAISYG8042.329997
38DAISYG8054.911126
39DAISYG8066.342584
40DAISYG8076.289
41DAISYG8086.280
42DARKCHOCOLATEG8012.562.030
43DARKCHOCOLATEG8022.560.750
44DARKCHOCOLATEG8032.562.0351
45DARKCHOCOLATEG8042.562.030
46DARKCHOCOLATEG8054.460
47DARKCHOCOLATEG8065.990
48DARKCHOCOLATEG8075.990
49DARKCHOCOLATEG8085.990
50DARKHEATHERG8012.322.0322425
51DARKHEATHERG8022.322.039243
52DARKHEATHERG8032.322.0313362
53DARKHEATHERG8042.322.033286
54DARKHEATHERG8054.912633
55DARKHEATHERG8066.340
56DARKHEATHERG8076.280
57DARKHEATHERG8086.2831
58ELECTRICGREENG8012.322.0320806
59ELECTRICGREENG8022.322.0321523
60ELECTRICGREENG8032.322.0319842
61ELECTRICGREENG8042.322.0312098
62ELECTRICGREENG8054.913409
63ELECTRICGREENG8066.34796
64ELECTRICGREENG8076.28313
65ELECTRICGREENG8086.280
Table1 (2)
 
Upvote 0
Also, sorry for the multiple responses - no edit button that I can see :(
That is an anti-spam measure for new members. Once you have made a few more posts you will have established yourself as a legitimate member and have a 10-minute window after posting to edit. :)

Some colors listed do not include the SpecialPrice row in the data set.
That is certainly why my code errored as your original sample data all had 4 rows per section I assumed that always the case.
Try this version.
VBA Code:
Sub Rearrange_v2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, r As Long, uba1 As Long, uba2 As Long, Col As Long
  Dim sStyle As String, sColor As String
  
  a = Range("A1").CurrentRegion.Value
  uba1 = UBound(a, 1)
  uba2 = UBound(a, 2)
  ReDim b(1 To (uba2 - 1) * (uba1 - 1), 1 To 6)
  sStyle = a(1, 1)
  i = 2
  k = 0
  Do
    Do
      Select Case a(i, 1)
        Case "RegularPrice": Col = 4
        Case "SpecialPrice": Col = 5
        Case "Total-Inventory": Col = 6
        Case Else
          Col = 2
          r = k
          sColor = a(i, 1)
          For j = 2 To uba2
            b(r + j - 1, 1) = sStyle
            b(r + j - 1, 2) = sColor
            b(r + j - 1, 3) = a(i, j)
          Next j
      End Select
      If Col > 2 Then
        r = k
        For j = 2 To uba2
          b(r + j - 1, Col) = a(i, j)
        Next j
      End If
      i = i + 1
    Loop Until Col = 6
    k = k + uba2 - 1
  Loop Until i > uba1
  Cells(1, uba2 + 2).Resize(, 6).Value = Split("Style|Color|Size|RegularPrice|SpecialPrice|Total-Inventory", "|")
  Cells(2, uba2 + 2).Resize(k, 6).Value = b
End Sub
 
Upvote 0
That is certainly why my code errored as your original sample data all had 4 rows per section I assumed that always the case.
Try this version.
Yeah, my apologies, I just took the top part of my data set for the example and completely spaced on that aspect. The updated version works perfectly.

While working on this today - I was able to manage to pull the data with the style number in its own column, instead of on top by itself (The style number changes much farther down the dataset). Would that require much editing to add to this script?

Before example:
AB Top 30 - 12-3-2020 UF - Copy.xlsx
ABCDEFGHIJ
1G800ASHGREYSMLXL2XL3XL4XL5XL
2G800RegularPrice$2.24$2.24$2.24$2.24$4.43$6.03$5.98$5.99
3G800SpecialPrice$1.92$1.92$1.92$1.92
4G800Total-Inventory2732248681612721907801011
5G800AZALEASMLXL2XL3XL4XL5XL
6G800RegularPrice$2.32$2.32$2.32$2.32$4.91$6.34$6.28$6.28
7G800SpecialPrice$2.03$2.03$2.03$2.03
8G800Total-Inventory1646200013554364277000
9G800BLACKSMLXL2XL3XL4XL5XL
10G800RegularPrice$2.32$2.32$2.32$2.32$4.91$6.34$6.28$6.28
11G800SpecialPrice$2.03$2.03$2.03$2.03
12G800Total-Inventory359355692467178282031072350751441006
13G500CAROLINABLUESMLXL2XL3XL4XL5XL
14G500RegularPrice$2.32$2.32$2.32$2.32$4.91$6.34$6.28$6.28
15G500SpecialPrice$2.03$2.03$2.03$2.03
16G500Total-Inventory27833594735804363417955600
17G500DAISYSMLXL2XL3XL4XL5XL
18G500RegularPrice$2.32$2.32$2.32$2.32$4.91$6.34$6.28$6.28
19G500SpecialPrice$2.03$2.03$2.03$2.03
20G500Total-Inventory2788610266447099971126258490
21G500DARKCHOCOLATESMLXL2XL3XL4XL5XL
22G500RegularPrice$2.56$2.56$2.56$2.56$4.46$5.99$5.99$5.99
23G500SpecialPrice$2.03$0.75$2.03$2.03
24G500Total-Inventory005100000
Sheet1


After example (same format):
AB Top 30 - 12-3-2020 UF - Copy.xlsx
LMNOPQ
1StyleColorSizeRegularPriceSpecialPriceTotal-Inventory
2G800ASHGREYS$2.24$1.9227322
3G800ASHGREYM$2.24$1.924868
4G800ASHGREYL$2.24$1.9216127
5G800ASHGREYXL$2.24$1.922190
6G800ASHGREY2XL$4.4378
7G800ASHGREY3XL$6.030
8G800ASHGREY4XL$5.9810
9G800ASHGREY5XL$5.9911
10G800ASHGREY
11G800AZALEAS$2.32$2.031646
12G800AZALEAM$2.32$2.032000
13G800AZALEAL$2.32$2.031355
14G800AZALEAXL$2.32$2.03436
15G800AZALEA2XL$4.91427
16G800AZALEA3XL$6.3470
17G800AZALEA4XL$6.280
18G800AZALEA5XL$6.280
19G800AZALEA
20G800BLACKS$2.32$2.0335935
21G800BLACKM$2.32$2.0356924
22G800BLACKL$2.32$2.0367178
23G800BLACKXL$2.32$2.0328203
24G800BLACK2XL$4.9110723
25G800BLACK3XL$6.345075
26G800BLACK4XL$6.28144
27G800BLACK5XL$6.281006
28G800BLACK
29G500CAROLINABLUES$2.32$2.0327833
30G500CAROLINABLUEM$2.32$2.035947
31G500CAROLINABLUEL$2.32$2.033580
32G500CAROLINABLUEXL$2.32$2.034363
33G500CAROLINABLUE2XL$4.914179
34G500CAROLINABLUE3XL$6.34556
35G500CAROLINABLUE4XL$6.280
36G500CAROLINABLUE5XL$6.280
37G500CAROLINABLUE
Sheet1




Note: This doesn't bother me at all, as it's pretty easy to eliminate, but there is a blank line after the sizes end (I assume it goes to a certain column when transposing, example on line 10), is it easy to not include those? Again, not a big deal, it's only a few clicks to remove those.


Thank you very much for your assistance in this. You are a magician!

Devin
 
Upvote 0
Would that require much editing to add to this script?
Try
VBA Code:
Sub Rearrange_v3()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, r As Long, uba1 As Long, uba2 As Long, Col As Long
  Dim sStyle As String, sColor As String
  
  a = Range("A1").CurrentRegion.Value
  uba1 = UBound(a, 1)
  uba2 = UBound(a, 2)
  ReDim b(1 To (uba2 - 1) * (uba1 - 1), 1 To 6)
  i = 1
  k = 0
  Do
    Do
      Select Case a(i, 2)
        Case "RegularPrice": Col = 4
        Case "SpecialPrice": Col = 5
        Case "Total-Inventory": Col = 6
        Case Else
          Col = 2
          r = k
          sStyle = a(i, 1)
          sColor = a(i, 2)
          For j = 3 To uba2
            b(r + j - 2, 1) = sStyle
            b(r + j - 2, 2) = sColor
            b(r + j - 2, 3) = a(i, j)
          Next j
      End Select
      If Col > 2 Then
        r = k
        For j = 3 To uba2
          b(r + j - 2, Col) = a(i, j)
        Next j
      End If
      i = i + 1
    Loop Until Col = 6
    k = k + uba2 - 2
  Loop Until i > uba1
  Cells(1, uba2 + 2).Resize(, 6).Value = Split("Style|Color|Size|RegularPrice|SpecialPrice|Total-Inventory", "|")
  Cells(2, uba2 + 2).Resize(k, 6).Value = b
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,880
Messages
6,175,157
Members
452,615
Latest member
bogeys2birdies

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