简体   繁体   English

通过分隔符按计数 excel 添加数字来拆分和连接字符串

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

I have this in thousands of excel cells我在数千个 excel 单元格中有这个

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.所以第一个是 15、30 和 30 的 3 个个性化,第二个是 12、5、16、25 和 52 的 5 个个性化。

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.工作表中的每个产品都被转换为 csv 以进行上传我想使用标签来确定需要多少个最大长度的表单字段,但是我不能两次使用相同的标签。 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.如果没有这个,将是 12,30,30 两次使用相同的标签,这是上传网站无法完成的。

But I have no clue in relation to VBA and limited knowledge of functions但我对 VBA 一无所知,对函数的了解也有限

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 =LEN(A2)-LEN(SUBSTITUTE(A2,"x",""))+1结果是 3

I can also replace the x with a comma using the substitute function but how can I as follows:我也可以使用替代函数用逗号替换 x 但我怎么做如下:

  • Split拆分
  • Add the increment添加增量
  • Rejoin重新加入

What would be the best way to achieve this?实现这一目标的最佳方法是什么? Functions in a cell ???单元格中的函数??? A VBA function ??? VBA 函数??? 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如果可以的话,有人可以给我一个入门或一些函数来看看我是一名 php 程序员,所以可以在需要时学习新代码

I php I would split the string to the array .我 php 我会将字符串拆分为数组。 . . .prepend each value in the array with the increment and then join as a string again but how can I do similar in excel. . 将数组中的每个值加上增量,然后再次作为字符串加入,但我如何在 excel 中做类似的事情。 Any help would be appreciated任何帮助将不胜感激

If you have Office 365 Excel then you can use TEXTJOIN as an array formula:如果您有 Office 365 Excel,则可以使用 TEXTJOIN 作为数组公式:

=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.作为数组公式,退出编辑模式时需要使用 Ctrl-Shfit-Enter 而不是 Enter 进行确认。

在此处输入图片说明


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.如果您没有 Office 365 Excel,则可以将此代码放入附加到工作簿的模块中,并使用上述公式。

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:如果有 Excel O365,(或 Excel 2019 对以下公式进行了一些小补充),您还可以使用:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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