excel - Deleting specific intervals of rows in a csv file with VBA -
thanks nice guys here @ forum, have been able use following macro need :
public sub main() 'declaration dim rng range const sourcerange = "h" dim numrange range, formulacell range dim sumaddr string dim c long 'loop trough rows set rng = range("h2") while rng.value <> "" rng.offset(20).resize(1).entirerow.insert set rng = rng.offset(21) wend 'fill blank rows in columns("a:a").select selection.specialcells(xlcelltypeblanks).select selection.formular1c1 = "=r[-1]c" each numrange in columns(sourcerange).specialcells(xlconstants, xlnumbers).areas sumaddr = numrange.address(false, false) set formulacell = numrange.offset(numrange.count, 0).resize(1, 1) formulacell.formula = "=sum(" & sumaddr & ")" 'change formatting liking: formulacell.font.bold = true formulacell.font.color = rgb(255, 0, 0) c = numrange.count next numrange end sub
in short, creates row every 20th row, takes number cell above in column "a" in blank row , sum 20th previous cells in column "h" put "total score" in blank row.
i have 1 more question regarding macro.
after having run macro above, "sum" of 20 , 20 rows placed in new row added. ideally, delete of 20 rows have been summed , have added row left. problem last part of previous macro sums given number of cells up, , if delete rows summing wrong (as number cell above in column a).
is there way add macro deletes 20 , rows without effecting summing , number previous cell in column a?
it this: delete rows 2-21, skip row 22, delete row 23-42, skip row 43, delete row 44-63, skip row 64 , on.
i understand means having alter previous macro posted, guess it's worth asking.
thanks in advance guys.
best, helge
here go:
public sub main() 'declaration dim rng range const sourcerange = "h" dim numrange range, formulacell range dim sumaddr string dim c long dim ifirstrow integer, ilastcell integer 'loop trough rows in h column set rng = range(sourcerange & "2") while rng.value <> "" rng.offset(20).resize(1).entirerow.insert rng.offset(20) .formula = "=sum(" & sourcerange & rng.row & ":" & sourcerange & rng.offset(19).row & ")" .formula = .value2 'change formatting liking: .font.bold = true .font.color = rgb(255, 0, 0) 'set next group start set rng = rng.offset(21) 'delete rows ifirstrow = .column - 1 range(.offset(-20, -ifirstrow), .offset(-1, -ifirstrow)) ilastcell = .cells(.rows.count, "a").end(xlup).row 'fill blank rows in cells(rng.offset(-1).row, 1) .formula = "=" & cells(ilastcell, 1).address .formula = .value2 end 'delete group rows .entirerow.delete end end wend end sub
Comments
Post a Comment