Remove spaces before and after hyphen in text string

Scipio

New Member
Joined
Aug 11, 2020
Messages
27
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

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