问题
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:
- Do not call your function
Split
- Use Nested
Replace
- 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