Find and Replace with exact match

skaisdead22

New Member
Joined
Dec 17, 2020
Messages
15
Office Version
  1. 2013
Platform
  1. Windows
Hello,

I am trying to utilize VBA to perform an absolute find and replace on a column of data within my workbook. It is essentially a glorified vlookup but replaces the data instead.

Presently, my code looks something like this:

Sub ReplaceNoS()
Dim myRange As Range, myList As Range
Set myRange = Sheets("Sheet1").Columns("E:E")
Set myList = Sheets("Sheet2").Columns("B:C")
For Each cel In myList.Columns(1).Cells
myRange.Replace what:=cel.Value, replacement:=cel.Offset(0, 1).Value
Next

End Sub

Sheet1 represents the sheet containing the old values that need to be replaced and Sheet2 contains the paired columns where the old value can be matched with its target data present directly in the column to its right.

The issue I am having is the current code can find a partial string and replace its corresponding value before it finds the full absolute match. Example. if in my Sheet1 there is a value "hot" and in Sheet2 the value "hot" is directly next to "123" the code will perform that replacement. However, in Sheet1 there is a value "hotpocket" and in Sheet2 the value "hotpocket" is directly next "456" the code would produce "123pocket" instead of the desired "456".

Essentially I am just asking if anyone has created a absolute find and replace script where the find is a list of values and the replace is from a list of paired values that are side by side in separate columns.

Thanks,
skaisdead22
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi & welcome to MrExcel.
You can do an exact match like
VBA Code:
myRange.Replace cel.Value, cel.Offset(0, 1).Value, xlWhole, , False, , False, False
 
Upvote 0
This is exactly what I was looking for.

Thank you so much for the help!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Hi & welcome to MrExcel.
You can do an exact match like
VBA Code:
myRange.Replace cel.Value, cel.Offset(0, 1).Value, xlWhole, , False, , False, False
Hi Fluff, I just wanted to say thanks for this post. Solved another of my dilemma's.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

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