excel vba - Collect and subtotal duplicate rows in a Variant 2D array -
i have dataset in excel consisting of 20 columns , varying number of rows, ranging 20,000 - 50,000.
each row collection of items 1 column denoting quantity of items in collection , column denoting total combined weight of collection. of these rows identical in columns, while identical in quantity , weight.
i want create macro runs through dataset , "stacks" rows duplicate on other parameters quantity , weight, , sums these 2 up.
in other words, macro converts this:
|param1|param2|...|param18|quantity|weight| | | 1 |...| c | 5 | 12.5 | | | 1 |...| c | 2 | 5.0 | | | 1 |...| c | 3 | 7.5 | | b | 2 |...| c | 1 | 2.3 | | b | 2 |...| c | 2 | 4.6 |
to this:
|param1|param2|...|param18|quantity|weight| | | 1 |...| c | 10 | 25.0 | | b | 2 |...| c | 3 | 6.9 |
i know possible in simple pivot table, number of reasons not viable in case.
since i'm dealing large dataset, want load memory @ once rather reading , writing line line speed performance (as suggested in tip #13 in great article http://www.databison.com/how-to-speed-up-calculation-and-improve-performance-of-excel-and-vba/). i'm stuck how make row operations on data stored in memory.
so far code looks this:
dim r, c, lastrow integer dim temp_range variant lastrow = cells(65536, 2).end(xlup).row 'load data set memory temp_range = sheets("1.1").range(sheets("1.1").cells(2, 1), sheets("1.1").cells(lastrow, 20)).value 'run through data set bottom top , bulk identical rows r = ubound(temp_range) lbound(temp_range) = r - 1 lbound(temp_range) 'pseudo code start here if row temp_range(r) = row temp_range(i) temp_range(i,19) = temp_range(r,19) + temp_range(i,19) temp_range(i,20) = temp_range(r,19) + temp_range(i,20) delete row temp_range(r) exit end if 'pseudo code end here next next r
i'm stuck @ pseudo-code section highligted in code. don't know how compare rows, copy quantity , weight 1 row , delete duplicate row in variant holding range in memory.
use microsoft query (sql) in excel:
such data manipulation operations ideal sql queries. no need of going through data line line using vba:
select s1.param1, s1.param2, s1.param18, sum(s1.quantity), sum(s1.weight) [sheet1$] s1 group param1, param2,param18
this guaranteed run , efficiently via ole db. whatever vba code less efficient.
to refresh query @ time run following code vba:
set ws = activesheet ws.querytables(1).refresh backgroundquery:=false
where ws worksheet locate query table.
either use microsoft query data->from other sources->from microsoft query or feel free use add-in: http://www.analystcave.com/excel-tools/excel-sql-add-in-free/
Comments
Post a Comment