excel - Macro cuts and paste whole row below data loop -
i have report lists sales salesperson month. when type of sale "open", cell in column d start o (open order, open layaway, etc). can't include open orders in subtotal monthly sales, instead have opens orders in separate section below sales in section called "open orders".
so need macro whenever cell in column d starts o, cut whole row , insert (needs insert isn't blanks when pasting) paste below data. shift data since inserting. problem i'm having macro continue cut , paste though have gone through rows in data set.
sub moveopens() 'this im using establish last row in data set cells(1, 1).select selection.end(xldown).select nrowmax = selection.row = 2 nrowmax sitem = cells(i, 4) while left(sitem, 1) = "o" rows(i).select selection.cut 'moves cursor below data set selection.end(xltoleft).select selection.end(xldown).select selection.offset(4, 0).select 'this part works thinks loop doesn't stop 'and start copy , pasting below new data section selection.insert sitem = cells(i, 4) loop next end sub how can macro know when have reached last row doesn't continue cutting , paste rows copied , pasted? let me know if need more details
i hope don't mind re-wrote code scratch. looks might have recorded macro this, place start when not sure how approach it, produces inefficient , confusing code sometimes.
anyway, should work you:
sub moveopens() dim lastrow, newlast, movedcount integer lastrow = activesheet.cells(activesheet.rows.count, "a").end(xlup).row 'find last row newlast = lastrow + 1 'newlast tracks new last row rows copied , pasted @ end movedcount = 0 = 2 lastrow if left(cells(i, 4), 1) = "o" 'copy row, increment newlast , paste @ bottom. rows(i).cut 'lastrow = lastrow - 1 cells(newlast, 1).select activesheet.paste rows(i).delete = - 1 'since deleted row, must decrement movedcount = movedcount + 1 'keeps track of number of rows moved not overshoot original last line end if if + movedcount = lastrow exit 'exit loop if reached original last line of file next end sub
Comments
Post a Comment