SOLVED

Need help building macro to find and replace multiple values in selected cells.

Copper Contributor

Hello everyone!

I have been trying to build some macro to find and replace multiple values in selected cells as below,

 Sub FR()

Dim rngCell As Range
Dim fndList As Variant
Dim rplcList As Variant
Dim F As Long

fndList = Array("United Kingdoms", "United States", "Australia")
rplcList = Array("UK", "US", "AUS")


For F = LBound(fndList) To UBound(fndList)

For Each rngCell In Selection
sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False

Next Rng

Next F

End Sub

 But I am having errors. Can you guys help this?

Many thanks,

Young

2 Replies
best response confirmed by cuong (Microsoft)
Solution

@Youngmrr Try it this way:

Sub FR()

Dim fndList As Variant
Dim rplcList As Variant
Dim F As Long
fndList = Array("United Kingdom", "United States", "Australia")
rplcList = Array("UK", "US", "AUS")

For F = 0 To UBound(fndList)
      Selection.Replace What:=fndList(F), Replacement:=rplcList(F), _
      LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
      SearchFormat:=False, ReplaceFormat:=False       
Next F
End Sub

@Riny_van_Eekelen It works!!! Thank you so much for your help! :)

1 best response

Accepted Solutions
best response confirmed by cuong (Microsoft)
Solution

@Youngmrr Try it this way:

Sub FR()

Dim fndList As Variant
Dim rplcList As Variant
Dim F As Long
fndList = Array("United Kingdom", "United States", "Australia")
rplcList = Array("UK", "US", "AUS")

For F = 0 To UBound(fndList)
      Selection.Replace What:=fndList(F), Replacement:=rplcList(F), _
      LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
      SearchFormat:=False, ReplaceFormat:=False       
Next F
End Sub

View solution in original post