+ Reply to Thread
Results 1 to 4 of 4

Delete Duplicate rows with duplicate in 1 column based on value from another column

Hybrid View

  1. #1
    Registered User
    Join Date
    11-30-2016
    Location
    Dundee
    MS-Off Ver
    MS Office
    Posts
    5

    Delete Duplicate rows with duplicate in 1 column based on value from another column

    Hi,

    On the attached, I need a formula or macro to identify duplicate rows in column A, then for all rows that contain the same data in column A (could be 4 or 5 lines), I want the whole row deleted based on criteria of what has the lowest value in column F.

    Many thanks
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    10-19-2012
    Location
    Omaha, Nebraska USA
    MS-Off Ver
    Excel 2010
    Posts
    249

    Re: Delete Duplicate rows with duplicate in 1 column based on value from another column

    Hi CraigR&M,

    Here is code that I think does what you describe. Out of any duplicates, the value with the highest corresponding value in Column F is kept.

    Sub DeleteDuplicates()
    
    ' Define Variables
    Dim i As Long
    Dim j As Long
    Dim k As Long
    Dim vMatch As Variant
    Dim LastRow As Long
    
    ' Suppress Error Notification
    On Error Resume Next
    Err.Clear
    
    ' Check Duplicates
    k = 2
    100 ' Loop from here
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    
    ' Determine if data has been completely processed
    If (LastRow <= k) Then
       Application.StatusBar = "Process Complete..."
       Application.Wait (Now + TimeValue("00:00:02"))
       GoTo 10000
    End If
    ' Check for duplicates and delete row with lowest value in Column F
    For i = k To LastRow
       Application.StatusBar = "Checking line " & i & " of " & LastRow & "..."
       j = i + 1
       vMatch = WorksheetFunction.Match(Range("A" & i), Range(Cells(j, 1), Cells(LastRow, 1)), 0)
       If (Err.Number = 0) Then
          If (Range("F" & i) < Range("F" & i + vMatch)) Then
             Rows(i).Delete shift:=xlUp
          Else
             Rows(i + vMatch).Delete shift:=xlUp
          End If
          k = i
          GoTo 100
       Else
          Err.Clear
       End If
    Next i
    
    10000 ' Clear Status Bar and Enable Error Notification
    Application.StatusBar = ""
    On Error GoTo 0
    
    End Sub
    Hope that helps,

    Dan
    Last edited by djbomaha; 11-30-2016 at 07:56 PM. Reason: Error in code

  3. #3
    Forum Contributor
    Join Date
    10-19-2012
    Location
    Omaha, Nebraska USA
    MS-Off Ver
    Excel 2010
    Posts
    249

    Re: Delete Duplicate rows with duplicate in 1 column based on value from another column

    *** Double post deleted ***
    Last edited by djbomaha; 11-30-2016 at 07:35 PM. Reason: Double Post

  4. #4
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Delete Duplicate rows with duplicate in 1 column based on value from another column

    Sub Deletesies()
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = True
    Application.StatusBar = "Finding Minimum Values"
    Dim cell As Range
    Dim dict As Object
    Set dict = CreateObject("scripting.dictionary")
    LR = Range("A" & Rows.Count).End(xlUp).Row
    With Range("I2:I" & LR)
        .FormulaR1C1 = "=MIN(IF(R2C1:R" & LR & "C1=R[0]C[-8],R2C6:R" & LR & "C6))"
        .FormulaArray = .FormulaR1C1
        .Value = .Value
    End With
    For i = LR To 2 Step -1
        If Cells(i, 6) <> Cells(i, 9) Then
            Cells(i, 1).EntireRow.Delete
        Else
            If Not dict.exists(Cells(i, 1).Value) Then
                dict.Add Cells(i, 1).Value, 0
            Else
                Cells(i, 1).EntireRow.Delete
            End If
        End If
        Cells(i, 9).Clear
        Application.StatusBar = Format((LR - i) / LR, "Percent") & " Row Removal Complete"
    Next
    Set dict = Nothing
    Application.StatusBar = False
    Application.ScreenUpdating = True
    End Sub
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

+ 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. Delete Duplicate rows with duplicate in 1 column based on value from another column
    By CraigR&M in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-30-2016, 02:06 PM
  2. [SOLVED] Vba to sum up rows based on column a & b and delete duplicate rows
    By kevivu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2016, 03:15 PM
  3. Delete Duplicate Rows, if column does not contain x
    By JulieQ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-23-2015, 04:08 PM
  4. highlight duplicate rows and delete based on highest value from column
    By adnan5586 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-10-2015, 02:05 AM
  5. [SOLVED] Delete rows based on duplicate cell, but leaving first and last duplicate.
    By LadyNicole in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-28-2013, 05:07 AM
  6. Replies: 4
    Last Post: 01-25-2011, 08:03 PM
  7. Replies: 5
    Last Post: 05-29-2006, 09:25 AM

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