excel vba - Replacing a range based on criteria using a macro vba -
thank in advance. pretty new vba trying have single column of cells copied 1 column , pasted number based on single criteria, can change. have list in column e , list in column f. want able pull data cells in column e based on adjacent cell in column f. basically, whatever name type "l7", macro pull data column e corresponds name. have far:
private sub worksheet_change (byval target range) if target.value = "" exit sub dim rn rn = 15 if target.row = 7 , target.column = 12 each cel in range("e:e") if cel.offset(0,1).value = cel.value range("l" & rn).value = cel.value rn = rn+1 end if next cel end if end sub now, want do. works if change name in cell "l7". problem not replace data previous time macro ran. if have list of 20 names , 10 names , run macro pull list of 20 names first, won't clear out names list when pull second.
i attempted several different things on this. tried:
sub clear_cells() sheets("sheet1").range("l15:l100").clearcontents end sub that didn't return error messages nothing happened.
i tried:
if range("l15:100").value <> "" range("l15:100").clearcontents end if that line of code gave me type 13 mismatch error.
simply put, need entire range of cells data being pasted change , there no leftover data previous time macro ran.
your appreciated.
you can call other macro within code:
private sub worksheet_change (byval target range) if target.value = "" exit sub call clear_cells() dim rn rn = 15 if target.row = 7 , target.column = 12 each cel in range("e:e") if cel.offset(0,1).value = cel.value range("l" & rn).value = cel.value rn = rn+1 end if next cel end if end sub note can done using array formulas, feel better option code not run every time cell changes on worksheet.
Comments
Post a Comment