Deleting this duplicates without shifting the cells

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
How can I accomplish this? Column B will always have duplicate values and I want to remove the duplicates but with a catch.

Book1
ABC
1Order TypeNameNumber
2FirstJohn45654
3SecondJohn45665
4ThirdJohn45878
5FirstJack56887
6SecondJack54513
7ThirdJack15687
8FirstJames78954
9SecondJames65895
10ThirdJames23564
11FirstJane23545
12SecondJane12562
13ThirdJane12564
14FirstJose12357
15SecondJose45879
16ThirdJose15877
17FirstHelen15448
18SecondHelen65897
19ThirdHelen78884
20fourthHelen45658
21FirstJames87945
22SecondJames46593
23ThirdJames32148
Sheet4


I want the end result to look like this. Essentially trying to delete duplicate values on Column B ONLY IF they are in successive rows. i,e if I see James on Rows 2,3,4, 21,22,23 I want to delete James from Rows 3,4 and 22,23 without shifting the cells to look like this

Book1
ABCD
1Order TypeNameNumber
2FirstJohn45654
3Second45665
4Third45878
5FirstJack56887
6Second54513
7Third15687
8FirstJames78954
9Second65895
10Third23564
11FirstJane23545
12Second12562
13Third12564
14FirstJose12357
15Second45879
16Third15877
17FirstHelen15448
18Second65897
19Third78884
20fourth45658
21FirstJames87945
22Second46593
23Third32148
24
Sheet4


Any thoughts?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi Coyotex3,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim ws As Worksheet
    Dim lngRow As Long, lngLastRow As Long
    Dim clnNames As New Collection
    
    Application.ScreenUpdating = False
    
    Set ws = ThisWorkbook.Sheets("Sheet1") '<-Sheet name containing data. Change to suit if necessary.
    lngLastRow = ws.Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For lngRow = 2 To lngLastRow
        On Error Resume Next
            clnNames.Add CStr(ws.Range("B" & lngRow)), ws.Range("B" & lngRow)
            If Err.Number <> 0 Then
                ws.Range("B" & lngRow).ClearContents
            End If
        On Error GoTo 0
    Next lngRow
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Solution
Coyotex because you can never have too many solutions, here is another.

VBA Code:
Sub Clear3()
Dim CellCnt As Integer

CellCnt = Cells(Rows.Count, "A").End(xlUp).Row

For i = CellCnt To 2 Step -1

Cells(i, 2).Select

If Cells(i, 2) Like Cells(i - 1, 2) Then
    Selection.ClearContents
End If

Next i

End Sub
 
Upvote 0
Guys thank you so much. I’m out and about right now. Once I get back home I’ll reply to you both! Really appreciate the help guys!!!
 
Upvote 0
Thanks Ezguy4u - I think I overcomplicated things.

Hi Coyotex3,

Try this simplified code:

VBA Code:
Option Explicit
Sub Macro2()

    Dim ws As Worksheet
    Dim lngRow As Long, lngLastRow As Long
    
    Application.ScreenUpdating = False
    
    Set ws = ThisWorkbook.Sheets("Sheet1") '<-Sheet name containing data. Change to suit if necessary.
    lngLastRow = ws.Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For lngRow = 2 To lngLastRow
        If StrConv(ws.Range("A" & lngRow), vbProperCase) <> "First" Then
           ws.Range("B" & lngRow).ClearContents
        End If
    Next lngRow
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Hi Coyotex3,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim ws As Worksheet
    Dim lngRow As Long, lngLastRow As Long
    Dim clnNames As New Collection
  
    Application.ScreenUpdating = False
  
    Set ws = ThisWorkbook.Sheets("Sheet1") '<-Sheet name containing data. Change to suit if necessary.
    lngLastRow = ws.Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  
    For lngRow = 2 To lngLastRow
        On Error Resume Next
            clnNames.Add CStr(ws.Range("B" & lngRow)), ws.Range("B" & lngRow)
            If Err.Number <> 0 Then
                ws.Range("B" & lngRow).ClearContents
            End If
        On Error GoTo 0
    Next lngRow
  
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
I could not get this one to work for some reason. I keep getting
Hi Coyotex3,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim ws As Worksheet
    Dim lngRow As Long, lngLastRow As Long
    Dim clnNames As New Collection
   
    Application.ScreenUpdating = False
   
    Set ws = ThisWorkbook.Sheets("Sheet1") '<-Sheet name containing data. Change to suit if necessary.
    lngLastRow = ws.Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   
    For lngRow = 2 To lngLastRow
        On Error Resume Next
            clnNames.Add CStr(ws.Range("B" & lngRow)), ws.Range("B" & lngRow)
            If Err.Number <> 0 Then
                ws.Range("B" & lngRow).ClearContents
            End If
        On Error GoTo 0
    Next lngRow
   
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
Hi Robert, thank you for this one. This one works well. I did run into some issues when trying to run the macro and the data was different (same format) it would give me an error. However you answered the original question and gave me what I needed THANK YOU!!!!
 
Upvote 0
Coyotex because you can never have too many solutions, here is another.

VBA Code:
Sub Clear3()
Dim CellCnt As Integer

CellCnt = Cells(Rows.Count, "A").End(xlUp).Row

For i = CellCnt To 2 Step -1

Cells(i, 2).Select

If Cells(i, 2) Like Cells(i - 1, 2) Then
    Selection.ClearContents
End If

Next i

End Sub
This one works as well! This one was able to run through a different set of data(similar format, much larger) without any issues. THANK YOU!!!
 
Upvote 0
Thanks Ezguy4u - I think I overcomplicated things.

Hi Coyotex3,

Try this simplified code:

VBA Code:
Option Explicit
Sub Macro2()

    Dim ws As Worksheet
    Dim lngRow As Long, lngLastRow As Long
   
    Application.ScreenUpdating = False
   
    Set ws = ThisWorkbook.Sheets("Sheet1") '<-Sheet name containing data. Change to suit if necessary.
    lngLastRow = ws.Range("A:C").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   
    For lngRow = 2 To lngLastRow
        If StrConv(ws.Range("A" & lngRow), vbProperCase) <> "First" Then
           ws.Range("B" & lngRow).ClearContents
        End If
    Next lngRow
   
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
Hey Robert, With this one I keep getting a subscript out of range message if I try to run the macro with different data(same format). I'll read up on this error, as I'm unfamiliar with errors in general.
 
Upvote 0
Coyotex because you can never have too many solutions, here is another.

VBA Code:
Sub Clear3()
Dim CellCnt As Integer

CellCnt = Cells(Rows.Count, "A").End(xlUp).Row

For i = CellCnt To 2 Step -1

Cells(i, 2).Select

If Cells(i, 2) Like Cells(i - 1, 2) Then
    Selection.ClearContents
End If

Next i

End Sub
In my dataset (3k rows) I have some typos such as in row 1 I might have "John Doe" in row 2 " Jonh Doe" and in row 3 "JOHN DOE" is there a way of making the macro delete those rows as well even those they are not exact?
 
Upvote 0
Hey Robert, With this one I keep getting a subscript out of range message if I try to run the macro with different data(same format). I'll read up on this error, as I'm unfamiliar with errors in general.

That error message is usually when the code is told to work with a specific tab but it doesn't exist in the workbook. Make sure the sheet name in the following is exactly how it is in the workbook (check also for leading or trailing spaces):

VBA Code:
Set ws = ThisWorkbook.Sheets("Sheet1") '<-Sheet name containing data. Change to suit if necessary.

Robert
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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