Find and Replace

extremealv

New Member
Joined
Nov 27, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am new to VBA and I tried searching for a solution in the forum and nothing similar was found.

I am trying to Find and Replace.

Here is what I have. In one specific sheet I would like to look for either NA, EMEA, ASIA. Then I would like to replace it only with JAPAN. I managed to do it using only the first value but I do not understand how to write a VBA where it will check the first value "NA" and check the next one "EMEA" and so on.

Sub ChangeValue()

Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long

fndList = Array("NA", "EMEA", "ASIA")
rplcList = Array("Japan!")

Set sht = Sheets("Sheet 1")

sht.Cells.Replace What:=fndList, Replacement:=rplcList, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
rpldList does not need to be an array since there is only one value that is used for all replacements.

You need to loop through fndList.

I also recommend CODE tags when posting code, which preserves formatting.
VBA Code:
Sub ChangeValue()

   Dim sht As Worksheet
   Dim fndList As Variant
   Dim fnd As Variant
   Dim rplc As String
   Dim x As Long
   
   fndList = Array("NA", "EMEA", "ASIA")
   rplcList = "Japan!"
   
   Set sht = Sheets("Sheet 1")
   
   For Each fnd In fndList
   
      sht.Cells.Replace What:=fnd, Replacement:=rplc, _
      LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
      SearchFormat:=False, ReplaceFormat:=False
   
   Next fnd


End Sub
 
Upvote 0
Thank you Jeff.

For some reason when I used the macro, it removed the found values and it did not replace it with anything. Leaving the values in the cell with no rplcList
 
Upvote 0
I'm sorry, there was an error in the code. I used the old List variable.
Rich (BB code):
Sub ChangeValue()

   Dim sht As Worksheet
   Dim fndList As Variant
   Dim fnd As Variant
   Dim rplc As String
   Dim x As Long
  
   fndList = Array("NA", "EMEA", "ASIA")
   rplc = "Japan!"
  
   Set sht = Sheets("Sheet 1")
  
   For Each fnd In fndList
  
      sht.Cells.Replace What:=fnd, Replacement:=rplc, _
      LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
      SearchFormat:=False, ReplaceFormat:=False
  
   Next fnd


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,159
Members
452,615
Latest member
bogeys2birdies

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