VBA: find duplicates and change

Zizzo

New Member
Joined
Nov 10, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I am a beginner at Excel VBA. I just wanna know what code I am going to use for changing the value of the selected column if found duplicate. For example, I want to create a code that changes the duplicates found from their original name to their original name + "_01" (or something similar). It is important that the code changes duplicates in the original row - not copying the row and pasting the result into some other column. I have tried different IF and countIF functions but have yet to succeed in VBA.

My range will be (A1:A100)


Example:

Row ARow A
aaaa
cccc
dddd
xxxx
ssss
eeee
ffff
rrrr
qqqq
aaaa_01
cccc_01
zzzz
ssss_01
zzzz_01
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this code...
VBA Code:
Sub FindandReplace()
    Dim cell As Range
    With Worksheets("Sheet1")
        With .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
            For Each cell In .Cells
                    cell.Value = cell.Value & "_" & WorksheetFunction.CountIf(.Parent.Range(.Cells(1), cell), cell.Value & "_*")
            Next
            .Replace what:="_0", replacement:="", lookat:=xlPart
        End With
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

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