VBA- how to delete duplicate rows keep the second row

RRED

New Member
Joined
Dec 10, 2019
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Team,
I really need help!! Please. I need a VBA code or some way to delete duplicate rows. I want to keep the second one or the last row.
In this example, I have a list of work orders ranging from unique or duplicate numbers. If the work order is unique I leave it alone, if it duplicates in cell b, I look at column E and choose the last row because it's the most recent. Keeping the last one is crucial. The work order is the same so I need to look at column E and keep External and not A-FACLTY .
Thank you in advance

A
B C D E

Order Type WORK Order Bas. start date Description Oper.WorkCenter
PM03 40265394 12/31/2019 365 BOILER SOURCE TEST A-FACLTY
PM03 40265394 12/31/2019 365 BOILER SOURCE TEST External
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
so... External will ALWAYS be the most recent line?
 
Upvote 0
Hi, without VBA you can filter this data, but no problem

Book1
ABCDE
1OrderTypeWORKOrderBas.startdateDescriptionOper.WorkCenter
2PM034026539412/31/2019365_BOILER_SOURCE_TESTExternal
3PM034026539912/31/2019365_BOILER_SOURCE_TESTA-FACLTY
4PM034026540012/31/2019365_BOILER_SOURCE_TESTExternal
5PM034026540012/31/2019365_BOILER_SOURCE_TESTA-FACLTY
6PM034026539412/31/2019365_BOILER_SOURCE_TESTA-FACLTY
7PM034026540012/31/2019365_BOILER_SOURCE_TESTExternal
8PM034026598012/31/2019365_BOILER_SOURCE_TESTA-FACLTY
9PM034026545912/31/2019365_BOILER_SOURCE_TESTA-FACLTY
10PM034026570012/31/2019365_BOILER_SOURCE_TESTA-FACLTY
11PM034026570012/31/2019365_BOILER_SOURCE_TESTExternal
12PM034026545912/31/2019365_BOILER_SOURCE_TESTExternal
RRED


Here ? my code
VBA Code:
Option Explicit
'Hernan Torres, Mikel ERP
'December 10, 2019

Sub check_duplicate_conditional() 'First: mark rows to delete
Dim start_b, rango As Range
Dim i, j As Long
Dim b() As Variant
Dim duplicado, excluir As Boolean

j = 0
Application.ScreenUpdating = False
    Range("B2").Select 'put your own range or get it from workbook, for continuos job
    Set start_b = Range("B" & ActiveCell.Row)
    b = Range(start_b, Cells(Rows.Count, "B").End(xlUp)) 'crea un array y lo guarda en la variable "b"
    Set rango = Range(start_b.Address, start_b.Resize(UBound(b)))
    For i = 1 To UBound(b, 1)
    duplicado = Application.CountIf(rango, (b(i, 1))) > 1
    excluir = Cells(start_b.Row - 1 + i, start_b.Column + 3) = "A-FACLTY"
    
    If (duplicado And excluir) Then
        Cells(start_b.Row - 1 + i, start_b.Column + 6) = 1
        j = j + 1
    End If
    Next
Application.ScreenUpdating = True

Run ("del_duplicate_conditional")
MsgBox j & " row(s) was deleted, from " & UBound(b, 1) & " row(s) checked", vbInformation, "Mikel ERP by htorres"

End Sub


Sub del_duplicate_conditional() 'Second: mark rows to delete
Dim start_b As Range
Dim i As Long
Dim b() As Variant
Dim eliminar As Boolean

Application.ScreenUpdating = False
    Range("H2").Select 'put your own range or get it from workbook, for continuos job
    Set start_b = Range("H" & ActiveCell.Row)
    b = Range(start_b, Cells(Rows.Count, "H").End(xlUp)) 'crea un array y lo guarda en la variable "b"
    
    For i = 1 To UBound(b, 1)
    If Not IsEmpty(ActiveCell.Value) Then
        ActiveCell.EntireRow.Delete
    End If
    If IsEmpty(ActiveCell) Then
    ActiveCell.Offset(1, 0).Activate
    End If
    Next
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,331
Members
449,077
Latest member
jmsotelo

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