arrays - Populating a 2 dimensional Range in excel using sum ifs function in VBA -
data in sheet profit based on store , location. looking have matrix location in 1 column , stores in row. each location , store return 1 value sum of profit stores (there maybe multiple stores in particular location) in location.
this excel function:
=sumifs(sheet2!$c$2:$c$6,sheet2!$a$2:$a$6,sheet1!$a5,sheet2!$b$2:$b$6,sheet1!b$4)
try like,
with sheets("sheet1") .range("b5:z99") .formula = "=sumifs(sheet2!$c$2:$c$6, sheet2!$a$2:$a$6, sheet1!$a5, sheet2!$b$2:$b$6, sheet1!b$4)" .value = .value end end
essentially, write formula block of cells revert formulas' results values.
edit dynamic last row:
dim lr long lr = sheets("sheet2").cells(rows.count, 3).end(xlup).row
then formula assignment be:
.formula = "=sumifs(sheet2!$c$2:$c$" & lr & ", sheet2!$a$2:$a$" & lr & ", sheet1!$a5, sheet2!$b$2:$b$" & lr & ", sheet1!b$4)"
edit: named ranges:
if move formula utilizing named ranges define extents of data being examined, use same match formula define limits of different columns.
example: in above sumifs, use named ranges sheet2's columns a, b , c. let's call named ranges ws2cola, ws2colb , ws2colc. know summing columns c use row of last number in column c define ranges each of columns.
ws2cola - applies to: =sheet2!$a$2:index(sheet2!$a:$a, match(1e99, sheet2!$c:$c)) ws2colb - applies to: =sheet2!$b$2:index(sheet2!$b:$b, match(1e99, sheet2!$c:$c)) ws2colc - applies to: =sheet2!$c$2:index(sheet2!$c:$c, match(1e99, sheet2!$c:$c))
the formula assignment becomes:
.formula = "=sumifs(ws2colc, ws2cola, sheet1!$a5, ws2colb, sheet1!b$4)"
using same formula define extents of each named range column prevent mismatched range references in sumifs.
Comments
Post a Comment