Panoos64

Well-known Member
Joined
Mar 1, 2014
Messages
882
Hi all, i would like to write a vba so that to delete the second duplicate entire rows as my below extracts. In sch. 2. are the original data and on sch. 2. is the expected result.

Thanking you in advance





Sch.1.
ACD
131-12-17
JanuaryFebruary
27800005 ΙNDIVIDUALS7,8006,500
37800022-BUISINESS
47800022-BUISINESS
57800027-TOUR OPERATORS
3,2822,155
67800027-TOUR OPERATORS
77800035-CONFERENCES5,4784,578
87800035-CONFERENCES
97800068-FUNCTIONS 1,258
107800068-FUNCTIONS

<colgroup><col style="mso-width-source:userset;mso-width-alt:804;width:17pt" width="22"> <col style="mso-width-source:userset;mso-width-alt:5778;width:119pt" width="158"> <col style="mso-width-source:userset;mso-width-alt:2084; width:43pt" width="57" span="2"> </colgroup><tbody>
</tbody>


Sch.2.
ACD
131-12-17
JanuaryFebruary
27800005 ΙNDIVIDUALS
7,8006,500
37800022-BUISINESS
47800027-TOUR OPERATORS
3,2822,155
57800035-CONFERENCES5,4784,578
67800068-FUNCTIONS
1,258

<colgroup><col style="mso-width-source:userset;mso-width-alt:804;width:17pt" width="22"> <col style="mso-width-source:userset;mso-width-alt:5778;width:119pt" width="158"> <col style="mso-width-source:userset;mso-width-alt:2084; width:43pt" width="57" span="2"> </colgroup><tbody>
</tbody>
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Code:
Option Explicit


Sub DeleteDuplicateRows()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 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


    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
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Note that COUNTIF works oddly with a Variant that is equal to vbNullString.
        ' Rather than pass in the variant, you need to pass in vbNullString explicitly.
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        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
    MsgBox "Duplicate Rows Deleted: " & CStr(n)




End Sub
 
Upvote 0
Thank you so much alan! It works perfect and nicely. In addition thanks also for your time spent for me. Have a great day!
 
Upvote 0
31/12/2017JanuaryFebruary
7800005 INDIVIDUALS7,8006,500
7800022-BUISINESS
7800027-TOUR OPERATORS3,2822,155
7800035-CONFERENCES5,4784,578
7800068-FUNCTIONS1,258
original data
31/12/2017JanuaryFebruary
this macro removed the duplicate rows7800005 INDIVIDUALS7,8006,500
7800022-BUISINESS
Sub Macro4()7800022-BUISINESS
'7800027-TOUR OPERATORS3,2822,155
' Macro4 Macro7800027-TOUR OPERATORS
' Macro recorded 14/11/2017 by bob7800035-CONFERENCES5,4784,578
'7800068-FUNCTIONS1,258
7800068-FUNCTIONS
'
10 For j = 2 To 100
If Cells(j, 1) = "" Then GoTo 999
If Cells(j, 1) = Cells(j + 1, 1) Then GoTo 20 Else GoTo 40
20 Rows(j + 1).Select
Selection.Delete Shift:=xlUp
GoTo 10
40 Next j
999 End Sub

<colgroup><col><col><col span="4"><col><col><col span="2"></colgroup><tbody>
</tbody>
 
Upvote 0
Hi oldb, the code works perfect and is a simple one! Thank you so much for your support to resolve the issue for my project. Have a nice day!
 
Upvote 0
Just for the records. There is an easy method to delete duplicate rows in a range keeping the first one.

Select the range
Home > Format as Table
in Tables Tools click in Delete Duplicates; uncheck January and February
Ok

Done!

Right-click in any cell inside the table; click in Table and convert to range (if desired)

M.
 
Upvote 0
Hi Marcelo, I tested your above suggestion and is right and also very simple too. My query is how you determine to delete specific second row and not the first ? Just I am asking to improve my knowledge in excel. Have a great day
 
Last edited:
Upvote 0
Hi Marcelo, I tested your above suggestion and is right and also very simple too. My query is how you determine to delete specific second row and not the first ? Just I am asking to improve my knowledge in excel. Have a great day

To determine if it is the first instance, in a column with duplicates, we can use COUNTIF with a progressive range.

Example

A
B
1
Name​
First instance?​
2
Anthony​
Yes​
3
Mary​
Yes​
4
John​
Yes​
5
Mary​
No​
6
Richard​
Yes​
7
Richard​
No​
8
Robert​
Yes​
9
Mary​
No​
10
Charles​
Yes​
11
Mary​
No​

<tbody>
</tbody>


Formula in B2 copied down
=IF(COUNTIF(A$2:A2,A2)=1,"Yes","No")

Note that the range A$2:A2 in the first cell (B2), in the second cell (B3) becomes A$2:A3, in the third A$2:A4, and so on.

M.
 
Upvote 0
Many thanks Marcelo for the above explanation, but regarding your below commands which for me is right and it works for my project, how you determine to delete the 2nd row of the duplicates? Just i would like to learn! Thanks!


Select the range
Home > Format as Table
in Tables Tools click in Delete Duplicates; uncheck January and February
Ok

Done!

Right-click in any cell inside the table; click in Table and convert to range (if desired)
 
Upvote 0

Forum statistics

Threads
1,215,606
Messages
6,125,814
Members
449,262
Latest member
hideto94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top