How to made custom Split array function in MS Excel?

穿精又带淫゛_ 提交于 2021-01-02 04:03:55

问题


How to make a custom made function Split(str as String, delimiter as String ) using Split() in VBA to be used in worksheet.. and it will return as array or SPILL the result.. For example,

Split("{20;30;40;50;60}",";")

will return Spill down Array result in the same array as in the written formula as:

20
30
40
50
60

I try to use the Split(String,";") in VBA function but it only return 1 value and in text type.. I also need to remove both ' { ' and ' } ' in the string if there but accept it even if it is no there..


回答1:


  1. Do not call your function Split
  2. Use Nested Replace
  3. We need to return a String array.

Function mysplit(str As String, deli As String) As String()
    mysplit = split(Replace(Replace(str, "{", ""), "}", ""), deli)
End Function

Then I would call it like this:

=LET(x,mySplit("{20;30;40;50;60}",";"),IF(ISNUMBER(--x),--x,x))

If you want it transposed then use TRANSPOSE

=LET(x,mySplit("{20;30;40;50;60}",";"),TRANSPOSE(IF(ISNUMBER(--x),--x,x)))


A single formula can do it all without the need of vba:

=FILTERXML("<a><b>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE("{20;30;40;50;60}",";","</b><b>"),"}",""),"{","")&"</b></a>","//b")




回答2:


The UDF should use a two dimensional array:

Public Function splitt(s As String, sep As String)
    arr = Split(s, sep)
    
    ReDim arr2(0 To UBound(arr), 1 To 1)
    
    For i = LBound(arr) To UBound(arr)
        arr2(i, 1) = arr(i)
    Next i
    
    splitt = arr2
End Function

Example:

With Excel 365 , it will auto-spill. Without Excel 365, it will need array-entry.



来源:https://stackoverflow.com/questions/65258035/how-to-made-custom-split-array-function-in-ms-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!