You do need a loop for each string but you don't need to loop through the data set.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.
arr = Application.Substitute(arr, "old_value", "new_value")
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.you don't need to loop through the data set
Hi Peter_SSs,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?
Source = WorksheetFunction.Transpose(Range("A2:A20").Value)
ToSearch = WorksheetFunction.Transpose(Range("C2:C4").Value)
ToReplace = WorksheetFunction.Transpose(Range("D2:D4").Value)
Book1 | ||||||||
---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | |||
1 | input | Search | Replace | output | ||||
2 | apples | apples | grapes | grapes | ||||
3 | oranges | peaches | watermelon | oranges | ||||
4 | peaches | lemon | pear | watermelon | ||||
5 | apples | grapes | ||||||
6 | apples | grapes | ||||||
7 | apples | grapes | ||||||
8 | oranges | oranges | ||||||
9 | oranges | oranges | ||||||
10 | peaches | watermelon | ||||||
11 | mango | mango | ||||||
12 | lemon | pear | ||||||
13 | avocado | avocado | ||||||
14 | avocado | avocado | ||||||
15 | bananas | bananas | ||||||
16 | bananas | bananas | ||||||
17 | lemon | pear | ||||||
18 | lemon | pear | ||||||
19 | apricot | apricot | ||||||
20 | cherry | cherry | ||||||
Sheet1 |
Thank you. This seems to be the shortest/fastest solution possible.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")
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'?This seems to be the shortest/fastest solution possible.