(VBA) How can I remove duplicates but exclude rows with a specific value in a column?

moddddmyexcel

New Member
Joined
Mar 6, 2023
Messages
23
Office Version
  1. 365
Platform
  1. Windows
My current code is below. The problem is in column 9 I have over 2000 possible different values but I do not want to remove duplicates for rows that contain "VNASTG" in column 9.

Sheets("Data").Range("A4:I300000").RemoveDuplicates Columns:=Array(1, 2, 9), Header:= _
xlYes
 

Attachments

  • example.png
    example.png
    10.9 KB · Views: 16
This small change should do it I think.

VBA Code:
Sub RemoveDupesWithException_v2()
  With Sheets("Data")
    With .Range("A4", .Range("I" & Rows.Count).End(xlUp))
      .Columns(9).Value = .Worksheet.Evaluate(Replace("if(left(#,3)=""VNA"",[B]#&""||""&ROW(#)[/B],#)", "#", .Columns(9).Address))
      .RemoveDuplicates Columns:=Array(1, 2, 9), Header:=xlYes
      .Columns(9).Replace [B]What:="||*", Replacement:=""[/B], LookAt:=xlPart
    End With
  End With
End Sub
I did not notice the data retention before. I see it now, this solution was crafty!
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Works great! How would I possibly add multiple criteria to this? Say there are additional locations I want to exclude as well like a VNASTG2.

Try using "Like"

Rich (BB code):
Sub RemoveDuplicatesExcep()
  Dim dic As Object
  Dim a As Variant, b As Variant, ky As Variant
  Dim i&, j&, k&
 
  a = Range("A5", Range("I" & Rows.Count).End(3)).Value2
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  Set dic = CreateObject("Scripting.Dictionary")
 
  For i = 1 To UBound(a, 1)
    ky = a(i, 1) & "|" & a(i, 2) & "|" & a(i, 9)
    If a(i, 9) Like "VNA*" Or Not dic.exists(ky) Then
        dic(ky) = Empty
        k = k + 1
        For j = 1 To UBound(a, 2)
          b(k, j) = a(i, j)
        Next
    End If
  Next
 
  Range("A5").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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