[VBA] Find & Replace with Loop for Named Range

Seba Robles

Board Regular
Joined
May 16, 2018
Messages
71
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello,

I'm not very familiarized with loops and would like your help.

I'm trying to create a code where I will Find text from a named range and Replace it from a List

Sheet 1 has myRange named
Sheet 2 has a List; Column A myFind and Column B myReplace

myFind is the text I want to find from the myRange in Sheet 1
myReplace is the text I want to replace it with

I want to create a code that finds the text of myRange in Sheet 1 and replaces it with the replacement of the list in column B Sheet 1.

The code should go through every row of the list in Sheet 2 and replacing it in myRange of Sheet 1 only.

Can you guys please help me out? Thanks in advance!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try this

Code:
Sub Find_Replace()
    Dim sh1 As Worksheet, sh2 As Worksheet, myFind As Range
    
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    
    For Each myFind In sh2.Range("A1", sh2.Range("A" & Rows.Count).End(xlUp))
        sh1.Range("myRange").Replace What:=myFind, Replacement:=myFind.Offset(0, 1), _
            LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    Next
End Sub
 
Upvote 0
Code:
Sub t()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
    For Each c In sh2.Range("A2", sh2.Cells(Rows.Count, 1).End(xlUp))
        sh1.Range("myRange").Replace c.Value, c.Offset(, 1).Value
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,536
Members
449,037
Latest member
tmmotairi

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