excel - How to copy paste row inside a loop if a cell is abiding by an if condition vba -
i built macro copy paste rows contain cells abide if condition. macro measures difference of values between 4 pairs of cells of row and, if difference bigger desired one, copy-pastes row, cell containing ‘guilty’ value (or values if 4 comparisons not abiding restrictions) belongs (or they), in different sheet (“wfrandvfr_performance”). @ end, colors "guilty" cells. works fine apart paste part, provided below:
sheets("wfrandvfr_performance").select range("a" & rows.count).end(xlup).offset(1).select activesheet.paste
below posting macro
sub wfrandvfr_performance() application.calculation = xlcalculationmanual application.screenupdating = false application.enableevents = false sheets("tracker").select dim mdiff1 double mdiff1 = 0.01 dim mdiff2 double mdiff2 = 0.03 dim mdiff3 double mdiff3 = 0.01 dim mdiff4 double mdiff4 = 0.03 sheets("tracker").select each cell1 in range(range("u2"), range("u2").end(xldown)) if cell1.value - cell1.offset(0, 1).value > mdiff1 or cell1.value - cell1.offset(0, 2).value > mdiff2 cell1.entirerow.copy sheets("wfrandvfr_performance").select range("a" & rows.count).end(xlup).offset(1).select activesheet.paste end if next cell1 sheets("tracker").select each cell2 in range(range("ab2"), range("ab2").end(xldown)) if cell2.value - cell2.offset(0, 1).value > mdiff3 or cell2.value - cell2.offset(0, 2).value > mdiff4 cell2.entirerow.copy sheets("wfrandvfr_performance").select range("a" & rows.count).end(xlup).offset(1).select activesheet.paste end if next cell2 sheets("wfrandvfr_performance").select columns(4).removeduplicates columns:=array(1) on error resume next columns(4).specialcells(xlblanks).entirerow.delete each cell3 in range(range("u2"), range("u2").end(xldown)) if cell3.value - cell3.offset(0, 1).value > mdiff1 cell3.offset(0, 1).interior.colorindex = 3 end if if cell3.value - cell3.offset(0, 2).value > mdiff2 cell3.offset(0, 2).interior.colorindex = 5 end if next cell3 each cell4 in range(range("ab2"), range("ab2").end(xldown)) if cell4.value - cell4.offset(0, 1).value > mdiff3 cell4.offset(0, 1).interior.colorindex = 3 end if if cell4.value - cell4.offset(0, 2).value > mdiff4 cell4.offset(0, 2).interior.colorindex = 5 end if next cell4 sheets("wfrandvfr_performance").select if not activesheet.autofiltermode activesheet.rows(1).autofilter end if application.calculation = xlcalculationautomatic application.screenupdating = true application.enableevents = true end sub
you find last row such:
dim lr long lr = sheets("wfrandvfr_performance").cells(sheets("wfrandvfr_performance").rows.count, 1).end(xlup).row cell1.entirerow.copy sheets("wfrandvfr_performance").range("a" & lr+1)
another option, , best (avoids copy/paste):
dim lr long lr = sheets("wfrandvfr_performance").cells(sheets("wfrandvfr_performance").rows.count, 1).end(xlup).row sheets("wfrandvfr_performance").range("a" & lr+1).value=cell1.entirerow.value
putting code:
dim lr long sheets("wfrandvfr_performance").rows(1).value=sheets("tracker").rows(1).value each cell1 in range(range("u2"), range("u2").end(xldown)) if cell1.value - cell1.offset(0, 1).value > mdiff1 or cell1.value - cell1.offset(0, 2).value > mdiff2 lr = sheets("wfrandvfr_performance").cells(sheets("wfrandvfr_performance").rows.count, 2).end(xlup).row sheets("wfrandvfr_performance").range("a" & lr+1).value=cell1.entirerow.value end if next cell1
Comments
Post a Comment