help to adjusting code clear repeated items into column across sheets

Mussa

Active Member
Joined
Jul 12, 2021
Messages
264
Office Version
  1. 2019
  2. 2010
hello
previous thread I got help and code from this thread
clear duplicated items in specific columns and arrange below for each item based on column
VBA Code:
Sub Mussa_v2[B]a[/B]()
  With Sheets("Sheet2")
    'Clear any existing data
    .UsedRange.Clear
    'Copy the table from Sh1 to Sh2
    Sheets("Sheet1").Range("A1").CurrentRegion.Copy .Range("A1")
    With .Range("A1").CurrentRegion
      'Sort based on column A to get all the groups together
      .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlYes
      With .Columns(1).Offset(1).Resize(.Rows.Count - 1)
        'Look at the column A cells from row 2 down and replace with True if they are the same as the cell above
        .Value = Evaluate(Replace(Replace("if(#=%,True,#)", "#", .Address[B](External:=True)[/B]), "%", .Offset(-1).Address[B](External:=True)[/B]))
        'Clear contents from all cells that contain True
        On Error Resume Next
        .SpecialCells(xlConstants, xlLogical).ClearContents
        On Error GoTo 0
      End With
    End With
  End With
End Sub
now I want clear repeated items from column A for first sheet and clear repeated items from column B for second , third sheet
CLEAR.xlsm
ABCDEF
1DEL NOBATCH NO TTLTT1IMPORTEXPORT
2CCD-1CC-1SS-1TRU1210
3CCD-1CC-1SS-1LTR125
4CCD-1CC-2SS-2FG55
5CCS-2CC-1SS-1TRR105
6CCS-2CC-1SS-1LTR2010
7CSD-1CS-1LL-1RRL155
8CSD-1CS-2LL-2TTY1010
9CCD-1CS-3LL-3MMW2010
10CCLCS-4LL-4NNW1010
11CCLCC-2SS-2LTR215
12CCMCC-3SS-3LTR225
13CSD-1CS-1LL-1RRL155
sh1




CLEAR.xlsm
ABCD
1ITEMDEL NOBATCH NO IMPORT
21CCS-2CC-1 SS-1 TRR10
32CCS-2CC-1 SS-1 LTR20
43CCD-1CC-1 SS-1 TRU12
54CCD-1CC-2 SS-2 FG5
65CSD-1CS-1 LL-1 RRL15
76CSD-1CS-2 LL-2 TTY10
87CCD-1CS-3 LL-3 MMW20
98CCLCS-4 LL-4 NNW10
109CCLCC-2 SS-2 LTR21
1110CCMCC-3 SS-3 LTR22
1211CSD-1CS-1 LL-1 RRL15
msh


CLEAR.xlsm
ABCD
1ITEMDEL NOBATCH NO IMPORT
21CCS-2CC-1 SS-1 TRR10
32CCD-1CC-1 SS-1 TRU12
43CCD-1CC-2 SS-2 FG5
54CCD-2CC-2 SS-2 FG6
65CCD-2CC-2 SS-2 FG7
sdf




result

CLEAR.xlsm
ABCDEF
1DEL NOBATCH NO TTLTT1IMPORTEXPORT
2CCD-1CC-1SS-1TRU1210
3CC-1SS-1LTR125
4CC-2SS-2FG55
5CCS-2CC-1SS-1TRR105
6CC-1SS-1LTR2010
7CSD-1CS-1LL-1RRL155
8CS-2LL-2TTY1010
9CS-3LL-3MMW2010
10CCLCS-4LL-4NNW1010
11CC-2SS-2LTR215
12CCMCC-3SS-3LTR225
13CSD-1CS-1LL-1RRL155
sh1




CLEAR.xlsm
ABCD
1ITEMDEL NOBATCH NO IMPORT
21CCS-2CC-1 SS-1 TRR10
32CC-1 SS-1 LTR20
43CCD-1CC-1 SS-1 TRU12
54CC-2 SS-2 FG5
65CSD-1CS-1 LL-1 RRL15
76CS-2 LL-2 TTY10
87CS-3 LL-3 MMW20
98CCLCS-4 LL-4 NNW10
109CC-2 SS-2 LTR21
1110CCMCC-3 SS-3 LTR22
1211CSD-1CS-1 LL-1 RRL15
msh




CLEAR.xlsm
ABCD
1ITEMDEL NOBATCH NO IMPORT
21CCS-2CC-1 SS-1 TRR10
32CCD-1CC-1 SS-1 TRU12
43CC-2 SS-2 FG5
54CCD-2CC-2 SS-2 FG6
65CC-2 SS-2 FG7
sdf


if it's not possible to mod the code I accept alternative
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi I made a new private subroutine that you can Call from your existing code. Just put the Call above End Sub in your current code. Call forRemovingConsecutiveDuplicates

VBA Code:
Private Sub forRemovingConsecutiveDuplicates()
Application.DisplayAlerts = False

    'Call this subroutine above End Sub in your current code - Call forRemovingConsecutiveDuplicates

    Dim lastRow As Long: Dim i As Long
    
    Sheets("Sheet1").Select 'Pick the right sheet
    'Find the lastRow
    With ActiveSheet
        lastRow = .UsedRange.Rows.Count + .UsedRange.Row - 1
    End With
        
    For i = lastRow To 2 Step -1 'Start at the bottom and work towards the top to clear duplicates
        If Range("A" & i) = Range("A" & i - 1) Then
            Range("A" & i).Value = ""
        End If
    Next i
    
    
    Sheets("Sheet2").Select 'Pick the right sheet
    'Find the lastRow
    With ActiveSheet
        lastRow = .UsedRange.Rows.Count + .UsedRange.Row - 1
    End With
        
    For i = lastRow To 2 Step -1 'Start at the bottom and work towards the top to clear duplicates
        If Range("B" & i) = Range("B" & i - 1) Then
            Range("B" & i).Value = ""
        End If
    Next i
    
    
    Sheets("Sheet3").Select 'Pick the right sheet
    'Find the lastRow
    With ActiveSheet
        lastRow = .UsedRange.Rows.Count + .UsedRange.Row - 1
    End With
        
    For i = lastRow To 2 Step -1 'Start at the bottom and work towards the top to clear duplicates
        If Range("B" & i) = Range("B" & i - 1) Then
            Range("B" & i).Value = ""
        End If
    Next i
    
Application.DisplayAlerts = True
End Sub
 
Upvote 0
thanks for your trying . your code just work for the third sheet:rolleyes:
 
Upvote 0
What are the Sheet names? I used Sheet1, Sheet2, and Sheet3, you may need to update the names.
 
Upvote 0
Interesting, I only test Sheet1 but you said only Sheet3 worked for you. The only thing different about the code is the columns. Column A for Sheet1, Column B for Sheet2 and 3. I would need to see like a screenshot of the code, something wasn't copied or pasted right maybe. You might be about to figure it out, if sheet 3 works, copy and paste and update the sheetname and column to check for duplicates.
 
Upvote 0
my bad ! I copy and paste just in sheet3 module . that's why the result just implement for sheet3.

it works, but I also made mistake about the lastrow in sheet1,2 in lastrow there is duplicate item should also delete it .
 
Upvote 0
Glad you were able to get it working. I noticed the last row in the result for 1 and 2 was it was there, so I made code generate the exact same results. I will paste a piece of code you can place in the three sections to clean all duplicates.
 
Upvote 0
Solution
Ok, this should remove all Duplicates. Each sheet has a section ;).

VBA Code:
Option Explicit
Private Sub forRemovingConsecutiveDuplicates()
Application.DisplayAlerts = False

    'Call this subroutine above End Sub in your current code - Call forRemovingConsecutiveDuplicates

    Dim lastRow As Long: Dim i As Long: Dim j As Long
    
    Sheets("Sheet1").Select  'Picks the right sheet
    With ActiveSheet  'Find the lastRow
        lastRow = .UsedRange.Rows.Count + .UsedRange.Row - 1
    End With
        
    For i = 2 To lastRow
        For j = 3 To lastRow
            If i = j Then  'Make sure the cell doesn't compare itself
                j = j + 1  'or it will become blank
            End If
            
            If Range("A" & i) = Range("A" & j) Then 'Flows top to bottom and
                Range("A" & j).Value = ""           'blanks the duplicate
            End If
        Next j
    Next i
    
        
    Sheets("Sheet2").Select 'Picks the right sheet
    With ActiveSheet 'Find the lastRow
        lastRow = .UsedRange.Rows.Count + .UsedRange.Row - 1
    End With
        
    For i = 2 To lastRow
        For j = 3 To lastRow
            If i = j Then
                j = j + 1
            End If
            
            If Range("B" & i) = Range("B" & j) Then
                Range("B" & j).Value = ""
            End If
        Next j
    Next i
    
        
    Sheets("Sheet3").Select 'Picks the right sheet
    With ActiveSheet 'Find the lastRow
        lastRow = .UsedRange.Rows.Count + .UsedRange.Row - 1
    End With
        
    For i = 2 To lastRow
        For j = 3 To lastRow
            If i = j Then
                j = j + 1
            End If
            
            If Range("B" & i) = Range("B" & j) Then
                Range("B" & j).Value = ""
            End If
        Next j
    Next i
    
    Sheets("Sheet1").Select 'Goes back to the 1st sheet
    
Application.DisplayAlerts = True
End Sub
 

Attachments

  • forRemovingConsecutiveDuplicates.jpg
    forRemovingConsecutiveDuplicates.jpg
    89.9 KB · Views: 5
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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