BatMan said:
I 'm looking for a macro that will delete duplicate rows. For example my
data has PO# in column A shipper # in column B and date in C. If row 2 has
the exact same data as row 1 delete row 2 then continue to evaluate
stepping through the file until the end.
If your data set is large and you need fast execution, try the
following (change the constant TABLE_NAME_CURRENT from "XXX" to the
name of your worksheet):
Sub Test()
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim Target As Excel.Range
Dim Con As Object
Dim rs As Object
Dim strCon As String
Dim strPath As String
Dim strSql1 As String
Dim lngCounter As Long
' Amend the following constants to suit
Const TABLE_NAME_CURRENT As String = "" & _
"XXX"
Const FILENAME_XL_TEMP As String = "" & _
"delete_me.xls"
Const TABLE_NAME_NEW As String = "" & _
"MyNewTable"
' Do NOT amend the following constants
Const CONN_STRING_1 As String = "" & _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=<PATH><FILENAME>;" & _
"Extended Properties='Excel 8.0;HDR=YES'"
' Build connection strings
strPath = ThisWorkbook.Path & _
Application.PathSeparator
strCon = CONN_STRING_1
strCon = Replace(strCon, _
"<PATH>", strPath)
strCon = Replace(strCon, _
"<FILENAME>", FILENAME_XL_TEMP)
' Build sql statement
strSql1 = ""
strSql1 = strSql1 & "SELECT DISTINCT * FROM "
strSql1 = strSql1 & " [" & TABLE_NAME_CURRENT & "$]"
' Delete old instance of temp workbook
On Error Resume Next
Kill strPath & FILENAME_XL_TEMP
On Error GoTo 0
' Save copy of worksheet to temp workbook
Set wb = Excel.Application.Workbooks.Add()
With wb
ThisWorkbook.Worksheets(TABLE_NAME_CURRENT). _
Copy .Worksheets(1)
.SaveAs strPath & FILENAME_XL_TEMP
.Close
End With
' Open connection to temp workbook
Set Con = CreateObject("ADODB.Connection")
With Con
.ConnectionString = strCon
.Open
Set rs = .Execute(strSql1)
End With
Set ws = ThisWorkbook.Worksheets.Add
With ws
.Name = TABLE_NAME_NEW
Set Target = .Range("A1")
End With
With rs
For lngCounter = 1 To .fields.Count
Target(1, lngCounter).Value = _
.fields(lngCounter - 1).Name
Next
End With
Target(2, 1).CopyFromRecordset rs
Con.Close
End Sub
Jamie.
--