transpose data for each sheet based on multiple prices

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
531
Office Version
  1. 2019
Hello
I have two sheets contains BRANDS in column C and many duplicates brands contain different prices in column E . so result should show from column I ( CODE,BRAND , PRICE)
the PRICE in header should increment as the brand needs when contains multiple prices.
transfer.xlsm
ABCDEFGHIJKLM
3ITEMCODEBRANDQTYUNIT PRICETOTAL
411241BS 1200R20 G580 JAP220.002,035.000447,700.00
521244BS 1200R20 R187 JAP4.002,000.0008,000.00
631269BS 1200R24 G580 JAP4.001,900.0007,600.00
741556BS 185R14C R660 TR12.00423.0005,076.00
851221BS 205/70R15C R623 THI72.00405.00029,160.00
961547BS 205R16C D840 THI8.00625.0005,000.00
1071502BS 215/65R16C R611 THI4.00600.0002,400.00
1181502BS 215/65R16C R611 THI8.00583.0004,664.00
1291227BS 215/70R15C R623 THI22.00425.0009,350.00
13101227BS 215/70R15C R623 THI28.00544.00015,232.00
14111503BS 225/85R16C R202 JAP6.00975.0005,850.00
15121310BS 225/95R16C D618 JAP40.00515.00020,600.00
16131310BS 225/95R16C D618 JAP50.00695.00034,750.00
17141310BS 225/95R16C D618 JAP38.00715.00027,170.00
18151310BS 225/95R16C D618 JAP58.00700.00040,600.00
19161402BS 245/70R16 D697 JAP4.00590.0002,360.00
20171346BS 255/70R15C D84024.00505.00012,120.00
21181346BS 255/70R15C D84017.00635.00010,795.00
22191326BS 265/60R18 D840 JAP36.00721.00025,956.00
23201534BS 265/65R17 D693 THI 36.00745.00026,820.00
24211391BS 265/65R17 D840 JAP5.00690.0003,450.00
25221391BS 265/65R17 D840 JAP50.00535.00026,750.00
26231411BS 275/55R20 ALENZA1 JAP4.00725.0002,900.00
27241411BS 275/55R20 ALENZA1 JAP4.00942.0003,768.00
28251190BS 285/50R20 DSPORT JAP1.00705.000705.00
29261190BS 285/50R20 DSPORT JAP8.00936.0007,488.00
30271315BS 315/80R22.5 G580 JAP 12.002,470.00029,640.00
31281257BS 315/80R22.5 R184 JAP37.002,015.00074,555.00
32291401BS 650R16 R230 JAP5.00570.0002,850.00
33301305BS 700R16 R230 JAP16.00762.00012,192.00
34311306BS 750R16 R230 JAP80.00715.00057,200.00
35321306BS 750R16 R230 JAP120.00940.000112,800.00
36331307BS 750R16 VSJ JAP5.00910.0004,550.00
37341284GC 1200R20 AZ0026 CHI80.00895.00071,600.00
38351284GC 1200R20 AZ0026 CHI324.001,125.000364,500.00
39361285GC 1200R20 AZ0183 CHI40.00925.00037,000.00
40371285GC 1200R20 AZ0183 CHI10.001,225.00012,250.00
41381285GC 1200R20 AZ0183 CHI140.001,205.000168,700.00
42391292GC 1200R24 AZ166 CHI24.00935.00022,440.00
43401385GC 315/80R22.5 AT161 CHI20.00735.00014,700.00
44411385GC 315/80R22.5 AT161 CHI10.00955.0009,550.00
45421287GC 315/80R22.5 AZ126 CHI60.00735.00044,100.00
46431294GC 315/80R22.5 AZ188 CHI20.00745.00014,900.00
47441294GC 315/80R22.5 AZ188 CHI24.00965.00023,160.00
48451492GC 385/65R22.5 AT131 CHI14.001,275.00017,850.00
49461528TH 185/65R14 H-93 CHI4.00134.000536.00
50471493WL 195/65R15 Z-108 CHI4.00173.000692.00
51481486WL 205/55R16 Z-108 CHI2.00185.000370.00
52TOTAL1,814.001,882,399.00
PURCHASING
Cell Formulas
RangeFormula
D52,F52D52=SUM(D4:D51)
F4:F51F4=D4*E4



.
transfer.xlsm
ABCDEF
3ITEMCODEBRANDQTYUNIT PRICETOTAL
411306BS 750R16 R230 JAP10.00775.0007,750.000
521306BS 750R16 R230 JAP4.00780.0003,120.000
631305BS 700R16 R230 JAP2.00770.0001,540.000
7TOTAL16.0012,410.00
SELLING
Cell Formulas
RangeFormula
D7,F7D7=SUM(D4:D6)
F4:F6F4=D4*E4



result

transfer.xlsm
ABCDEFGHIJKLMN
1CODEBRANDUNIT PRICE1UNIT PRICE2UNIT PRICE3UNIT PRICE4
21241BS 1200R20 G580 JAP2,035.000---
3ITEMCODEBRANDQTYUNIT PRICETOTAL1244BS 1200R20 R187 JAP2,000.000---
411241BS 1200R20 G580 JAP220.002,035.000447,700.001269BS 1200R24 G580 JAP1,900.000---
521244BS 1200R20 R187 JAP4.002,000.0008,000.001556BS 185R14C R660 TR423.000---
631269BS 1200R24 G580 JAP4.001,900.0007,600.001221BS 205/70R15C R623 THI405.000---
741556BS 185R14C R660 TR12.00423.0005,076.001547BS 205R16C D840 THI625.000---
851221BS 205/70R15C R623 THI72.00405.00029,160.001502BS 215/65R16C R611 THI600.000583.000--
961547BS 205R16C D840 THI8.00625.0005,000.001227BS 215/70R15C R623 THI425.000544.000--
1071502BS 215/65R16C R611 THI4.00600.0002,400.001503BS 225/85R16C R202 JAP975.000---
1181502BS 215/65R16C R611 THI8.00583.0004,664.001310BS 225/95R16C D618 JAP515.000695715700
1291227BS 215/70R15C R623 THI22.00425.0009,350.001402BS 245/70R16 D697 JAP590.000---
13101227BS 215/70R15C R623 THI28.00544.00015,232.001346BS 255/70R15C D840505.000635.000--
14111503BS 225/85R16C R202 JAP6.00975.0005,850.001326BS 265/60R18 D840 JAP721.000---
15121310BS 225/95R16C D618 JAP40.00515.00020,600.001534BS 265/65R17 D693 THI 745.000---
16131310BS 225/95R16C D618 JAP50.00695.00034,750.001391BS 265/65R17 D840 JAP535.000690.000--
17141310BS 225/95R16C D618 JAP38.00715.00027,170.001411BS 275/55R20 ALENZA1 JAP725.000942.000--
18151310BS 225/95R16C D618 JAP58.00700.00040,600.001190BS 285/50R20 DSPORT JAP936.000705.000--
19161402BS 245/70R16 D697 JAP4.00590.0002,360.001315BS 315/80R22.5 G580 JAP 2,470.000---
20171346BS 255/70R15C D84024.00505.00012,120.001257BS 315/80R22.5 R184 JAP2,015.000---
21181346BS 255/70R15C D84017.00635.00010,795.001401BS 650R16 R230 JAP570.000---
22191326BS 265/60R18 D840 JAP36.00721.00025,956.001305BS 700R16 R230 JAP762.000---
23201534BS 265/65R17 D693 THI 36.00745.00026,820.001306BS 750R16 R230 JAP715.000940.000--
24211391BS 265/65R17 D840 JAP5.00690.0003,450.001307BS 750R16 VSJ JAP910.000---
25221391BS 265/65R17 D840 JAP50.00535.00026,750.001284GC 1200R20 AZ0026 CHI895.0001,125.000--
26231411BS 275/55R20 ALENZA1 JAP4.00725.0002,900.001285GC 1200R20 AZ0183 CHI925.0001,225.0001,205.000-
27241411BS 275/55R20 ALENZA1 JAP4.00942.0003,768.001292GC 1200R24 AZ166 CHI935.000---
28251190BS 285/50R20 DSPORT JAP1.00705.000705.001385GC 315/80R22.5 AT161 CHI735.000955.000--
29261190BS 285/50R20 DSPORT JAP8.00936.0007,488.001287GC 315/80R22.5 AZ126 CHI735.000---
30271315BS 315/80R22.5 G580 JAP 12.002,470.00029,640.001294GC 315/80R22.5 AZ188 CHI745.000965.000--
31281257BS 315/80R22.5 R184 JAP37.002,015.00074,555.001492GC 385/65R22.5 AT131 CHI1,275.000---
32291401BS 650R16 R230 JAP5.00570.0002,850.001528TH 185/65R14 H-93 CHI134.000---
33301305BS 700R16 R230 JAP16.00762.00012,192.001493WL 195/65R15 Z-108 CHI173.000---
34311306BS 750R16 R230 JAP80.00715.00057,200.001486WL 205/55R16 Z-108 CHI185.000---
35321306BS 750R16 R230 JAP120.00940.000112,800.00
36331307BS 750R16 VSJ JAP5.00910.0004,550.00
37341284GC 1200R20 AZ0026 CHI80.00895.00071,600.00
38351284GC 1200R20 AZ0026 CHI324.001,125.000364,500.00
39361285GC 1200R20 AZ0183 CHI40.00925.00037,000.00
40371285GC 1200R20 AZ0183 CHI10.001,225.00012,250.00
41381285GC 1200R20 AZ0183 CHI140.001,205.000168,700.00
42391292GC 1200R24 AZ166 CHI24.00935.00022,440.00
43401385GC 315/80R22.5 AT161 CHI20.00735.00014,700.00
44411385GC 315/80R22.5 AT161 CHI10.00955.0009,550.00
45421287GC 315/80R22.5 AZ126 CHI60.00735.00044,100.00
46431294GC 315/80R22.5 AZ188 CHI20.00745.00014,900.00
47441294GC 315/80R22.5 AZ188 CHI24.00965.00023,160.00
48451492GC 385/65R22.5 AT131 CHI14.001,275.00017,850.00
49461528TH 185/65R14 H-93 CHI4.00134.000536.00
50471493WL 195/65R15 Z-108 CHI4.00173.000692.00
51481486WL 205/55R16 Z-108 CHI2.00185.000370.00
52TOTAL1,814.001,882,399.00
PURCHASING
Cell Formulas
RangeFormula
D52,F52D52=SUM(D4:D51)
F4:F51F4=D4*E4




transfer.xlsm
ABCDEFGHIJKL
1CODEBRANDUNIT PRICE1UNIT PRICE2
21305BS 700R16 R230 JAP770.000-
3ITEMCODEBRANDQTYUNIT PRICETOTAL1306BS 750R16 R230 JAP775.000780.000
411306BS 750R16 R230 JAP10.00775.0007,750.000
521306BS 750R16 R230 JAP4.00780.0003,120.000
631305BS 700R16 R230 JAP2.00770.0001,540.000
7TOTAL16.0012,410.00
8
SELLING
Cell Formulas
RangeFormula
D7,F7D7=SUM(D4:D6)
F4:F6F4=D4*E4

data in each sheet could be 5000 rows
the match for each brand is choice for column B or C .
thanks
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
With Power Query
Book4
ABCDEFGHIJKLM
1ITEMCODEBRANDQTYUNIT PRICETOTALCODEBRANDUnitPrice1UnitPrice2UnitPrice3UnitPrice4
211241BS 1200R20 G580 JAP220203576001190BS 285/50R20 DSPORT JAP705936
321244BS 1200R20 R187 JAP4200050761221BS 205/70R15C R623 THI405
431269BS 1200R24 G580 JAP41900291601227BS 215/70R15C R623 THI425544
541556BS 185R14C R660 TR1242350001241BS 1200R20 G580 JAP2035
651221BS 205/70R15C R623 THI7240524001244BS 1200R20 R187 JAP2000
761547BS 205R16C D840 THI862546641257BS 315/80R22.5 R184 JAP2015
871502BS 215/65R16C R611 THI460093501269BS 1200R24 G580 JAP1900
981502BS 215/65R16C R611 THI8583152321284GC 1200R20 AZ0026 CHI8951125
1091227BS 215/70R15C R623 THI2242558501285GC 1200R20 AZ0183 CHI92512251205
11101227BS 215/70R15C R623 THI28544206001287GC 315/80R22.5 AZ126 CHI735
12111503BS 225/85R16C R202 JAP6975347501292GC 1200R24 AZ166 CHI935
13121310BS 225/95R16C D618 JAP40515271701294GC 315/80R22.5 AZ188 CHI745965
14131310BS 225/95R16C D618 JAP50695406001305BS 700R16 R230 JAP762
15141310BS 225/95R16C D618 JAP3871523601306BS 750R16 R230 JAP715940
16151310BS 225/95R16C D618 JAP58700121201307BS 750R16 VSJ JAP910
17161402BS 245/70R16 D697 JAP4590107951310BS 225/95R16C D618 JAP515695715700
18171346BS 255/70R15C D84024505259561315BS 315/80R22.5 G580 JAP 2470
19181346BS 255/70R15C D84017635268201326BS 265/60R18 D840 JAP721
20191326BS 265/60R18 D840 JAP3672134501346BS 255/70R15C D840505635
21201534BS 265/65R17 D693 THI 36745267501385GC 315/80R22.5 AT161 CHI735955
22211391BS 265/65R17 D840 JAP569029001391BS 265/65R17 D840 JAP690535
23221391BS 265/65R17 D840 JAP5053537681401BS 650R16 R230 JAP570
24231411BS 275/55R20 ALENZA1 JAP47257051402BS 245/70R16 D697 JAP590
25241411BS 275/55R20 ALENZA1 JAP494274881411BS 275/55R20 ALENZA1 JAP725942
26251190BS 285/50R20 DSPORT JAP1705296401486WL 205/55R16 Z-108 CHI185
27261190BS 285/50R20 DSPORT JAP8936745551492GC 385/65R22.5 AT131 CHI1275
28271315BS 315/80R22.5 G580 JAP 12247028501493WL 195/65R15 Z-108 CHI173
29281257BS 315/80R22.5 R184 JAP372015121921502BS 215/65R16C R611 THI600583
30291401BS 650R16 R230 JAP5570572001503BS 225/85R16C R202 JAP975
31301305BS 700R16 R230 JAP167621128001528TH 185/65R14 H-93 CHI134
32311306BS 750R16 R230 JAP8071545501534BS 265/65R17 D693 THI 745
33321306BS 750R16 R230 JAP120940716001547BS 205R16C D840 THI625
34331307BS 750R16 VSJ JAP59103645001556BS 185R14C R660 TR423
35341284GC 1200R20 AZ0026 CHI8089537000
36351284GC 1200R20 AZ0026 CHI324112512250
37361285GC 1200R20 AZ0183 CHI40925168700
38371285GC 1200R20 AZ0183 CHI10122522440
39381285GC 1200R20 AZ0183 CHI140120514700
40391292GC 1200R24 AZ166 CHI249359550
41401385GC 315/80R22.5 AT161 CHI2073544100
42411385GC 315/80R22.5 AT161 CHI1095514900
43421287GC 315/80R22.5 AZ126 CHI6073523160
44431294GC 315/80R22.5 AZ188 CHI2074517850
45441294GC 315/80R22.5 AZ188 CHI24965536
46451492GC 385/65R22.5 AT131 CHI141275692
47461528TH 185/65R14 H-93 CHI4134370
48471493WL 195/65R15 Z-108 CHI41730
49481486WL 205/55R16 Z-108 CHI21850
50TOTAL15901414023
Sheet1
Cell Formulas
RangeFormula
D50,F50D50=SUM(D4:D51)
F2:F49F2=D4*E4


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"CODE"}, {{"All", each _, type table [ITEM=number, CODE=number, BRAND=text, QTY=number, UNIT PRICE=number, TOTAL=number]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([All],"Index",1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"All", "CODE"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"ITEM", "CODE", "BRAND", "QTY", "UNIT PRICE", "TOTAL", "Index"}, {"ITEM", "CODE", "BRAND", "QTY", "UNIT PRICE", "TOTAL", "Index"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded Custom",{"CODE", "BRAND", "UNIT PRICE", "Index"}),
    #"Added Prefix" = Table.TransformColumns(#"Removed Other Columns", {{"Index", each "UnitPrice" & Text.From(_, "en-US"), type text}}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Added Prefix", {{"Index", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Added Prefix", {{"Index", type text}}, "en-US")[Index]), "Index", "UNIT PRICE")
in
    #"Pivoted Column"
 
Upvote 0
Please note: In the above, I just noticed that the sort is not correct for the order of pricing for second, third, etc. Still working on that.
 
Upvote 0
Try this:

VBA Code:
Sub transpose_data()
  Dim ary As Variant, ar As Variant
  Dim a() As Variant, b() As Variant, c() As Variant
  Dim dic As Object, ky As Variant
  Dim i As Long, j As Long, k As Long, n As Long
  Dim nRow As Long, nCol As Long, fila As Long, nMax As Long
  
  Application.ScreenUpdating = False
  
  ary = Array("PURCHASING", "SELLING")
  Set dic = CreateObject("Scripting.Dictionary")
  
  For Each ar In ary
    dic.RemoveAll
    Erase a, b, c
    a = Sheets(ar).Range("A4:E" & Sheets(ar).Range("B" & Rows.Count).End(3).Row).Value
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 1) + 2)
    nMax = 0
    
    For i = 1 To UBound(a)
      If Not dic.exists(a(i, 2)) Then
        dic(a(i, 2)) = dic.Count + 1 & "|" & 1
      End If
      
      nRow = Split(dic(a(i, 2)), "|")(0)
      nCol = Split(dic(a(i, 2)), "|")(1)
      b(nRow, nCol) = i
      dic(a(i, 2)) = nRow & "|" & nCol + 1
    Next

    ReDim c(1 To dic.Count, 1 To dic.Count + 2)
    k = 0
    For Each ky In dic.keys
      nRow = Split(dic(ky), "|")(0)
      nCol = Split(dic(ky), "|")(1)
      If nCol > nMax Then nMax = nCol
      n = 2
      k = k + 1
      For j = 1 To nCol - 1
        fila = b(nRow, j)
        n = n + 1
        c(k, 1) = a(fila, 2)
        c(k, 2) = a(fila, 3)
        c(k, n) = a(fila, 5)
      Next
    Next
    With Sheets(ar)
      .Range("I2").Resize(UBound(c, 1), UBound(c, 2)).Value = c
      .Range("I1", .Cells(UBound(c, 1) + 1, nMax - 1)).Sort .Range("I2"), xlAscending, Header:=xlYes
      .Range("B3:C3").Copy .Range("I1")
      k = 11
      For j = 1 To nMax - 1
        .Cells(1, k).Value = "UNIT PRICE" & j
        k = k + 1
      Next
      .Range("B3").Copy
      .Range("I1", .Cells(1, 9 + nMax)).PasteSpecial xlFormats
      .Range("B4").Copy
      .Range("I2:J" & UBound(c, 1) + 1).PasteSpecial xlFormats
      .Range("F4").Copy
      .Range("K2", .Cells(UBound(c, 1) + 1, 10 + nMax - 1)).PasteSpecial xlFormats
    End With
  Next
  
  Application.ScreenUpdating = False

End Sub
 
Upvote 0
Hi Alan,
that's great , but you use CODE column to sort !
and may you fix cells by show number formatting " #,##0.00" also show zero as hyphen as I did in OP?
 
Upvote 0
Hi Dante
it works ,but there are problems !
see picture1 what shows when run from first time !

p1.JPG


see picture2 what shows when run again !

p2.JPG



as you see there are some errors in formatting ,also I would show number formatting " #,##0.00" also show zero as hyphen in empty cells as I did in OP if you have time.
thanks.
 
Upvote 0
Within Power Query, you cannot have both Currency ($) and Text (-) in the same column. That type of format needs to be done in Native Excel. If you have currency then the cells that are null need to be either blank or zero. If this is critical to your report then do your formatting in Excel.

I don't understand what you want with this statement but you use CODE column to sort !
 
Upvote 0
it works ,but there are problems !

Try:
VBA Code:
Sub transpose_data()
  Dim ary As Variant, ar As Variant
  Dim a() As Variant, b() As Variant, c() As Variant
  Dim dic As Object, ky As Variant
  Dim i As Long, j As Long, k As Long, n As Long
  Dim nRow As Long, nCol As Long, fila As Long, nMax As Long
  
  Application.ScreenUpdating = False
  
  ary = Array("PURCHASING", "SELLING")
  Set dic = CreateObject("Scripting.Dictionary")
  
  For Each ar In ary
    dic.RemoveAll
    Erase a, b, c
    a = Sheets(ar).Range("A4:E" & Sheets(ar).Range("B" & Rows.Count).End(3).Row).Value
    ReDim b(1 To UBound(a, 1), 1 To UBound(a, 1) + 2)
    nMax = 0
    
    For i = 1 To UBound(a)
      If Not dic.exists(a(i, 2)) Then
        dic(a(i, 2)) = dic.Count + 1 & "|" & 1
      End If
      
      nRow = Split(dic(a(i, 2)), "|")(0)
      nCol = Split(dic(a(i, 2)), "|")(1)
      b(nRow, nCol) = i
      dic(a(i, 2)) = nRow & "|" & nCol + 1
    Next

    ReDim c(1 To dic.Count, 1 To dic.Count + 2)
    k = 0
    For Each ky In dic.keys
      nRow = Split(dic(ky), "|")(0)
      nCol = Split(dic(ky), "|")(1)
      If nCol > nMax Then nMax = nCol
      n = 2
      k = k + 1
      For j = 1 To nCol - 1
        fila = b(nRow, j)
        n = n + 1
        c(k, 1) = a(fila, 2)
        c(k, 2) = a(fila, 3)
        c(k, n) = a(fila, 5)
      Next
    Next
    With Sheets(ar)
      .Range("I1", .Cells(1, Columns.Count)).EntireColumn.Clear
      .Range("I2").Resize(UBound(c, 1), UBound(c, 2)).Value = c
      .Range("B3:C3").Copy .Range("I1")
      .Range("I1", .Cells(UBound(c, 1) + 1, 9 + nMax)).Sort .Range("I2"), xlAscending, Header:=xlYes
      k = 11
      For j = 1 To nMax - 1
        .Cells(1, k).Value = "UNIT PRICE" & j
        k = k + 1
      Next
      .Range("B3").Copy
      .Range("I1", .Cells(1, 9 + nMax)).PasteSpecial xlFormats
      .Range("B4").Copy
      .Range("I2:J" & UBound(c, 1) + 1).PasteSpecial xlFormats
      .Range("F4").Copy
      .Range("K2", .Cells(UBound(c, 1) + 1, 10 + nMax - 1)).PasteSpecial xlFormats
      .Cells.EntireColumn.AutoFit
    End With
  Next
  
  Application.ScreenUpdating = False

End Sub

🤗
 
Upvote 0
thanks again Dante.:)
but I suspect the selling sheet doesn't accept more than two unit price for headers when add more prices for the same brands should show (unit price1,unit price2,unit price3) but will shows error subscript out of range in this line
VBA Code:
c(k, n) = a(fila, 5)
what's my bad?!!:confused:
 
Upvote 0
what's my bad?
It's actually a problem with my macro on some counters.
I have already made the modifications and tested with 5000 records in each sheet and up to 45 unit prices and the macro works immediately.

VBA Code:
Sub transpose_data()
  Dim ary As Variant, ar As Variant
  Dim a() As Variant, b() As Variant, c() As Variant
  Dim dic As Object, ky As Variant
  Dim i As Long, j As Long, k As Long, n As Long
  Dim nRow As Long, nCol As Long, fila As Long, nMax As Long
  
  Application.ScreenUpdating = False
  
  ary = Array("PURCHASING", "SELLING")
  Set dic = CreateObject("Scripting.Dictionary")
  
  For Each ar In ary
    dic.RemoveAll
    Erase a, b, c
    a = Sheets(ar).Range("A4:E" & Sheets(ar).Range("B" & Rows.Count).End(3).Row).Value
    nMax = 0
    
    For i = 1 To UBound(a)
      If Not dic.exists(a(i, 2)) Then
        dic(a(i, 2)) = dic.Count + 1 & "|" & 1
      End If
    
      nRow = Split(dic(a(i, 2)), "|")(0)
      nCol = Split(dic(a(i, 2)), "|")(1)
      dic(a(i, 2)) = nRow & "|" & nCol + 1
      If nCol > nMax Then nMax = nCol
    Next
    
    ReDim b(1 To dic.Count + 1, 1 To nMax + 1 + 2)
    dic.RemoveAll
    For i = 1 To UBound(a)
      If i = 53 Then
        i = i
      End If
      If Not dic.exists(a(i, 2)) Then
        dic(a(i, 2)) = dic.Count + 1 & "|" & 1
      End If
    
      nRow = Split(dic(a(i, 2)), "|")(0)
      nCol = Split(dic(a(i, 2)), "|")(1)
      b(nRow, nCol) = i
      dic(a(i, 2)) = nRow & "|" & nCol + 1
    Next

    ReDim c(1 To dic.Count, 1 To nMax + 2)
    i = dic.Count
    k = 0
    For Each ky In dic.keys
      nRow = Split(dic(ky), "|")(0)
      nCol = Split(dic(ky), "|")(1)
      
      n = 2
      k = k + 1
      For j = 1 To nCol - 1
        fila = b(nRow, j)
        n = n + 1
        c(k, 1) = a(fila, 2)
        c(k, 2) = a(fila, 3)
        c(k, n) = a(fila, 5)
      Next
    Next
    With Sheets(ar)
      .Range("I1", .Cells(1, Columns.Count)).EntireColumn.Clear
      .Range("I2").Resize(UBound(c, 1), UBound(c, 2)).Value = c
      .Range("B3:C3").Copy .Range("I1")
      .Range("I1", .Cells(UBound(c, 1) + 1, 10 + nMax)).Sort .Range("I2"), xlAscending, Header:=xlYes
      k = 11
      For j = 1 To nMax
        .Cells(1, k).Value = "UNIT PRICE" & j
        k = k + 1
      Next
      .Range("B3").Copy
      .Range("I1", .Cells(1, 10 + nMax)).PasteSpecial xlFormats
      .Range("B4").Copy
      .Range("I2:J" & UBound(c, 1) + 1).PasteSpecial xlFormats
      .Range("F4").Copy
      .Range("K2", .Cells(UBound(c, 1) + 1, 10 + nMax)).PasteSpecial xlFormats
      .Cells.EntireColumn.AutoFit
    End With
  Next
  Application.CutCopyMode = False
  Application.ScreenUpdating = False

End Sub


😇
 
Upvote 0
Solution

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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