Find/Replace using VBA

dpchheda

New Member
Joined
Oct 31, 2011
Messages
10
Hi,

I have around 10 names which I want to replace with their code names. For Example, "Alan Richard" needs to be replaced with AR001. Problem is I don't know cell range and this name might appear more than once in the data.

Please help.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Something like this maybe...

Code:
Sub ReplaceNames()
  Dim N As Variant, NameList As Variant, NameCodes As Variant
  NameList = Array("Alan Richard", "Sam Spade", "Joe Blow", "John Doe")
  NameCodes = Array("AR001", "SS001", "JB001", "JD001")
  For Each N In NameList
    Cells.Replace N, NameCodes, xlPart, , False
  Next
End Sub
where you would use your actual names and codes in place of the example ones I used above. Note that the order of the names in the NameList and the codes in the NameCodes must be in a one-to-one correspondence.
 
Upvote 0
Hi Rick,

Thanks for your post.

This code replaces all the cell values (all other names) with only AR001 and ignores other namecodes in the code.
 
Upvote 0
This code replaces all the cell values (all other names) with only AR001 and ignores other namecodes in the code.
Sorry, my fault, try this code instead...

Code:
Sub ReplaceNames()
  Dim X As Long, NameList As Variant, NameCodes As Variant
  NameList = Array("Alan Richard", "Sam Spade", "Joe Blow", "John Doe")
  NameCodes = Array("AR001", "SS001", "JB001", "JD001")
  For X = LBound(NameList) To UBound(NameList)
    Cells.Replace NameList(X), NameCodes(X), xlPart, , False
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,182
Members
448,872
Latest member
lcaw

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