Question : XLS 2003 vba question - delete duplicate rows

Hi - in excel 2003 I'm trying to setup a sub that will accomplish deleting duplicate rows.

I found a sample vba script that got me started - it's useful for deleting duplicate rows in excel but doesn't entirely accomplish what I want to be able to do.

In its present form (with some modifications from me) I have it mostly working but need to address these items:

1. I want to be able to do an instr test to identify the column I want to use for the duplicate check - right now it's done with me setting the range to say "A1" as shown below, so that column A is used -- but the column may vary - it's best if I can do a string test on row1 (which has the header names in it, looking for say "*SKU*" (wildcards) in A1:AA1 - find the cell that and use that cell to set the column...e.g. might be B1, etc. - but I'm not sure how to write the code to accomplish that.

2. If there are what appear to be blank rows but they are not really...e.g. say row 1-10 has visible data, but someone typed something into rows 11, 12, then deleted it, the code is picking up rows 11 and 12 as needing processing - so maybe there's a len test done on the concantonation of say the first 3 columns of each row (e.g. len (a1+b2+c1)>0) to see if the row needs processing? the issue is I need an accurate count on how many duplicates because later processes rely on this to determine what path to take (e.g. if >100 duplicates found do this, otherwise this)

3. I want to be able to see the results/detail of what was deleted - I have the script setup to show the # of rows it deleted - see bottom "result_sheet.Range("number_rows_deleted").Value = CStr(N)" - but would like the detail - show the row itself - like sequentially write down the rows...see "dupkillerresult" tab in the xls file - rows 11/12 - if it could write the row number, tab name, then row detail....

PS - i am fine with the fact that when a duplicate is found, the first instance/row is preserved.

thanks in advance for any help!
 
shows tab setup sample data usa_data tab - vba is in there too
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
Public Sub DeleteDuplicateRows_usa() 'ctrl-u
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' DeleteDuplicateRows
' This will delete duplicate records, based on the Active Column. That is,
' if the same value is found more than once in the Active Column, all but
' the first (lowest row number) will be deleted.
'
' To run the macro, select the entire column you wish to scan for
' duplicates, and run this procedure.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim R As Long
Dim N As Long
Dim V As Variant
Dim Rng As Range
Dim xlSheet_dupkillerresult As Excel.Worksheet
Dim my_active_source As Excel.Worksheet

Set my_active_source = Worksheets("usa_data")
    my_active_source.Activate
    my_active_source.Range("A1").Select
Set my_active_source = Nothing

On Error GoTo EndMacro
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual


Set Rng = Application.Intersect(ActiveSheet.UsedRange, _
                    ActiveSheet.Columns(ActiveCell.Column))

Application.StatusBar = "Processing Row: " & Format(Rng.Row, "#,##0")

N = 0
For R = Rng.Rows.Count To 2 Step -1
If R Mod 500 = 0 Then
    Application.StatusBar = "Processing Row: " & Format(R, "#,##0")
End If

V = Rng.Cells(R, 1).Value
If V = vbNullString Then
    If Application.WorksheetFunction.CountIf(Rng.Columns(1), vbNullString) > 1 Then
        Rng.Rows(R).EntireRow.Delete
        N = N + 1
    End If
Else
    If Application.WorksheetFunction.CountIf(Rng.Columns(1), V) > 1 Then
        Rng.Rows(R).EntireRow.Delete
        N = N + 1
    End If
End If
Next R

EndMacro:

Application.StatusBar = False
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Set xlSheet_dupkillerresult = Worksheets("dupkillerresult")
xlSheet_dupkillerresult.Activate
xlSheet_dupkillerresult.Range("usa_count").Value = CStr(N)
Set xlSheet_dupkillerresult = Nothing
'MsgBox "Duplicate Rows Deleted: " & CStr(N)

End Sub

Answer : XLS 2003 vba question - delete duplicate rows

Random Solutions  
 
programming4us programming4us