Delete non-duplicate rows

greatavion

New Member
Joined
Apr 3, 2023
Messages
1
Office Version
  1. 2010
Platform
  1. Windows
Hello folks,

I have a spreadsheet that contains many rows of data. Some of the cell values in column F are duplicates while others are unique and there is only one instance. I would like to use VBA to delete the rows that contain unique values in column F, while leaving the duplicate values (+ their corresponding rows) found in column F.

I have used the below piece of code and it works fine. The problem is the data in Column B disappears on execution of the below code. As I am new to vba, unable to figure out what is the root cause.

Sub Unique_Delete()
Dim UsdRws As Long

UsdRws = Range("A" & Rows.Count).End(xlUp).Row
With Cells(1, Columns.Count).End(xlToLeft).Offset(1, 1).Resize(UsdRws)
.Formula = "=if(countifs(f:f,f2)>1,"""",1)"
.Value = .Value
.SpecialCells(xlConstants).EntireRow.Delete
End With
End Sub

Any assistance would be greatly appreciated.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hello...

Have you tried?

VBA Code:
Option Explicit

Sub Unique_Delete()
    Dim UsdRws As Long
    Dim i As Long
    
    UsdRws = Range("A" & Rows.Count).End(xlUp).Row
    For i = UsdRws To 2 Step -1
        If WorksheetFunction.CountIfs(Range("F:F"), Range("F" & i)) = 1 Then
            Rows(i).Delete
        End If
    Next i
End Sub


This code loops through each row from the last row to the second row and checks whether the value in column F has a count of 1 (i.e., is unique). If so, it deletes only that row.

Note: Make sure to test this code on a copy of your data before running it on your actual spreadsheet.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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