Remove spaces before and after hyphen in text string

Scipio

New Member
Joined
Aug 11, 2020
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Consider the text string:

Imm Glob - Xembify Subcutaneous Solution 10 GM/50ML

This appears as a drug name in a cell in Excel.

I need a formula to remove the spaces before and after the hyphen that I can then copy down about 25,000 rows.

I have tried various forms of IF(ISNUMBER(SEARCH combined with concatenating with TEXTAFTER and/or TEXTBEFORE but cannot come up with a way to remove both the spaces.

Objective:

Imm Glob-Xembify Subcutaneous Solution 10 GM/50ML

Problem: same drug name appears in the database with space before the hyphen, no space before the hyphen, no space before the hyphen and space after...you get the picture --- totally inconsistent naming conventions.

It is wreaking havoc on my data analysis as my pivot table cannot properly group all records of the same drug due to the inconsistent use of text and spaces in the naming conventions.

Would be grateful for any suggestions.

Kind Regards,
Scipio
 
Do you need a formula copied down all those rows? Could you just use Excel's Find/Replace to replace " -" with "-" and then Find/Replace to replace "- " with "-" in the original column?

Otherwise, if you have the new REGEX functions you could try this

25 03 08.xlsm
AB
1
2Imm Glob - Xembify Subcutaneous Solution 10 GM/50MLImm Glob-Xembify Subcutaneous Solution 10 GM/50ML
3Imm Glob -Xembify Subcutaneous Solution 10 GM/50MLImm Glob-Xembify Subcutaneous Solution 10 GM/50ML
4Imm Glob- Xembify Subcutaneous Solution 10 GM/50MLImm Glob-Xembify Subcutaneous Solution 10 GM/50ML
5
Replace
Cell Formulas
RangeFormula
B2:B4B2=REGEXREPLACE(A2:A4," *\- *","-")
Dynamic array formulas.
 
Upvote 0
.. or if you don't have REGEX

25 03 08.xlsm
AB
1
2Imm Glob - Xembify Subcutaneous Solution 10 GM/50MLImm Glob-Xembify Subcutaneous Solution 10 GM/50ML
3Imm Glob -Xembify Subcutaneous Solution 10 GM/50MLImm Glob-Xembify Subcutaneous Solution 10 GM/50ML
4Imm Glob- Xembify Subcutaneous Solution 10 GM/50MLImm Glob-Xembify Subcutaneous Solution 10 GM/50ML
5
Replace
Cell Formulas
RangeFormula
B2:B4B2=SUBSTITUTE(SUBSTITUTE(TRIM(A2:A4)," -","-"),"- ","-")
Dynamic array formulas.
 
Upvote 0
Thanks Peter. I do need to copy down all the rows. I will give the new REGEX function a look.
Thanks again.
 
Upvote 0
Peter --- thank you. The SUBSTITUTE + TRIM Functions worked well. I'm going to study the new REGEX functions when time permits, but in the meantime, your suggestion has helped tremendously. Thanks again.

Kind Regards,
Scipio
 
Upvote 0
Scipio

=REGEXREPLACE(TRIM(A2:A4);{" - "," -","- "},"-")
Did you try that? For me (once the formula separators were made consistent) that produced three results for each item of sample data.


@Scipio
If you have looked at or are going to look at the REGEX options I did have an extra character in my post 2 suggestion. The extra character makes no difference to the working of the formula but is not necessary. The adjusted formula would be

Scipio.xlsm
AB
1
2Imm Glob - Xembify Subcutaneous Solution 10 GM/50MLImm Glob-Xembify Subcutaneous Solution 10 GM/50ML
3Imm Glob -Xembify Subcutaneous Solution 10 GM/50MLImm Glob-Xembify Subcutaneous Solution 10 GM/50ML
4Imm Glob- Xembify Subcutaneous Solution 10 GM/50MLImm Glob-Xembify Subcutaneous Solution 10 GM/50ML
Replace
Cell Formulas
RangeFormula
B2:B4B2=REGEXREPLACE(A2:A4," *- *","-")
Dynamic array formulas.
 
Upvote 0
Peter --- thank you. I haven't had time to look at it yet but will study your formula when time permits. Thank you again for the help with the Substitute function --- it made a huge difference in our ability to transform our raw data into the analysis we need. For many Excel users like me, text formulas often present complexities that can be more challenging than numerical calculations, so I really appreciate the help.
 
Upvote 0

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