excel - Store Cell Value but Exclude Empty Cells: -
i'm writing code searches column b changes in names inserts row , formulas based on variable data in columns e:j when name changes new (new value in cell). names listed more once, consecutively, in column b , i'd group 1 total per person it's doing 1 total each time there data in column b, not each time name changes. other issue i'm having is not totaling last person because there nothing empty cells in column b after last name nothing ever change activate "then". appreciate feedback on code. here have:
dim firstrow integer ' start on row 7 avoid including header row = 7 firstrow = 1 previous = range("b7").value while row < 1000 ' move next row row = row + 1 current = range("b" & row).value if current <> "" , current <> previous rows(row).insert shift:=xldown ' formulas columns g, i, j, , k range("g" & row).formula = "=sum(e" & firstrow + 2 & ":g" & row - 1 & ")" range("i" & row).formula = "=sum(h" & firstrow + 2 & ":i" & row - 1 & ")" range("j" & row).formula = ws.range("g" & row) - ws.range("i" & row) range("k" & row).formula = ws.range("j" & row) / ws.range("g" & row) row = row firstrow = row end if previous = current wend
you consider different approach:
looping through rows in reverse order last 1 first one
using
autofilter(),specialcells()methods ofrangeobject isolate blocks of contiguous not empty cells write totals formulas
like follows:
option explicit sub main() dim irow long dim area range range("b1", cells(rows.count, 2).end(xlup)) irow = .rows.count 2 step -1 if .cells(irow, 1) <> .cells(irow + 1, 1) .rows(irow + 1).entirerow.insert shift:=xldown next .autofilter field:=1, criteria1:="<>" if application.worksheetfunction.subtotal(103, .cells) > 0 set area = .specialcells(xlcelltypevisible) .parent.autofiltermode = false each area in area.areas area.offset(area.rows.count).resize(1) .offset(, 5).formula = "=sum(" & intersect(range("e:g"), area.entirerow).address & ")" .offset(, 7).formula = "=sum(" & intersect(range("h:i"), area.entirerow).address & ")" .offset(, 8).formular1c1 = "=rc7-rc9" .offset(, 9).formular1c1 = "=rc10/rc7" end next end if end end sub
Comments
Post a Comment