-1

我在 excel 中有一个列,其中包含一个由“;”划分的连接字符串 例如

SM/123456789/1;PM/123456789/21;AM/123456789/1;GM/123456789/81;QM/123456789/1

我想返回第二个正斜杠的值,例如

1;21;1;81;1

注意:我将使用仅提取一个输入的最后一个“/ SM/123456789/199

IF(ISERROR(FIND("/",B19)),"",RIGHT(B19,LEN(B19)-FIND("/",B19,FIND("/",B19)+1)))

这将在 的情况下提取199或 1 SM/123456789/1。我如何实现这一目标?这里有数组公式的机会吗?

4

3 回答 3

2

如果您订阅了 Office 365 Excel,请使用以下数组公式:

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

作为一个数组公式,退出编辑模式时必须使用 Ctrl-Shift-Enter 而不是 Enter 来确认。如果正确完成,Excel 将{}围绕公式。

在此处输入图像描述


如果您没有订阅 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
于 2017-05-13T03:43:53.547 回答
0

如果您没有TEXTJOIN功能,您可以尝试以下方法,但这种方法需要帮助列,

在此处输入图像描述

单元格上的公式B1

=TRIM(RIGHT(SUBSTITUTE(TRIM(MID(SUBSTITUTE($A1,";",REPT(" ",999)),(COLUMN(A1)-1)*999+1,999)),"/",REPT(" ",999)),999))

您必须向右拖动它,直到column F (5 columns)输入字符串包含 5 个以 . 分隔的文本字符串;。最后添加一个公式来连接所有由,column G分隔的字符串;

=B1&";"&C1&";"&D1&";"&E1&";"&F1

于 2017-05-13T08:12:58.263 回答
0

这是一个带有正则表达式的 UDF,可以选择替换分隔符。

Option Explicit
Option Base 0    '<~~this is the default but I've included it because it has to be 0

Function mySplitString(str As String, _
                        Optional delim As String = ";")
    Dim n As Long, nums() As Variant
    Static rgx As Object, cmat As Object

    'with rgx as static, it only has to be created once; beneficial when filling a long column with this UDF
    If rgx Is Nothing Then
        Set rgx = CreateObject("VBScript.RegExp")
    End If

    mySplitString = vbNullString

    'make sure the str terminated in a semi-colon
    str = str & Chr(59)

    With rgx
        .Global = True
        .MultiLine = False
        .Pattern = "\d{1,3}[\;]{1}"
        If .Test(str) Then
            Set cmat = .Execute(str)
            'resize the nums array to accept the matches
            ReDim nums(cmat.Count - 1)
            'populate the nums array with the matches
            For n = LBound(nums) To UBound(nums)
                nums(n) = cmat.Item(n)
            Next n
            'convert the nums array to a delimited string
            If delim <> Chr(59) Then
                mySplitString = Replace(Join(nums, delim), Chr(59), vbNullString)
            Else
                mySplitString = Join(nums, vbNullString)
                mySplitString = Left(mySplitString, Len(mySplitString) - 1)
            End If
        End If
    End With
End Function

在 B2:B3 中用作,

=mySplitString(A2, ",")
=mySplitString(A3)

在此处输入图像描述

于 2017-05-13T04:53:16.840 回答