简体   繁体   中英

How to split a word whereever there's a number using MS Excel Delimiter

So I have a list of combinations as mentioned below which I needed to be split with an alphabet and a number following it.

组合式

I want it to be split as below using MS Excel; Text to Columns Delimit function or any other efficient function-

分裂

I tried using Fixed Width which doesn't work if there are 2 or more digits numeric value after the alphabetical character. Is there a way to do this as I have to repeat this for a 13000 combinations?

Since the Text are constant, create a header row of the constants and with your values starting in A1, put this formula in B2 and copy over and down:

=MID($A2,FIND(B$1,$A2),IF(ISNUMBER(--MID($A2,FIND(B$1,$A2)+2,1)),3,2))

在此处输入图片说明

Insert a delimiter character where ever the pattern is found then split the modified string into new columns.

Option Explicit

Sub makeSplit()

    Dim i As Long, j As Long, str As String, arr As Variant

    'set the worksheet
    With Worksheets("sheet5")

        'step throughj the cells in column A
        For i = .Cells(.Rows.Count, "A").End(xlUp).Row To 2 Step -1

            'put the cell value into a variable
            str = .Cells(i, "A").Value2

            'step through the characters in the sting backwards
            For j = Len(str) - 1 To 3 Step -1

                'is the current character alphabetic and the previous character a digit?
                If Not IsNumeric(Mid(str, j, 1)) And IsNumeric(Mid(str, j - 1, 1)) Then

                    'insert a space
                    str = Left(str, j - 1) & Space(1) & Mid(str, j)

                End If

            Next j

            'split the string using the space as delimiter
            .Cells(i, "A").Resize(1, UBound(Split(str, Space(1))) + 1) = Split(str, Space(1))

        Next i

    End With

End Sub

I found a way but is going to be resource intensive.

First have in a sheet all your strings in a column, and make sure you have columns free right to that column.

Then make a new, empty, first row, and put in the letters R, C, P, D, O, S, since you said they are always in that order. These are the recognition letters, needed for the formula.

Now under each letter, right of each string, put in the formula

=C$1&MAX(IFERROR(VALUE(MID($A2,SEARCH(C$1,$A2)+1,2)),0),VALUE(MID($A2,SEARCH(C$1,$A2)+1,1)),0)

This works only if the numbers are no longer then 2 digits, for three it would be

=C$1&MAX(IFERROR(VALUE(MID($A2,SEARCH(C$1,$A2)+1,3)),0),IFERROR(VALUE(MID($A2,SEARCH(C$1,$A2)+1,2)),0),VALUE(MID($A2,SEARCH(C$1,$A2)+1,1)),0)

For four, keep on adding an IFERROR() with the )),2) )),3) increasing like above.

Once all the formulas have evaluated, you can copy and paste as values if you want to get rid of the formulas.

Example sheet (Dutch Excel):

例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM