Find and Replace with exact match

skaisdead22

New Member
Joined
Dec 17, 2020
Messages
12
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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,856
Office Version
  1. 365
Platform
  1. Windows
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
 

skaisdead22

New Member
Joined
Dec 17, 2020
Messages
12
Office Version
  1. 2013
Platform
  1. Windows
This is exactly what I was looking for.

Thank you so much for the help!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,856
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,138
Messages
5,640,337
Members
417,139
Latest member
madcabbie

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