Find/Replace using VBA

dpchheda

New Member
Joined
Oct 31, 2011
Messages
8
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.
 

Some videos you may like

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,721
Office Version
  1. 2010
Platform
  1. Windows
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.
 

dpchheda

New Member
Joined
Oct 31, 2011
Messages
8
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.
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,721
Office Version
  1. 2010
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,489
Messages
5,601,986
Members
414,489
Latest member
Xlambda

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
Top