Simulate string split function in Excel formula

后端 未结 8 1740
难免孤独
难免孤独 2020-12-09 09:15

I am trying to split a string in an excel formula, something like I can do in many programming languages, e.g.

string words = \"some text\".split(\' \');
         


        
8条回答
  •  旧时难觅i
    2020-12-09 09:30

    Some great worksheet-fu in the other answers but I think they've overlooked that you can define a user-defined function (udf) and call this from the sheet or a formula.

    The next problem you have is to decide either to work with a whole array or with element.

    For example this UDF function code

    Public Function UdfSplit(ByVal sText As String, Optional ByVal sDelimiter As String = " ", Optional ByVal lIndex As Long = -1) As Variant
        Dim vSplit As Variant
        vSplit = VBA.Split(sText, sDelimiter)
        If lIndex > -1 Then
            UdfSplit = vSplit(lIndex)
        Else
            UdfSplit = vSplit
        End If
    End Function
    

    allows single elements with the following in one cell

    =UdfSplit("EUR/USD","/",0)

    or one can use a blocks of cells with

    =UdfSplit("EUR/USD","/")

提交回复
热议问题