Macro to remove non-duplicates

MrMatt

New Member
Joined
Apr 26, 2012
Messages
30
Hi

I have a Conditional Filter on Columns A and C to highlight duplicates.

What I'm trying to achieve is a macro that finds the non-duplicates in Column C (e.g. Luke) and then deletes that value and all subsequent values to the right, shifting the cells below up.

Actual:
ABCDE
James1Jamesabc123
Bill8Lukedef456
Ted3Billghi789
Art4Tedjkl012
Mike7Artmno345
etc.

<tbody>
</tbody>

Desired:
ABCDE
James1Jamesabc123
Bill8Billghi789
Ted3Tedjkl012
Art4Artmno345
Mike7Mikepqr678

<tbody>
</tbody>

Any suggestions on how to achieve this would be much appreciated.

Many Thanks

Matt
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Something like this should work...

Code:
Sub Macro1()

Dim x As Long
Dim cll As Range

For Each cll In Selection
x = Application.WorksheetFunction.CountIf(ActiveSheet.Range("A:A"), cll)
If x = 0 Then
Range("C" & cll.Row & ":E" & cll.Row).Delete Shift:=xlUp
End If
Next cll

End Sub

Select the cells in C that you want to check, then it will check if they appear in Column A, if not then it deletes the corresponding cells from C to E.
 
Upvote 0
Hi

I have a Conditional Filter on Columns A and C to highlight duplicates.

What I'm trying to achieve is a macro that finds the non-duplicates in Column C (e.g. Luke) and then deletes that value and all subsequent values to the right, shifting the cells below up.

Actual:
ABCDE
James1Jamesabc123
Bill8Lukedef456
Ted3Billghi789
Art4Tedjkl012
Mike7Artmno345
etc.

<tbody>
</tbody>

Desired:
ABCDE
James1Jamesabc123
Bill8Billghi789
Ted3Tedjkl012
Art4Artmno345
Mike7Mikepqr678

<tbody>
</tbody>

Any suggestions on how to achieve this would be much appreciated.

Many Thanks

Matt

I though i might have had it.. but just can't rap my code into it..
 
Upvote 0
Something like this should work...

Code:
Sub Macro1()

Dim x As Long
Dim cll As Range

For Each cll In Selection
x = Application.WorksheetFunction.CountIf(ActiveSheet.Range("A:A"), cll)
If x = 0 Then
Range("C" & cll.Row & ":E" & cll.Row).Delete Shift:=xlUp
End If
Next cll

End Sub

Select the cells in C that you want to check, then it will check if they appear in Column A, if not then it deletes the corresponding cells from C to E.

This sort of works :) but I'm having to click on each individual cell. If I filter to highlight the non-matching cells, it doesn't work. I'm offline for the remainder of the day but this is a good start so I'll tinker with the macro tomorrow and provide a further update then.

Thanks for your help so far.

Matt
 
Upvote 0
Dont filter anything just select all the cells in column C that you want to check... For example: select from C2 to C20... and then run the macro...

Also you can easily change "For Each cll In Selection" for something like "For Each cll In Range("C2:C20")" and thus having a predefined range to work at... the last row could easily be determined by code if it's not gonna be the same always...

You can also change "Range("C" & cll.Row & ":E" & cll.Row).Delete Shift:=xlUp" from "E" to any other last column you want to delete. I put "E" since that's the last one in your example.

I can help you, but I'll need a little more detail... for example, do you have any more data in Column C? or all the data is names-to-check? do Column C has a header?
 
Last edited:
Upvote 0
Assuming the data has headings, and column F available to use as a helper, try this in a copy of your workbook.
Code:
Sub rDeleteNonDupes()
  Dim rDel As Range
  
  Application.ScreenUpdating = False
  Range("F2").Formula = "=COUNTIF(A:A,C2)=0"
  With Range("C1", Range("E" & Rows.Count).End(xlUp))
    .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("F1:F2"), Unique:=False
    On Error Resume Next
    Set rDel = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlVisible)
    ActiveSheet.ShowAllData
    On Error GoTo 0
  End With
  If Not rDel Is Nothing Then
    rDel.Delete Shift:=xlUp
  End If
  Range("F2").ClearContents
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Dont filter anything just select all the cells in column C that you want to check... For example: select from C2 to C20... and then run the macro...

Also you can easily change "For Each cll In Selection" for something like "For Each cll In Range("C2:C20")" and thus having a predefined range to work at... the last row could easily be determined by code if it's not gonna be the same always...

You can also change "Range("C" & cll.Row & ":E" & cll.Row).Delete Shift:=xlUp" from "E" to any other last column you want to delete. I put "E" since that's the last one in your example.

I can help you, but I'll need a little more detail... for example, do you have any more data in Column C? or all the data is names-to-check? do Column C has a header?

Thanks Jeffrey, it works much better with For Each cll in Range. Rather than calculating the last row I just put in a high value row that I know is blank ("C2:C500") and it works well on some test data. I'll test it with my real data shortly and update you.

Thanks again.

Matt
 
Upvote 0

Forum statistics

Threads
1,216,518
Messages
6,131,121
Members
449,624
Latest member
MandlaMan

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