excel - Comparing Lists and Deleting Duplicates -
i have been using following formula test whether or not single entry in 1 column exists in second column:
=countif($e$1:$e$99504,$i1)>0
what able if above formula evaluates true, delete duplicate entry second column (i.e. duplicate in e1-e99504 range), , shift cells of entry up. believe, aside cell shifting, can done without vba.
however, have 5 columns compare in particular case, more possibly in future. such trying determine way perform above operations on multiple columns using vba. otherwise need run same process 10 times , feel there more efficient way should learn. hierarchy of comparisons follows:
suppose have columns a,b,c,d,e. compared b, c, d, each time duplicates in unfixed columns removed while entries in remain. b fixed, moving through c,d, , e in same manner , again entries in b preserved. until d compared e , process complete. has made algorithm this? appreciated.
using pure vba solution , assuming data looks this:
i'd recommend working backwards since deleting , shifting data around. try code:
sub removeduplicates() dim lastcol integer lastcol = 5 'col 5 column e dim wks worksheet set wks = worksheets("sheet1") dim searchrange range set searchrange = wks.range("a1:a" & wks.cells(rows.count, "a").end(xlup).row) dim comparearray variant dim searcharray variant 'get values col search against comparearray = searchrange.value col = lastcol - 1 1 step -1 'set values search matches searcharray = searchrange.offset(0, col - 1).value 'set values last column compare against comparearray = searchrange.offset(0, col).value = 1 ubound(comparearray) if comparearray(i, 1) = searcharray(i, 1) 'match found, delete , shift left cells(i, col).delete shift:=xltoleft end if next next col end sub
results:
make sure update sheet name, range , last column number match criteria.
Comments
Post a Comment