This post will introduce a step-by-step guide on how to remove everything and only keep duplicate rows throught Formula and VBA Code in Excel.
Sometimes we may meet duplicate rows exist in table. In previous tutorial we have taught you how to remove duplicate values in excel, this time we will introduce you the method to only keep duplicate rows in table.
Precondition:
Create a simple table. See screenshot below. Obliviously there are some duplicate values.
Table of Contents
1. Remove Everything but Only Keep Duplicate Rows by Formula & Filter Function
To look up duplicate values from the list we can apply COUNTIF function here.
Step1: In cell B2 enter the formula.
=COUNTIF($A$2:$A$8, A2)=1
If data in A list is unique, then formula will return True, otherwise it returns False.
Step2: Click Enter. We get FALSE for product S001, so it has a copy in the list.
Step3: Drag down the fill handle till reach the end of the list.
Step4: Click on column B, click Data in ribbon, then click on Filter in Sort & Filter group to create filter.
Step5: Click small triangle button to load filter criteria. Check on True under Search section, then click OK.
Verify that only TRUE values are listed.
Step6: Select the visible range except headers. Click Home in the ribbon, then click the small triangle belongs to Find & Select icon in Editing group, then select Go To Special option.
Step7: On Go To Special dialog, check on Visible cells only option, then click OK.
Verify that visible cells in the table are selected automatically.
Step8: On selected range, right click to load below options. Select Delete Row.
Step9: On pops up message, click OK. Then entire sheet rows (visible rows) are deleted immediately.
Step10: Click Data->Filter again to clear filter.
Verify that only duplicate rows are stayed.
Clear texts in B column, then table with only duplicate rows is displayed properly.
2. Remove Everything Only Keep Duplicate Rows with VBA Code
If you want to remove everything and only keep duplicate Cell Values in Excel using VBA code, you can use the following code:
Step1: Press “Alt + F11” to open the VBA editor.
Step2: Insert a new module by clicking on “Insert” > “Module” in the menu bar.
Step3: Copy and paste the above code into the module.
Sub KeepDuplicateCells_ExcelHow()
Dim rng As Range
Dim dict As Object
Dim arr As Variant
Dim i As Long, j As Long
Set dict = CreateObject("Scripting.Dictionary")
On Error Resume Next
Set rng = Application.InputBox("Select the range to keep duplicate cell values", Type:=8)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "No range selected", vbExclamation
Exit Sub
End If
arr = rng.Value
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
If Not dict.exists(arr(i, j)) Then
dict.Add arr(i, j), 1
Else
If dict(arr(i, j)) = 1 Then
dict(arr(i, j)) = 2
End If
End If
Next j
Next i
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
If dict(arr(i, j)) < 2 Then
rng.Cells(i, j).ClearContents
End If
Next j
Next i
End Sub
Step4: Go back to your Excel workbook and Press “Alt + F8” to open the “Macro” dialog box. Select the ” KeepDuplicateCells_ExcelHow” macro from the list and click “Run“.
Step5: select the range that you want to work with.
Step6: Click “OK” to run the macro and remove all non-duplicate cell values from the selected range.
This VBA code uses Application.InputBox to prompt the user to select a range. The code checks if the user has selected a valid range and exits the macro if no range is selected. Otherwise, the code proceeds to iterate through each cell in the selected range and keeps only the duplicate cell values, removing all non-duplicate cell values.
3. Conclusion
You’ll be able to remove all non-duplicate rows from your data range, leaving only the duplicate values behind. This can be useful when you need to identify and analyze the duplicated values in your data set, or when you want to remove any unnecessary or redundant data.