Excel VBA - How to find blank cell and sum from active cell up to blank cell -
i have following code find first blank cell , sum data below @ last blank cell.
dim r range dim lngrowstart long if range("a1").formula <> "" lngrowstart = 1 else lngrowstart = range("a1").end(xldown).row end if set r = cells(lngrowstart, 1).end(xldown).offset(1, 0) if left(r.offset(-1, 0).formula, 1) <> "=" r.formular1c1 = "=subtotal(9,r[-" & r.row - lngrowstart & "]c:r[-1]c)" end if but assumes data in column , first set of continuous data, how modify active cell sum above continuous data?
for example:
2 4 3 blank (sum above=9) 1 3 2 blank (sum above=6)
you can use udf below (explanation inside code's comments):
function sumcontinrange(curcell range) double dim rngstart range, sumrng range if curcell <> "" ' find first empty cell using find function set rngstart = columns(curcell.column).find(what:="", after:=curcell, lookin:=xlvalues) else ' find first empty cell using find function set rngstart = columns(curcell.column).find(what:="", after:=curcell, lookin:=xlvalues, searchdirection:=xlprevious) end if ' set sum range set sumrng = range(rngstart.offset(-1, 0), rngstart.offset(-1, 0).end(xlup)) sumcontinrange = worksheetfunction.sum(sumrng) ' return value end function then, test passing activecell using sub below:
sub testfunc() if activecell.value <> "" activecell.end(xldown).offset(1) = sumcontinrange(activecell) else activecell = sumcontinrange(activecell) end if end sub
Comments
Post a Comment