"SUBSTITUTE" Function With Wilcard For Symbols?

Dhira

Board Regular
Joined
Feb 23, 2006
Messages
85
Office Version
  1. 2021
Platform
  1. Windows
In trying to create a formula that remove EVERY symbol from a cell (C6) and replacing it with a space.... the "SUBSTITUTE" function is telling me I have too many nested Substitutes.

This is the formula:
Code:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE([COLOR=Red]C6[/COLOR],"/"," "),"%"," "),"!"," "),","," "),"*"," "),"-"," "),"("," "),")"," ")

It works thus far, but I would still like to add more symbols into the nest.
Is there a way/formula to do this that replaces ANY symbol with a space?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
perhaps a udf such as this:
Excel Workbook
CD
18P/()-*J,!%DP J D
Sheet


backed up by a function:
Code:
Function RemoveStuff(FromWhatString, CharactersToRemove, CharactersToPutInstead)
For i = 1 To Len(CharactersToRemove)
  FromWhatString = Replace(FromWhatString, Mid(CharactersToRemove, i, 1), CharactersToPutInstead, , , vbTextCompare)
Next i
RemoveStuff = FromWhatString
End Function
(Excel Jeanie or this site seems to absorb the spaces in D18)
 
Last edited:
Upvote 0
p45cal,

How would I set that function in the spreadsheet?
Thanks.
 
Upvote 0
In trying to create a formula that remove EVERY symbol from a cell (C6) and replacing it with a space.... the "SUBSTITUTE" function is telling me I have too many nested Substitutes.

This is the formula:

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(C6,"/"," "),"%"," "),"!"," "),","," "),"*"," "),"-"," "),"("," "),")"," ")

It works thus far, but I would still like to add more symbols into the nest.
Is there a way/formula to do this that replaces ANY symbol with a space?
If you're using Excel 2003 or earlier then you can only nest 8 functions as you have discovered.

Not the optimal solution but one that will work...

Enter the above formula in a cell then refer to that cell in another formula that removes additional characters.

Someone may be able to come up with a UDF that can do it but you'd have to let them know EXACTLY what all of the characters are to be removed or, what characters are to be saved.
 
Upvote 0
p45cal,

How would I set that function in the spreadsheet?
Thanks.

I've shown the formula you would use in D18.
To add the code, press Alt+F11, then using the menus, Insert|Module, paste the code where the flashing cursor is.
Go back to the sheet, recalculate or re-enter the formula. If it gives the right answer, save the workbook and close the newly opened vb editor.
If you get an error Variable not defined, either remove Option Explicit from the top of the module or add
Dim i as Long
as the first line under Function....

If, on the sheet when you're entering the formula, you click on fx on the formula bar, it will be more obvious what to enter.
 
Last edited:
Upvote 0
Try using

Code:
Function ReplaceSpecial(ByVal Txt As String) As String
With CreateObject("VBScript.RegExp")
    .Pattern = "[^A-Z\s0-9]+"
    .Global = True
    .IgnoreCase = True
    ReplaceSpecial = Application.Trim(.Replace(Txt, ""))
End With
End Function


Formula cell D18
=SUBSTITUTE(ReplaceSpecial(C18)," ","")

Biz
 
Upvote 0
Dhira,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
If you have the morefunc add-in you could use regular expressions. The following formula will remove all characters that are not a number, letter or space.<o:p></o:p>
<o:p></o:p>
=REGEX.SUBSTITUTE(C6,"[^ \w]"," ")<o:p></o:p>
<o:p></o:p>
Thank you,<o:p></o:p>
<o:p></o:p>
Joseph Marro<o:p></o:p>
 
Upvote 0
p45cal & Biz,

Thank you. That did work, however I had to remove the function because I kept on getting a Privacy warning every time I tried to save - incredibly annoying It wouldn't go away even when I changed settings in the trust center (Excel 2007) SO I had to try Joseph's :
Dhira,<!--?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /--><o:p></o:p>
<o:p></o:p>
If you have the morefunc add-in you could use regular expressions. The following formula will remove all characters that are not a number, letter or space.<o:p></o:p>
<o:p></o:p>
=REGEX.SUBSTITUTE(C6,"[^ \w]"," ")<o:p></o:p>
<o:p></o:p>
Thank you,<o:p></o:p>
<o:p></o:p>
Joseph Marro<o:p></o:p>
And it works a treat.
(Had to download morefunc at DOwnload.com since it wasn't available)

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,176
Members
452,893
Latest member
denay

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