excel - Applying a different formula every nth row -
so have sheet i'd apply formula every 7th row. can't same formula, needs "offset" formula well.
for example, first range formula "=(c4+c5)-c3"; second range, "=(c11+c12) - c10"; , on.
this have far:
sub applyformula() applycf range("c6") applycf range("c13") 'and on, every 7 rows 'is there other way can apply these ranges instead of typing them? 'with offset formula or that. end sub sub applycf(rng range) rng.formula = "=(c4+c5)-c3" 'i'd macro "offset" formula, 'so c13 range "=(c11+c12) - c10" end sub
for applycf
sub, this:
sub applycf(rng range) if rng.count <> 1 exit sub ' in case range more 1 cell dim prevcell range, twoprevcell range, threeprecell range set prevcell = rng.offset(-1, 0) set twoprevcell = rng.offset(-2, 0) set threeprevcell = rng.offset(-3, 0) rng.formula = "=(" & twoprevcell & "+" & prevcell & ")-" & threeprevcell end sub
it tweaked, instance, need see formula in formula bar? can evaluate math in vba , put answer in.
per comment, try every 6th cell (it's whole macro, no need split them):
sub test() ' assume want run every 6th cell in column c, starting c1 dim lastrow long lastrow = cells(rows.count, 3).end(xlup).row ' gets our last row in column c data dim cel range, rng range = 6 lastrow step 6 'have start @ row 4, since other row less messes formula cells(i, 3).select ' because can't have row 3-3 cells(i, 3).formula = "=(" & cells(i - 2, 3).address & "+" & cells(i - 1, 3).address & ")-" & cells(i - 3, 3).address next end sub
Comments
Post a Comment