Adding additional substitute to substitute formula

pluginguin

New Member
Joined
Aug 10, 2016
Messages
21
Office Version
  1. 2019
Platform
  1. Windows
I have a formula that replaces all spaces in a string with a dash "-".
The problem with this is, is that when the original string already contains a dash between spaces " - " the endresult now is "---"
I would like to modify my formula to prevent this from happening and in those cases just remove the spaces.

Example string: "Trampolines - A great way to train your equilibrium"

Current formula:
Excel Formula:
=SUBSTITUTE(D71," ","-")

Current result: "Trampolines---A-great-way-to-train-your-equilibrium"

Desired result: "Trampolines-A-great-way-to-train-your-equilibrium"
How to modify the formula?
 

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.
Hi, how about:

Excel Formula:
=SUBSTITUTE(SUBSTITUTE(D71," - ","-")," ","-")
 
Upvote 0
Solution
How about:
VBA Code:
        =SUBSTITUTE(Application.Trim(D71)," ","-")
It will reduce multiple " " to a single " " first.
 
Upvote 0
How about:
VBA Code:
        =SUBSTITUTE(Application.Trim(D71)," ","-")
It will reduce multiple " " to a single " " first.
Hi Alex, this formula didn't do anything for my problem. I don't have multiple spaces... I had a dash between 2 spaces. When the spaces get replaced by a dash I get 3 dashes. FormR's (simple) solution works!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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