Friday, April 9, 2010

VBScript - Delete duplicate rows in excel using advanced filter

VBScript - Delete duplicate rows in excel using advanced filter

'Delete duplicate rows in excel using advanced filter
'Francis de la Cerna
'I've been doing this the long way for, well, a long time.
'Sorry, excel, I didn't know you could do that.

set xl = createobject("excel.application")
set wb = xl.workbooks.add
set ws1 = wb.worksheets("sheet1")
set ws2 = wb.worksheets("sheet2")

xl.visible = true

'add some data to sheet1

ws1.range("a1:c1").value = array("a", "b", "c")
ws1.range("a2:c2").value = array( 1 , 2 , 3 )
ws1.range("a3:c3").value = array( 4 , 5 , 6 )
ws1.range("a4:c4").value = array( 7 , 8 , 9 )
ws1.range("a5:c5").value = array( 4 , 5 , 6 )
ws1.range("a6:c6").value = array( 1 , 2 , 3 )
ws1.range("a7:c7").value = array("a", "b", "c")

'filter unique records and copy result in sheet2 starting in cell a1

'i think the 1st row is considered as a header row

xlFilterInPlace = 1
xlFilterCopy = 2
uniqueRecords = true
ws1.range("a1:c7").advancedFilter xlFilterCopy, , ws2.range("a1"), uniqueRecords

[Via]