+ Reply to Thread
Results 1 to 4 of 4

VBA to remove duplicate entries in a column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-08-2018
    Location
    Atlanta, Georgia
    MS-Off Ver
    Office 365
    Posts
    104

    VBA to remove duplicate entries in a column

    Hi all,

    I have names of our employees in column A that is being populated from another spreadsheet using "If" formulae. I need to delete the duplicate entries and also delete the row. For this I am trying the following VBA code, but running into problems. The code is stopping at the "Lastrow" line with a run-time error message 438- "object doesn't support this property or method". Any help would be greatly appreciated.

    Also I would like to delete the rows with blanks in this column.
    Sub DelDuplicates()
    
    Dim lastrow As Long, fnd As Variant, loop_ctr As Integer
    
    With Sheets("Bonus Calculation")
        For loop_ctr = 3 To 100
            lastrow = Cells(Rows.Count, .Column(1)).End(xlUp).Row
            fnd = Application.Match(.Range("A" & loop_ctr).Value, Range(Cells(.Row + 1, .Column), Cells(lastrow, .Column)), 0)
            
            If Not IsError(fnd) Then
            .Range("A" & loop_ctr).Rows("1:1").EntireRow.Delete Shift:=xlUp
            End If
        Next loop_ctr
    End With
    End Sub

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to remove duplicate entries in a column

    Hi bsrivatsa,

    Try this (though initially on a copy of your data as the results cannot be undone if they're not as expected):

    Option Explicit
    Sub Macro3()
    
        Dim objMyUniqueData As Object
        Dim rngMyCell As Range
        Dim rngDelRange As Range
        Dim lngLastRow As Long
        Dim xlnCalcMethod As XlCalculation
        
        With Application
            .ScreenUpdating = False
            xlnCalcMethod = .Calculation
            .Calculation = xlCalculationManual
        End With
        
        Set objMyUniqueData = CreateObject("Scripting.Dictionary")
    
        lngLastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    
        For Each rngMyCell In Sheets("Sheet1").Range("A3:A" & lngLastRow)
            If Len(rngMyCell) > 0 Then
                If objMyUniqueData.Exists(CStr(rngMyCell)) = False Then
                    objMyUniqueData.Add CStr(rngMyCell), rngMyCell
                Else
                    If rngDelRange Is Nothing Then
                        Set rngDelRange = rngMyCell
                    Else
                        Set rngDelRange = Union(rngDelRange, rngMyCell)
                    End If
                End If
            Else
                If rngDelRange Is Nothing Then
                    Set rngDelRange = rngMyCell
                Else
                    Set rngDelRange = Union(rngDelRange, rngMyCell)
                End If
            End If
        Next rngMyCell
        
        If Not rngDelRange Is Nothing Then
            rngDelRange.EntireRow.Delete
        End If
        
        With Application
            .Calculation = xlnCalcMethod
            .ScreenUpdating = True
        End With
    
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Contributor
    Join Date
    10-08-2018
    Location
    Atlanta, Georgia
    MS-Off Ver
    Office 365
    Posts
    104

    Re: VBA to remove duplicate entries in a column

    Thank you trebor76.

    I found this solution elsewhere online and seems to work well for me.

    Sub RemoveDuplicateRows()
    
    
    Dim MyRange As Range
    Dim LastRow As Long
    
    LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    Set MyRange = ActiveSheet.Range("A1:D" & LastRow)
    MyRange.RemoveDuplicates Columns:=1, Header:=xlYes
    End Sub

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to remove duplicate entries in a column

    NP. Thanks for the rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How to get reference of duplicate value and set remove duplicate value for future entries
    By bala04msw in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-20-2016, 08:13 AM
  2. Replies: 0
    Last Post: 07-12-2014, 03:17 PM
  3. [SOLVED] Remove duplicate entries from one column and output it into another column using formulas
    By alrikvincent in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 07-12-2014, 08:55 AM
  4. Replies: 0
    Last Post: 06-14-2012, 12:38 PM
  5. How to remove duplicate entries?
    By dorian86 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-06-2007, 12:55 AM
  6. [SOLVED] How to remove duplicate entries in column?
    By registrations@discountcosmeticsguide.com in forum Excel General
    Replies: 1
    Last Post: 06-24-2005, 05:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1