Delete and shift cells up

Chewyhairball

Active Member
Joined
Nov 30, 2017
Messages
312
Office Version
  1. 365
Platform
  1. Windows
Hi folks

Looking for VBA to find duplicate values in ID column and delete them by shifting cells up. (not deleting whole row). Thanks

Example below is what I want.

Actual table range will be B2:E22
1658162041729.png
to this
1658162120714.png
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with. It will generally get you faster results.

See if this does what you want. Assumes cells G2:G3 are empty - but could be any other vertical pair of cells.

VBA Code:
Sub ReoveDuplicateIDRows()
  Dim rDupes As Range
 
  Range("G3").Formula = "=ISNUMBER(MATCH(B3,B$2:B2,0))"
  With Range("B2:E22")
    .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("G2:G3"), Unique:=False
    If .Columns(1).SpecialCells(xlVisible).Count > 1 Then
      Set rDupes = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlVisible)
      .Parent.ShowAllData
      rDupes.Delete Shift:=xlUp
    Else
      .Parent.ShowAllData
    End If
    Range("G3").ClearContents
  End With
End Sub
 
Upvote 0
I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with. It will generally get you faster results.

See if this does what you want. Assumes cells G2:G3 are empty - but could be any other vertical pair of cells.

VBA Code:
Sub ReoveDuplicateIDRows()
  Dim rDupes As Range
 
  Range("G3").Formula = "=ISNUMBER(MATCH(B3,B$2:B2,0))"
  With Range("B2:E22")
    .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("G2:G3"), Unique:=False
    If .Columns(1).SpecialCells(xlVisible).Count > 1 Then
      Set rDupes = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlVisible)
      .Parent.ShowAllData
      rDupes.Delete Shift:=xlUp
    Else
      .Parent.ShowAllData
    End If
    Range("G3").ClearContents
  End With
End Sub
Hi Peter. I have not had a chance to look at the code yet but as soon as I do I will let you know if it works.

thanks

Rory
 
Upvote 0
As for XL2BB I cannot send anything, other than a screenshot of some random made up data, from a works PC.
 
Upvote 0
Hi folks

Looking for VBA to find duplicate values in ID column and delete them by shifting cells up. (not deleting whole row). Thanks

Example below is what I want.

Actual table range will be B2:E22
View attachment 69562 to this View attachment 69563
Hi
This code doesn't remove all of them. It leaves on. For the example above it removes the bottom 2 with ID 4 but leaves the top one.
I tried it with 4 ID's the same but same results. I would like it to remove all of them like in my example above.

thanks

Rory
 
Upvote 0
Yes, sorry, I missed that in the original images. Try this one instead.

VBA Code:
Sub ReoveDuplicateIDRows_2()
  Dim rDupes As Range
  
  With Range("B2:E22")
    Range("G3").Formula = "=COUNTIF(" & .Columns(1).Address & "," & .Cells(2, 1).Address(0, 0) & ")>1"
    .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("G2:G3"), Unique:=False
    If .Columns(1).SpecialCells(xlVisible).Count > 1 Then
      Set rDupes = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlVisible)
      .Parent.ShowAllData
      rDupes.Delete Shift:=xlUp
    Else
      .Parent.ShowAllData
    End If
    Range("G3").ClearContents
  End With
End Sub
 
Upvote 0
Solution
Sub ReoveDuplicateIDRows_2() Dim rDupes As Range With Range("B2:E22") Range("G3").Formula = "=COUNTIF(" & .Columns(1).Address & "," & .Cells(2, 1).Address(0, 0) & ")>1" .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("G2:G3"), Unique:=False If .Columns(1).SpecialCells(xlVisible).Count > 1 Then Set rDupes = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlVisible) .Parent.ShowAllData rDupes.Delete Shift:=xlUp Else .Parent.ShowAllData End If Range("G3").ClearContents End With End Sub
Perfect!!
thanks very much :)
 
Upvote 0
Note then marking a post as the solution, please mark the post that contains the solution (not your own post saying it worked).
When a post is marked as the solution, it is then shown right underneath the original question, so people viewing the question can easily see the question and solution in a single quick glance without having to hunt through all the posts.
 
Upvote 0
Note then marking a post as the solution, please mark the post that contains the solution (not your own post saying it worked).
When a post is marked as the solution, it is then shown right underneath the original question, so people viewing the question can easily see the question and solution in a single quick glance without having to hunt through all the posts.
The post with the solution is marked?!
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,196
Latest member
Maxkapoor

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