Removing Duplicates

Cruiser69

Board Regular
Joined
Mar 12, 2018
Messages
61
Office Version
  1. 365
Platform
  1. Windows
Hi all.
Is there anyone that can please help removing duplicates.
I have searched, but can't find an answer.
We have stock which is scanned. When we export the excel workbook it comes through as in the example below.
Sometimes it duplicates the numbers in column A.
I can't use the delete duplicates down vba, as the description for number 5 is below what would be deleted.
Row 3 would be deleted as well as row 8.
The cells in column B which are empty are fine, as we will fill in the descriptions as we go.
Is there a vba or formula which would accomplish this. Either to remove the row, or remove the duplicate number.

1682443354349.png



Thanks for looking,

Graham
 

Attachments

  • 1682442757032.png
    1682442757032.png
    30.2 KB · Views: 7

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try the following on a copy of your worksheet.

VBA Code:
Option Explicit
Sub Cruiser69()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<~~ Change to actual sheet name
    Dim LRow As Long, LCol As Long, i As Long
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
    
    Dim r As Range
    Set r = ws.Range("A2:A" & LRow).Resize(, 2)
    Dim a, b
    a = r
    ReDim b(1 To UBound(a, 1), 1 To 1)
    For i = 1 To UBound(a, 1)
        If WorksheetFunction.CountIf(r.Columns(1), a(i, 1)) > 1 And a(i, 2) = "" Then b(i, 1) = 1
    Next i
    ws.Cells(2, LCol).Resize(UBound(b, 1)).Value = b
    
    i = WorksheetFunction.Sum(ws.Columns(LCol))
    If i > 0 Then
        ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol)).Sort Key1:=ws.Cells(2, LCol), _
        order1:=xlAscending, Header:=xlNo
        ws.Cells(2, LCol).Resize(i).EntireRow.Delete
    End If
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try the following on a copy of your worksheet.

VBA Code:
Option Explicit
Sub Cruiser69()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<~~ Change to actual sheet name
    Dim LRow As Long, LCol As Long, i As Long
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
   
    Dim r As Range
    Set r = ws.Range("A2:A" & LRow).Resize(, 2)
    Dim a, b
    a = r
    ReDim b(1 To UBound(a, 1), 1 To 1)
    For i = 1 To UBound(a, 1)
        If WorksheetFunction.CountIf(r.Columns(1), a(i, 1)) > 1 And a(i, 2) = "" Then b(i, 1) = 1
    Next i
    ws.Cells(2, LCol).Resize(UBound(b, 1)).Value = b
   
    i = WorksheetFunction.Sum(ws.Columns(LCol))
    If i > 0 Then
        ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol)).Sort Key1:=ws.Cells(2, LCol), _
        order1:=xlAscending, Header:=xlNo
        ws.Cells(2, LCol).Resize(i).EntireRow.Delete
    End If
Application.ScreenUpdating = True
End Sub
Hi Kevin.

This is perfect.

Thanks for your help.

Regards,

Graham
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

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