# Extract variable length string from variable data

#### toyz4me

##### New Member
Hi, Could you please help with a formula to extract "P4/*"up to the comma "," from the following examples:

Data --------------------------------------- Output
P4/3H6, ----------------------------------- P4/3H6
G6/3H4,GEN/18A12,P4/3H4, ------------ P4/3H4
P4/2C1.1,SLD/DIST, --------------------- P4/2C1.1
CEN/REVMET,P4/2C8, ------------------- P4/2C8
G8/3H3,L9/3H3,P4/3H3,R111/3H3, ---- P4/3H3
P4/3H15,S14/3H15, --------------------- P4/3H15

I have used MID and FIND in this formula: =MID(E4,FIND("P4/",E4)*1,8) to extract 8 characters, but the majority of the required strings are 6, some are 7 and a few are 8. My knowledge of Excel is not good enough to find the length of the P4/ string up to the comma and only extract it. I have searched and looked at some similar extraction formula but can not get them to work properly for me e.g. =MID(E4,FIND("P4/",E4)*1,(FIND(",",E4)-(FIND("P4/",E4)*1)))

Any help would be much appreciated

### Excel Facts

To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try:
Code:
``=MID(E4,FIND("P4/",E4),FIND(",",MID(E4,FIND("P4/",E4),LEN(E4)))-1)``

Here is another formula you can consider...

=REPLACE(REPLACE(E4,FIND(",",E4,FIND("P4/",E4)),99,""),1,FIND("P4/",E4)-1,"")

Thanks gents, both formula work a treat and very much appreciated.

Replies
3
Views
213
Replies
1
Views
822
Replies
3
Views
3K
Replies
3
Views
553
Replies
9
Views
806

1,196,126
Messages
6,013,604
Members
441,777
Latest member

### 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.

### Which adblocker are you using?

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

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