Nested Substitution functions Simplified in to a Single Substitution function?

MEUserII

Board Regular
Joined
Oct 27, 2017
Messages
91
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
Platform
  1. Windows
I have the following formula enter in to "A2": =SUBSTITUTE( (SUBSTITUTE( ("$GR$1"), (1), ("") )), ("$"), ("") ) ; is there a way to make both nested substitutions of "1" and "$" with a single substitute function?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
$GR$1 is: =ADDRESS( (ROW() ), (COLUMN() ), (1) ) ; I am using this as a test to see how to combine nested Substitution functions in to one single Substitution function.

Anyone have a solution?
 
Last edited:
Upvote 0
You need to nest them.

You could write your own function in VBA to do that, though. Not sure it's worth the trouble.
 
Upvote 0
You need to nest them.

You could write your own function in VBA to do that, though. Not sure it's worth the trouble.
Got it, to make sure I understand, you mean by writing my own function you mean to write a user defined function (UDF)?
 
Upvote 0
.. to write a user defined function (UDF)?
I believe that is what shg meant. I had been playing with this one. It uses Regular Expressions which has special meanings for some characters (eg $ sign) so I haven't tested this thoroughly & it may not be that robust.

It does require that all values being searched for are replaced with the same text (but that can be something other than the null string "" if you want) and it does give the option of searching in a case-sensitive way if you want. See examples of its use below. The values being searched for must be separated by the "|" character and as the function stands, that character cannot be one of the ones searched for.

Code:
Function SubstituteMultiple(s As String, sFindVals As String, Optional sReplacement As String = "", Optional bMatchCase As Boolean = True) As String
  Static RX As Object
  
  If RX Is Nothing Then
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
  End If
  RX.Pattern = "([^a-zA-Z0-9\|])"
  RX.Pattern = RX.Replace(sFindVals, "\$1")
  RX.IgnoreCase = Not bMatchCase
  SubstituteMultiple = RX.Replace(s, sReplacement)
End Function

Excel Workbook
AB
1$GR$1GR
2$GR$1@GR@@
3Bob%ob
4Bob%o%
5Catt
6This is a longer than three wordsThvowels vowels vowel lvowelngvowelr thvoweln thrvowelvowel wvowelrds
7This is another exampleThis is anoyyer exyy
Sheet1
 
Last edited:
Upvote 0
This looks like a small part of a bigger spreadsheet. If you're interested, you may get a different/better answer if some more information/context is provided. regards
 
Upvote 0
I was thinking of something much more simpleminded:

[Table="width:, class:grid"][tr][td="bgcolor:#C0C0C0"][/td][td="bgcolor:#C0C0C0"]
A​
[/td][td="bgcolor:#C0C0C0"]
B​
[/td][td="bgcolor:#C0C0C0"]
C​
[/td][/tr][tr][td="bgcolor:#C0C0C0"]
2​
[/td][td]$GR$1[/td][td="bgcolor:#E5E5E5"]GR[/td][td]B2: =MSub(A2, 1, "", "$", "")[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
3​
[/td][td]
13579​
[/td][td="bgcolor:#E5E5E5"]24680[/td][td]B3: =MSub(A3, 1,2,3,4,5,6,7,8,9,0)[/td][/tr]
[/table]


Code:
Function MSub(s As String, ParamArray FindRepl() As Variant) As String
  Dim i As Long
  
  MSub = s
  For i = 0 To UBound(FindRepl) Step 2
    MSub = Replace(MSub, FindRepl(i), FindRepl(i + 1))
  Next i
End Function
 
Upvote 0

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

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