Setting up loop in Excel VBA to repeatedly sum up specific range -
i have spreadsheet i'm trying repeatedly populate column k specific data various similar points (80 cells down each iteration) in column e.
so k2 should example display total of e25 + e35 + e42 + e56 + e63.
then k3 should display total of e105 + e185 + e122 + e136 + e143.
i have written macro first step (and works), follows:
sub disctoptest() dim source range dim destination range dim total long set destination = range("k2") set source = range("e25") total = worksheetfunction.sum( _ source.value + _ source.offset(10, 0).value + _ source.offset(17, 0).value + _ source.offset(31, 0).value + _ source.offset(38, 0).value) destination.select destination.value = total set source = nothing set destination = nothing end sub
then inserted loop repeat operation entirety of database, whenever run added macro excel either freezes or refuses work. code i'm using loop:
sub disctop() dim source range dim destination range dim total long set destination = range("k2") set source = range("e25") until destination.offset(0, -1) = "" destination.select total = worksheetfunction.sum( _ source.value + _ source.offset(10, 0).value + _ source.offset(17, 0).value + _ source.offset(31, 0).value + _ source.offset(38, 0).value) destination.value = total source = source.offset(80, 0) destination = destination.offset(1, 0) loop range("a1").activate set source = nothing set destination = nothing end sub
in case might easier use different kind of loop, need repeat operation precisely 680 times in column k.
any tips , advice appreciated!
you need use set
on range variables make them change cell reference:
set source = source.offset(80, 0) set destination = destination.offset(1, 0)
also
destination.offset(0, -1) = ""
never return true sum function return 0 when 5 references empty.
so need test else like: total = 0
or can following formula:
=index(e:e,25+((row(1:1)-1)*80)) +index(e:e,35+((row(1:1)-1)*80)) +index(e:e,42+((row(1:1)-1)*80)) + index(e:e,56+((row(1:1)-1)*80)) +index(e:e,63+((row(1:1)-1)*80))
put in k2 , copy down.
Comments
Post a Comment