Solved

# Extract character string from cell in excel

Posted on 2010-11-24
Medium Priority
684 Views
I have text strings from each of which I need to extract a specific character string.  The character string I need to extract always has the same number of characters however the original text strings are not all the same length. An example text string is below.  The segment - Serial number: - is always the same.  The string I need to extract is the 5 digits following Serial number:  There is always a ; following the 5 digit serial number which I do not wish to include.

Equipment QD5fj425;   Serial number: 93222;   Firmware: V 4.47 - 11/08 AV;   QD5 Version: V 6.01

Any help is appreciated. Thank you.
0
Question by:Dave

LVL 24

Accepted Solution

StephenJR earned 2000 total points
ID: 34206835
This works for your example, but does rely on there being colon space after serial number.

=MID(A1,FIND("Serial number",A1)+15,5)
0

LVL 2

Expert Comment

ID: 34206887
Considering you have the original text string in A1, and the segment string ("Serial number") in B1, the formula you are looking for in C1 is =LEFT(RIGHT(A1,LEN(A1)-FIND(B1,A1)-LEN(B1)),FIND(";",RIGHT(A1,LEN(A1)-FIND(B1,A1)-LEN(B1)))-1)
A step by step process to reach this would be:
1. Write the string you need to parse in A1, and the string that identifies the segment in B1
A1 = "Equipment QD5fj425;   Serial number: 93222;   Firmware: V 4.47 - 11/08 AV;   QD5 Version: V 6.01"
B1 = "Serial number:" (the idea is to identify this segment position and find yourself at the beginning of the number you need to extract)

2. Identify the position of the segment in D1
D1 = FIND(B1,A1)
3. Trim the original string removing the text from the beginning to the segment position, including the segment. Have that in E4.
E1 = RIGHT(A1,LEN(A1)-D1-LEN(B1)) - returns the right side of the string having a length equal with the total length of the string minus the position at which the segment begins minus the segment length. )
E1 result should be 93222;   Firmware: V 4.47 - 11/08 AV;   QD5 Version: V 6.01
4. In F1, use the previous returned result to find the delimiter and return the left side of the string until delimiter position
F1=LEFT(E1,FIND(";",E1)-1)                 (returns 93222)

To reach the result provided as an answer, if you need the whole formula in a single string, just recurrently replace the computed cells with the formula contained in them until you base your formula only on A1 and B1.
You can also change B1 with the actual value you are looking for to use a single formula and a hardcoded string, but that won't be so easy to maintain.
B1 = LEFT(RIGHT(A1,LEN(A1)-FIND("Serial number:",A1)-LEN("Serial number:")),FIND(";",RIGHT(A1,LEN(A1)-FIND("Serial number:",A1)-LEN("Serial number:")))-1)
0

LVL 13

Expert Comment

ID: 34206988
=LEFT(MID(A1,FIND("Serial number: ",A1)+LEN("Serial number:")+1,LEN(A1)),5)
for a fixed length of 5
or
=LEFT(MID(A1,FIND("Serial number: ",A1)+LEN("Serial number:")+1,LEN(A1)),FIND("|",SUBSTITUTE(A1,";","|",2))-(FIND("Serial number: ",A1)+LEN("Serial number:")+1))
for a variable length
0

LVL 45

Expert Comment

ID: 34207179
dcruickshank,

The User Defined Function (UDF) below is in the attached file.

Usage: =sno(A1)

where A1 is the cell containing the original string.

The UDF will cope with Serial Numbers up to 6 digits in length.

Hope that helps.

Patrick
``````Function sno(ByVal rng As Range) As Long
Dim str1() As String

str1 = Split(rng, ";", , vbTextCompare)
sno = --Trim(Right(str1(1), 6))

End Function
``````
serial-no-extract-01.xls
0

Author Closing Comment

ID: 34207277
Thank you very much! Works perfectly
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Debits & Credits have been the foundation of financial record keeping since 1494 - over 500 years. Excel is a brilliant tool for leveraging this ancient power - not least with Pivot Tables, sorting and filtering.  This article seeks by illustration …
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
###### Suggested Courses
Course of the Month6 days, 1 hour left to enroll