Transpose sequence of values based on conditions

CrisRock87

New Member
Joined
Aug 13, 2016
Messages
2
Hello folks,

I am currently struggling to set up a macro to transpose arrays of records based on conditions.

I am quite new to VBA, so any tips and advice on how best to achieve this will be very much appreciated.

Below is the format of my 'raw' data in Excel (Version 16.0.6741.2056)

Column B contains DATETIME representing half hourly trading intervals in chronological order.
Each 'NAME' listed in column C might submit multiple offers/prices (up to 20) for each trading period.
The number of PRICE for each NAME varies each period.

ABCD
1DATETIMENAMEPRICE
21/07/2015 8:00:00 AMALPHAX
31/07/2015 8:00:00 AMALPHAX
41/07/2015 8:00:00 AMALPHAX
51/07/2015 8:00:00 AMBETAX
61/07/2015 8:00:00 AMGAMMAX
71/07/2015 8:00:00 AMGAMMAX
81/07/2015 8:00:00 AMDELTAX
91/07/2015 8:00:00 AMDELTAX
101/07/2015 8:00:00 AMDELTAX
111/07/2015 8:00:00 AMDELTAX
.......
231/07/2015 8:30:00 AMALPHAX
241/07/2015 8:30:00 AMBETAX
241/07/2015 8:30:00 AMBETAX
251/07/2015 8:30:00 AMGAMMAX
261/07/2015 8:30:00 AMDELTAX
271/07/2015 8:30:00 AMDELTAX
........

<tbody>
</tbody>
























And this is what I am attempting to achieve:

For each trading period, I'd like to have for each 'NAME' the set of offers/prices reported in columns.

I've provided an example below.


ABCDEFG..
1DATETIMENAMEPRICE 1PRICE 2PRICE 3PRICE 4..
21/07/2015 8:00:00 AMALPHAXXX
31/07/2015 8:00:00 AMBETAX
41/07/2015 8:00:00 AMGAMMAXX
51/07/2015 8:00:00 AMDELTAXXXX
......
121/07/2015 8:30:00 AMALPHAX
131/07/2015 8:30:00 AMBETAXX
141/07/2015 8:30:00 AMGAMMAX
151/07/2015 8:30:00 AMDELTAXX
......

<tbody>
</tbody>

















So far I have had no luck in achieving the above.

I am dealing with a massive amount of data (several years and over hundred different "NAMES")

Any suggestion on how to streamline this process is welcome.

Thanks.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
You don't need VBA, just remove duplicates or the advanced filter, then a formula:


Excel 2010
ABCDEFGHIJKL
1DATETIMENAMEPRICE
21/7/2015 8:00ALPHA70.451/7/2015 8:00ALPHA70.4550.4633.83
31/7/2015 8:00ALPHA50.461/7/2015 8:00BETA67.67
41/7/2015 8:00ALPHA33.831/7/2015 8:00GAMMA16.9120.41
51/7/2015 8:00BETA67.671/7/2015 8:00DELTA38.4666.0357.41.76
61/7/2015 8:00GAMMA16.911/7/2015 8:30ALPHA67.05
71/7/2015 8:00GAMMA20.411/7/2015 8:30BETA85.3344.51
81/7/2015 8:00DELTA38.461/7/2015 8:30GAMMA61.46
91/7/2015 8:00DELTA66.031/7/2015 8:30DELTA99.676.22
101/7/2015 8:00DELTA57.4
111/7/2015 8:00DELTA1.76
121/7/2015 8:30ALPHA67.05
131/7/2015 8:30BETA85.33
141/7/2015 8:30BETA44.51
151/7/2015 8:30GAMMA61.46
161/7/2015 8:30DELTA99.6
171/7/2015 8:30DELTA76.22
Sheet1
Cell Formulas
RangeFormula
I2{=IFERROR(INDEX($C$1:$C$17,SMALL(IF($A$2:$A$17=$G2,IF($B$2:$B$17=$H2,ROW($B$2:$B$17))),COLUMN(A1))),"")}
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Even better is to add a cumulative count column then set a pivot table:


Excel 2010
ABCD
1DATETIMENAMEPRICEcount
21/7/2015 8:00ALPHA70.451
31/7/2015 8:00ALPHA50.462
41/7/2015 8:00ALPHA33.833
51/7/2015 8:00BETA67.671
61/7/2015 8:00GAMMA16.911
71/7/2015 8:00GAMMA20.412
81/7/2015 8:00DELTA38.461
91/7/2015 8:00DELTA66.032
101/7/2015 8:00DELTA57.43
111/7/2015 8:00DELTA1.764
121/7/2015 8:30ALPHA67.051
131/7/2015 8:30BETA85.331
141/7/2015 8:30BETA44.512
151/7/2015 8:30GAMMA61.461
161/7/2015 8:30DELTA99.61
171/7/2015 8:30DELTA76.222
Sheet1 (2)
Cell Formulas
RangeFormula
D2=COUNTIFS($A$2:A2,A2,$B$2:B2,B2)


koA4Bcg.png
 
Upvote 0
Even better is to add a cumulative count column then set a pivot table:

Excel 2010
ABCD
1DATETIMENAMEPRICEcount
21/7/2015 8:00ALPHA70.451
31/7/2015 8:00ALPHA50.462
41/7/2015 8:00ALPHA33.833
51/7/2015 8:00BETA67.671
61/7/2015 8:00GAMMA16.911
71/7/2015 8:00GAMMA20.412
81/7/2015 8:00DELTA38.461
91/7/2015 8:00DELTA66.032
101/7/2015 8:00DELTA57.43
111/7/2015 8:00DELTA1.764
121/7/2015 8:30ALPHA67.051
131/7/2015 8:30BETA85.331
141/7/2015 8:30BETA44.512
151/7/2015 8:30GAMMA61.461
161/7/2015 8:30DELTA99.61
171/7/2015 8:30DELTA76.222

<tbody>
</tbody>
Sheet1 (2)

Worksheet Formulas
CellFormula
D2=COUNTIFS($A$2:A2,A2,$B$2:B2,B2)

<tbody>
</tbody>

<tbody>
</tbody>



koA4Bcg.png

Thank you very much Sheetspread,
You just saved me some trouble.
Both approaches work well and achieve exactly what I am looking for.
I was considering VBA because I am dealing with multiple csv and I have to perform this process on a regular basis.

C
 
Upvote 0
I was considering VBA because I am dealing with multiple csv and I have to perform this process on a regular basis.

The formula/table placement can be recorded in a macro and adjusted for dynamic ranges:

Code:
Sub pricepivtab()
Dim lr%
lr = Cells(Rows.Count, 1).End(xlUp).Row
Cells(1, 4).Value = "Count"
Cells(2, 4).Resize(lr - 1).Value = "=COUNTIFS($A$2:A2,A2,$B$2:B2,B2)"

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        Sheets("Data").Cells(1, 1).Resize(lr, 4), Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:=Sheets("Data").Cells(2, 6), TableName:="PivotTable2", _
        DefaultVersion:=xlPivotTableVersion14

    With ActiveSheet.PivotTables("PivotTable2")
    With .PivotFields("DATETIME")
        .Orientation = xlRowField
        .Position = 1
    End With
    With .PivotFields("NAME")
        .Orientation = xlRowField
        .Position = 2
    End With
    With .PivotFields("Count")
    .Orientation = xlColumnField
        .Position = 1
    End With
    .AddDataField .PivotFields("PRICE"), "Sum of PRICE", xlSum
        .ColumnGrand = False
        .RowGrand = False
      .RowAxisLayout xlTabularRow
      .PivotFields("DATETIME").Subtotals(1) = False
    .RepeatAllLabels xlRepeatLabels
    End With
End Sub

(I changed the sheet name to Data from Sheet1 (2) )
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,385
Members
448,956
Latest member
JPav

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