VBA other Duplicate id address

KDS14589

Board Regular
Joined
Jan 10, 2019
Messages
182
Office Version
  1. 2016
Platform
  1. Windows
I have the following code to identify duplicates in Column “G” and identifies them in Column “K” with Duplicate RGB. But I want to go one step more and have the Row number of the other duplicate included with the Duplicate RGB, such as Duplicate RBG row 569

VBA Code:
For row_data = row_data_start To row_data_last

On Error Resume Next



If Application.WorksheetFunction.CountIf(Ws.Range("G4:G500"), Ws.Range("G" & row_data)) > 1 Then

Ws.Range("K" & row_data).Value = "Duplicate RGB " '''''& Ws.Range("G" & row_data).Address > 1

Ws.Range("K" & row_data).Interior.Color = vbRed

Else

Ws.Range("K" & row_data).Value = ""

End If

Next row_data
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this:

VBA Code:
Sub Duplicate_Rows()
  Dim c As Range, rng As Range, f As Range
  Dim cell As String, cad As String
  Dim sh As Worksheet
  
  Set sh = Sheets("Sheet1")   'Fit the name of your sheet
  Set rng = sh.Range("G4", sh.Range("G" & Rows.Count).End(3))
  rng.Offset(, 4).Clear
  
  For Each c In rng
    cad = ""
    Set f = rng.Find(c.Value, c, xlValues, xlWhole, , xlNext)
    If Not f Is Nothing Then
      cell = f.Address
      If cell <> c.Address Then
      
        'Check if there are 2 or more equal data
        Do
          If f.Row <> c.Row Then cad = cad & f.Row & ", "
          Set f = rng.FindNext(f)
        Loop While Not f Is Nothing And f.Address <> cell
        
        With sh.Range("K" & c.Row)
          .Value = "Duplicate RGB " & Left(cad, Len(cad) - 2)
          .Interior.Color = vbRed
        End With
        
      End If
    End If
  Next
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Sub Duplicate_Rows()
  Dim c As Range, rng As Range, f As Range
  Dim cell As String, cad As String
  Dim sh As Worksheet
 
  Set sh = Sheets("Sheet1")   'Fit the name of your sheet
  Set rng = sh.Range("G4", sh.Range("G" & Rows.Count).End(3))
  rng.Offset(, 4).Clear
 
  For Each c In rng
    cad = ""
    Set f = rng.Find(c.Value, c, xlValues, xlWhole, , xlNext)
    If Not f Is Nothing Then
      cell = f.Address
      If cell <> c.Address Then
     
        'Check if there are 2 or more equal data
        Do
          If f.Row <> c.Row Then cad = cad & f.Row & ", "
          Set f = rng.FindNext(f)
        Loop While Not f Is Nothing And f.Address <> cell
       
        With sh.Range("K" & c.Row)
          .Value = "Duplicate RGB " & Left(cad, Len(cad) - 2)
          .Interior.Color = vbRed
        End With
       
      End If
    End If
  Next
End Sub
it works
THANKS
 
Upvote 0
Im glad to help you, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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