How to Replace All occurrences in Array?

Fractalis

Active Member
Joined
Oct 11, 2011
Messages
328
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Having a one dimensional VBA array, is possible to find a list of strings and Replace All occurrences of each string within array?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Yes it's possible but there is no shortcut. You have loop through each element of the array and have an inner loop to replace each string in the list. Or vice versa.
 
Upvote 0
Could you give us a bit more detail?
How big is the array?
How big is the list of strings to replace?
Could we have an example of such an array, the strings to replace, the replacement string(s) & the final resultant array?
 
Upvote 0
Yes it's possible but there is no shortcut. You have loop through each element of the array and have an inner loop to replace each string in the list. Or vice versa.
You do need a loop for each string but you don't need to loop through the data set.
This will work.
PS: I use it to remove non-breaking spaces (Application.Trim can handle a whole array without looping too)

VBA Code:
    arr = Application.Substitute(arr, "old_value", "new_value")
 
Upvote 2
you don't need to loop through the data set
Thanks for that, it's a new on one on me. When I type Application, Substitute is not available in Intellisense. I only get it by using Application.WorksheetFunction. I thought the first argument had to be a String. Did not know it would work on an array.
 
Upvote 0
@6StringJazzer, someone else suggested to me once to use WorksheetFunction while building the function so you get the intellisense but then swapping to just Application which can handle arrays and allows the use of IsError and IfError.
 
Upvote 0
Could you give us a bit more detail?
How big is the array?
How big is the list of strings to replace?
Could we have an example of such an array, the strings to replace, the replacement string(s) & the final resultant array?
Hi Peter_SSs,

The actual input has about 6K strings and list of strings to search has about 2k strings.

Below is a little example:

VBA Code:
Source = WorksheetFunction.Transpose(Range("A2:A20").Value)

ToSearch = WorksheetFunction.Transpose(Range("C2:C4").Value)
ToReplace = WorksheetFunction.Transpose(Range("D2:D4").Value)

Book1
ABCDEF
1inputSearchReplaceoutput
2applesapplesgrapesgrapes
3orangespeacheswatermelonoranges
4peacheslemonpearwatermelon
5applesgrapes
6applesgrapes
7applesgrapes
8orangesoranges
9orangesoranges
10peacheswatermelon
11mangomango
12lemonpear
13avocadoavocado
14avocadoavocado
15bananasbananas
16bananasbananas
17lemonpear
18lemonpear
19apricotapricot
20cherrycherry
Sheet1
 
Upvote 0
You do need a loop for each string but you don't need to loop through the data set.
This will work.
PS: I use it to remove non-breaking spaces (Application.Trim can handle a whole array without looping too)

VBA Code:
    arr = Application.Substitute(arr, "old_value", "new_value")
Thank you. This seems to be the shortest/fastest solution possible.
 
Upvote 0
Thanks for getting back to us. Glad we could help.
PS: Just be careful,this does not do whole word replacements so replacing apple will change both apple and pineapple
 
Upvote 0
This seems to be the shortest/fastest solution possible.
:unsure: I'm not so sure. With that method, if you have "pineapples" in the column A list, you will end up with "pinegrapes" in your array. Is that what you want? You did mention 'strings' in your question but I'm wondering if you really meant 'words'?

It is also unclear whether case-sensitivity might come into play with your real data. For example, if "Apples" was in column A that would not get replaced if the column C value is "apples" with the Application.Substitute suggestion. Can you clarify that issue too please?

One more: Is it true with your real data that the original list (column A) consists of single words only, or might some cells have multiple words? If multiple words are possible, would you want "red apples" to become "red grapes" or would the entire cell need to match a column C value?
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,161
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