Modify and Extract Text ( Substitute)

nburaq

Board Regular
Joined
Apr 2, 2021
Messages
222
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi Gents,
I have table shown below and I would like to format the text shown in column A like in column B. I have done this by extremely crazy formula took almost 1 hour to do it but I am pretty sure there are some clever guys who can do this in a shorter way ! PS: be careful with quotation marks in cells :) also words before numbers may vary ( I have managed to do it with two words before number starts)
Good Luck Gents!

sample.xlsx
AB
1TextMod Text
2Line 2-NC-81-806-D212"-NC-81-806-D21
3Gas Line 4"-NG-81-003-F83S4"-NG-81-003-F83S
Sheet1
Cell Formulas
RangeFormula
B2:B3B2=IF(COUNTIF(A2,"*"&CHAR(34)&"*")=0,SUBSTITUTE(IF(LEN(A2)-LEN(SUBSTITUTE(A2," ",""))=1,MID(TRIM(A2),1+FIND("~",SUBSTITUTE(TRIM(A2)," ","~",1)),255),MID(TRIM(A2),1+FIND("~",SUBSTITUTE(TRIM(A2)," ","~",2)),255)),"-","""-",1),MID(TRIM(A2),1+FIND("~",SUBSTITUTE(TRIM(A2)," ","~",2)),255))
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How about
Excel Formula:
=FILTERXML("<k><m>"&SUBSTITUTE(IF(ISNUMBER(FIND(CHAR(34),A2)),A2,SUBSTITUTE(A2,"-","""-",1))," ","</m><m>")&"</m></k>","//m[position()=last()]")
 
Upvote 0
A bit shorter,
Book1
AB
1TextMod Text
2Line 2-NC-81-806-D212"-NC-81-806-D21
3Gas Line 4"-NG-81-003-F83S4"-NG-81-003-F83S
Sheet1
Cell Formulas
RangeFormula
B2:B3B2=SUBSTITUTE(SUBSTITUTE(TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",100)),100)),"""-","-"),"-","""-",1)
 
Upvote 0
Solution
A bit shorter,
Book1
AB
1TextMod Text
2Line 2-NC-81-806-D212"-NC-81-806-D21
3Gas Line 4"-NG-81-003-F83S4"-NG-81-003-F83S
Sheet1
Cell Formulas
RangeFormula
B2:B3B2=SUBSTITUTE(SUBSTITUTE(TRIM(RIGHT(SUBSTITUTE(A2," ",REPT(" ",100)),100)),"""-","-"),"-","""-",1)
Thanks for the solutions both method worked very well. except one thing I dont know why quotation marks sometimes appears double like 4"" for some rows. Is it possible to help me for this problem as well? Thanks again!
 
Upvote 0
sometimes appears double like 4"" for some rows. Is it possible to help me for this problem as well?
Not without seeing it. The formula was based on the example provided, if the results are inconsistent then it would suggest that there is an error or inconsistency in the data that we do not know about.
 
Upvote 0
Maybe they are a pair of apostrophes, try this
Excel Formula:
=FILTERXML("<k><m>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"""-","-"),"-","""-",1),"''","")," ","</m><m>")&"</m></k>","//m[position()=last()]")
 
Upvote 0
Maybe they are a pair of apostrophes,
Good point, I was thinking 0 width space between the original " and - as a possibility, hadn't thought of it being 2 singles instead.
 
Upvote 0
I was thinking 0 width space between the original " and -
Yup, that's another possibility We'll just have to wait for the OP to supply some more examples.
 
Upvote 0
Good point, I was thinking 0 width space between the original " and - as a possibility, hadn't thought of it being 2 singles instead.
Gentlemen's,
Once again what a great community and people. I am really happy to be a member of this forum! Good news, both formulas work perfect. I have checked the formulas on a clean page again and no issue so far. Thanks again for your help and concern.
 
Upvote 0
Glad it's sorted & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,813
Members
449,469
Latest member
Kingwi11y

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