excel vba - Using VBA To Add Specific Cells In A Row Depending On The Value Of A Specific Cell -
i trying sum cells within row based on period. instance in period 4 cells b2,c2,d2,e2,and f2. need "if sum this" function run rows have data entered. can have on 200,000 rows, using excel formula doesn't work. output of summing corresponding cell in column o. below shortened version of code (i didn't figure needed post 12 periods).
when run code 'range' of object'_global' failed , highlights specified range chicken. if knows cleaner way this, i'd appreciate it.
sub addrowsbyselectedperiodmacro() sheets("blah").select dim lastrow long, long, period long, chicken long lastrow = activesheet.usedrange.rows.count period = range("p2") = 2 lastrow if period = "1" chicken = cells(i, 2).value + cells(i, 3).value elseif period = "2" chicken = cells(i, 2).value + cells(i, 3).value + cells(i, 4).value else: period = "12" chicken = cells(i, 2).value + cells(i, 3).value + cells(i, 4).value + cells(i, 5).value + cells(i, 6).value + cells(i, 7).value + cells(i, 8).value + cells(i, 9).value + cells(i, 10).value + cells(i, 11).value + cells(i, 12).value + cells(i, 13).value + cells(i, 14).value end if range("02:o").value = chicken next end sub
who needs chicken?
the range want sum on row starting column 2 column2 + period. can define range range.cells(i, 2), cells(i, 2 + period))
, sum range worksheetfunction.sum(range(cells(i, 2), cells(i, 2 + period)))
.
sub addrowsbyselectedperiodmacro() dim lastrow long, long, period long sheets("blah") lastrow = .usedrange.rows.count period = .range("p2") = 2 lastrow .range("o" & i).value = worksheetfunction.sum(.range(.cells(i, 2), .cells(i, 2 + period))) next end end sub
Comments
Post a Comment