Excel array formula to sum for criteria then sort -
looking excel array formula sum "weight" each "code" , sort "code"s in order of weight.
| | b | c | d | e | |1 | code | weight | | code |totweight| |2 | 444 | 30 | | 444 | 50 | |3 | 333 | 10 | | 222 | 40 | |4 | 222 | 10 | | 333 | 40 | |5 | 444 | 10 | | 555 | 20 | |6 | 444 | 10 | |7 | 222 | 30 | |8 | 333 | 20 | |9 | 555 | 20 | |10| 333 | 10 | cols , b inputs, d , e outputs.
while may possible convoluted , calculation-intensive array formula, intents , purposes trying accomplish database select statement aggregate sum, group , order clauses. like,
select code, weight (select cw.code, sum(cw.weight) weight tblcodeweights cw group cw.code) order weight desc, code; i suggest vba sub procedure utilizing adodb.connection worksheet can produce results trying achieve. following code verbose , trimmed down little every facet of operation handled , should self-explanatory or researchable.
option explicit sub sortedfilteredsums() dim cnx object, rs object dim sws1 string, swb string, scnx string, ssql string dim ws1tbladdr string worksheets("sheet8") ws1tbladdr = .cells(1, 1).currentregion.address(0, 0) sws1 = worksheets("sheet8").name end swb = thisworkbook.fullname scnx = "provider=microsoft.jet.oledb.4.0;data source=" & swb _ & ";extended properties=""excel 8.0;hdr=yes;imex=1"";" debug.print scnx set cnx = createobject("adodb.connection") set rs = createobject("adodb.recordset") cnx.open scnx ssql = "select [code], [weight] (" ssql = ssql & " select cw.[code], sum(cw.[weight]) [weight]" ssql = ssql & " [" & sws1 & "$" & ws1tbladdr & "] cw" ssql = ssql & " group cw.code" ssql = ssql & ") order [weight] desc, [code]" debug.print ssql 'select code, weight ' (select cw.code, sum(cw.weight) weight ' tblcodeweights cw ' group cw.code) 'order weight desc, code; rs.open ssql, cnx worksheets("sheet8") .range("d1").resize(1, 2) = array("code", "totweight") .range("d2").copyfromrecordset rs end rs.close: set rs = nothing cnx.close: set cnx = nothing end sub please note cannot performed on workbook has not been saved; e.g. not on new untitled workbook. results should similar following.
the test workbook used example temporarily available from:

Comments
Post a Comment