Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Breaking Up Variable-Length Part Numbers.

Breaking Up Variable-Length Part Numbers

by Allen Wyatt
(last updated September 20, 2014)

3

Marty has a worksheet with a long series of part numbers in column A. These consist of letters and numbers, such as A123BC, AB123C, etc. Marty wants to break the data into three columns, so that text before the numbers will be in one column, numbers in the second column, and text after the numbers in the third.

The factor that complicates dividing the part number into segments is that there is no set length for each component of the combined part number. If the components were of standard lengths, then you could use the Text to Columns function in Excel. Since they aren't, and there is no delimiter between the components, then that potential avenue for solution is closed.

If you want to use formulas to pull apart the part numbers, then you will need three of them, one for each component you want to extract. Assuming that the part numbers follow the pattern indicated (text, digits, text) and that the first part number is in cell A1, you could use the following in cell B1:

=LEFT(A1,MATCH(FALSE,ISERROR(1*MID(A1,ROW(INDIRECT("1:100")),1)),0)-1)

This needs to be entered as an array formula, meaning that you need to enter it using Ctrl+Shift+Enter. The formula finds the first numeric digit in the part number, and then returns everything before that digit. It will work on any part number that isn't over 100 characters in length.

To extract the second component of the part number, you can put the following formula in cell C1:

=MID(A1,MATCH(FALSE,ISERROR(1*MID(A1,ROW(INDIRECT("1:100")),
1)),0),COUNT(1*MID(A1,ROW(INDIRECT("1:100")),1)))

Again, this is a single formula, and it needs to be entered as an array formula (Ctrl+Shift+Enter) so that it can work on each character in the original part number. It examines the part number and determines the beginning point of the digits, and then extracts all those digits. It returns a text string, even though that string is composed of digits. If you want it to actually be treated as a number (which would get rid of any leading zeros, of course), then you need to enclose the entire formula in a Value function, as shown here:

=VALUE(MID(A1,MATCH(FALSE,ISERROR(1*MID(A1,ROW(INDIRECT("1:100")),
1)),0),COUNT(1*MID(A1,ROW(INDIRECT("1:100")),1))))

To get the last component of the part number, you need to use the following formula, again entered as an array formula:

=MID(A1,MATCH(FALSE,ISERROR(1*MID(A1,ROW(INDIRECT("1:100")),
1)),0)+COUNT(1*MID(A1,ROW(INDIRECT("1:100")),1)),100)

While this approach works very well, array formulas are notoriously calculation intensive, especially when you have a lot of the formulas in your worksheet. If you need to pull apart a thousand part numbers, that means that you end up with 3,000 array formulas, which can be very, very slow in recalculating.

If you find yourself in this situation, you may want to use a macro to actually pull apart the part numbers. The following macro should work on part numbers that follow the pattern of text, digits, text, as already described.

Sub Split1()
    Dim C As Range
    Dim sNew As New
    Dim i As Integer
    
    For Each C In Selection
        sNew = ""
        i = 1

        ' Get first part, which is text
        Do While IsNumeric(Mid(C, i, 1)) = False
            sNew = sNew & Mid(C, i, 1)

            i = i + 1
            If i > Len(C) Then Exit Do
        Loop
        C.Offset(0, 1).Value = sNew

        ' Pull next part, which should be digits
        sNew = ""
        Do While IsNumeric(Mid(C, i, 1)) = True
            sNew = sNew & Mid(C, i, 1)

            i = i + 1
            If i > Len(C) Then Exit Do
        Loop
        C.Offset(0, 2).Value = sNew

        ' Use rest of original cell
        sNew = Mid(C, i, Len(C))
        C.Offset(0, 3).Value = sNew
    Next C
End Sub

To use the macro, just make a selection of part numbers and run it. The macro uses the concept of looking for changes between numeric/nonnumeric values in string of characters in the cell. When one of these boundaries is reached, the part of the original string before the boundary is stuffed into a new cell. This concept can be shortened a bit, as is done in the following example.

Sub Split2()
    Dim C As Range
    Dim j As Integer
    Dim k As Integer

    For Each C In Selection
        j = 1
        Do While Not (IsNumeric(Mid(C.Value, j, 1))) And j <= Len(C)
            j = j + 1
        Loop
        k = j

        Do While IsNumeric(Mid(C.Value, k, 1)) And k <= Len(C)
            k = k + 1
        Loop

        C.Offset(0, 1) = Left(C, j - 1)
        C.Offset(0, 2) = Mid(C, j, k - j)
        C.Offset(0, 3) = Mid(C, k, Len(C) - (k - 1))
    Next C
End Sub

The difference between this version of the macro and the previous one, of course, is that this version steps through the original cell and determines the boundaries all at once. When they are known, then the components of the original part number are stuffed into the cells.

An interesting approach to pulling apart the part numbers is to use a couple of short user-defined functions that determine where the boundaries are between the components. Consider the following two functions:

Function pNumber(X)
    i = 1
    Do Until Mid(X, i, 1) Like "#": i = i + 1: Loop
    pNumber = i
End Function
Function pAlpha(X)
    X = UCase(X)
    i = pNumber(X)
    Do Until Mid(X, i, 1) Like "[A-Z]": i = i + 1: Loop
    pAlpha = i
End Function

These are much shorter than the previous macros, and all they do is return the boundary where the numbers start (in the case of pNumber) and the boundary where the second group of text starts (in the case of pAlpha). To use the functions, you use the following three formulas to return, respectively, the first, second, and third components of the original part number:

=MID(A1,1,pNumber(A1)-1)
=MID(A1,pNumber(A1),pAlpha(A1)-pNumber(A1))
=MID(A1,pAlpha(A1),LEN(A1)-pAlpha(A1)+1)

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10369) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Breaking Up Variable-Length Part Numbers.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Copying and Moving Footnotes and Endnotes

If you need to move footnotes or endnotes from one location to another in a document, you can use editing techniques you ...

Discover More

Comments Don't Appear when Cell is Pointed To

One way that you can view comments in a worksheet is to have them appear when you hover the mouse pointer over a cell. If ...

Discover More

Creating Thin Spaces

Thin spaces are a typographic device that allows you add a bit of space between elements of a document. There are no thin ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Combining Numbers and Text in a Cell

There are times when it can be beneficial to combine both numbers and text in the same cell. This can be easily done using a ...

Discover More

Moving the House Number to Its Own Cell

Excel is great at manipulating data, but sometimes it is difficult to figure out the best way to do the manipulation. This ...

Discover More

Converting from Relative to Absolute

Addresses used in a formula can be either relative or absolute. If you need to switch between the two types of addressing, ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 4?

2017-07-06 17:20:05

Unknown

=IF(R13<>0,IF(Q13<>0,R13/Q13,IF(R13>0,1,-1)),0)

Can you please show me how to read the formula above? Thanks!


2014-09-20 09:51:18

Willy Vanhaelen

If you prefer the macro approach, here is a shorter version based on For Next loops instead of Do While loops:

Sub Split3()
Dim A As Integer, B As Integer, C As Range
For Each C In Selection
For A = 1 To Len(C)
If IsNumeric(Mid(C, A, 1)) Then Exit For
Next A
For B = A + 1 To Len(C)
If Not IsNumeric(Mid(C, B, 1)) Then Exit For
Next B
C.Offset(0, 1) = Left(C, A - 1)
C.Offset(0, 2) = Mid(C, A, B - A)
C.Offset(0, 3) = Mid(C, B)
Next C
End Sub


2014-09-20 09:31:10

Michael (Micky) Avidan

With the P/N in A1 - I would suggest the following:
* Starting with an Array Formula in C1:
=-LOOKUP(,-MID(A1,MIN(FIND(ROW($1:$10)-1,A1&"1234567890")),ROW($1:$256)))
* In B1: =MID(A1,1,FIND(C1,A1)-1)
* In D1: =SUBSTITUTE(A1,B1&C1,"")
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2015)
ISRAEL


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.