简体   繁体   中英

Split and join a string by delimiter adding a number as per count excel

I have this in thousands of excel cells

15x30x30

12x5x16x25x52

Each Number is the maximum characters allowed for each personalization on a product.

So the first one is 3 personalizations of 15, 30 and 30 the second is 5 personalizations of 12, 5, 16, 25 and 52.

I want to change the above into this

1-12, 2-30, 3-30

Each product in the sheet is being converted to csv for upload I want to use tags to identify how many form fields of max length are needed however I cannot use the same tag twice. The prepended number is incrementing based on the count

Without this it would be 12,30,30 using the same tag twice which cannot be done by the upload site.

But I have no clue in relation to VBA and limited knowledge of functions

For Example

I can count the how many items split by delimeter there are as follows:

=LEN(A2)-LEN(SUBSTITUTE(A2,"x",""))+1 The result would be 3

I can also replace the x with a comma using the substitute function but how can I as follows:

  • Split
  • Add the increment
  • Rejoin

What would be the best way to achieve this? Functions in a cell ??? A VBA function ??? and if so can someone give me a starter or a few functions to look at I am a php programmer so can learn new code if needed

I php I would split the string to the array . . .prepend each value in the array with the increment and then join as a string again but how can I do similar in excel. Any help would be appreciated

If you have Office 365 Excel then you can use TEXTJOIN as an array formula:

=TEXTJOIN(", ",TRUE,ROW(INDIRECT("1:" & LEN(A1)-LEN(SUBSTITUTE(A1,"x",""))+1)) & "-" & TRIM(MID(SUBSTITUTE(A1,"x",REPT(" ",999)),(ROW(INDIRECT("1:" & LEN(A1)-LEN(SUBSTITUTE(A1,"x",""))+1)) -1)*999+1,999)))

Being an array formula it needs to be confirmed with Ctrl-Shfit-Enter instead of Enter when exiting Edit mode.

在此处输入图片说明


If you do not have Office 365 Excel then you can put this code in a module attached to the workbook and use the formula as described above.

Function TEXTJOIN(delim As String, skipblank As Boolean, arr)
    Dim d As Long
    Dim c As Long
    Dim arr2()
    Dim t As Long, y As Long
    t = -1
    y = -1
    If TypeName(arr) = "Range" Then
        arr2 = arr.Value
    Else
        arr2 = arr
    End If
    On Error Resume Next
    t = UBound(arr2, 2)
    y = UBound(arr2, 1)
    On Error GoTo 0

    If t >= 0 And y >= 0 Then
        For c = LBound(arr2, 1) To UBound(arr2, 1)
            For d = LBound(arr2, 1) To UBound(arr2, 2)
                If arr2(c, d) <> "" Or Not skipblank Then
                    TEXTJOIN = TEXTJOIN & arr2(c, d) & delim
                End If
            Next d
        Next c
    Else
        For c = LBound(arr2) To UBound(arr2)
            If arr2(c) <> "" Or Not skipblank Then
                TEXTJOIN = TEXTJOIN & arr2(c) & delim
            End If
        Next c
    End If
    TEXTJOIN = Left(TEXTJOIN, Len(TEXTJOIN) - Len(delim))
End Function

If one has Excel O365, (or Excel 2019 with some small addition to the below formula) you could also use:

=TEXTJOIN(", ",,SEQUENCE(LEN(A1)-LEN(SUBSTITUTE(A1,"x",""))+1)&"-"&FILTERXML("<t><s>"&SUBSTITUTE(A1,"x","</s><s>")&"</s></t>","//s"))

在此处输入图片说明

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