VBA UDF拆分字符串数组

时间:2017-08-04 09:26:25

标签: excel-vba excel-formula vba excel

我已经编写了这个有效的功能,现在我希望能够将其称为工作表功能,就像在图片中一样,欢迎任何指导:

阵列有两个部分用分号分隔。

Function CellToRange(strDelimiter As String, Optional strColDelimiter As String) As String

    On Error GoTo CellToRange_Error

    Dim rnSource, rnDest As Range
    Dim i As Integer
    Dim Orig As Variant
    Dim txt As String
    Dim intPos As Integer

    Set rnSource = Range("F16")
    Set rnDest = Range("D21")

    txt = rnSource
    Orig = Split(txt, strDelimiter)
    intPos = InStr(1, txt, strColDelimiter) / 2

    For i = 0 To intPos - 1
        If i = intPos - 1 Then
            rnDest.Offset(i).Value = Mid(Orig(i), 1, InStr(1, Orig(i), strColDelimiter) - 1)
        Else
            rnDest.Offset(i).Value = Orig(i)
        End If
    Next i

    For i = intPos - 1 To UBound(Orig)
        If i = intPos - 1 Then
            rnDest.Offset(i - (intPos - 1), 1).Value = Mid(Orig(i), InStr(1, Orig(i), strColDelimiter) + 1, Len(Orig(i)))
        Else
            rnDest.Offset(i - (intPos - 1), 1).Value = Orig(i)
        End If
    Next i

    On Error GoTo 0

    Exit Function

CellToRange_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure CellToRange of Function Functions"

End Function

How I'd like to call the function

Desired outcome

1 个答案:

答案 0 :(得分:0)

这里有龙。 Haven之前没有试过这个,我希望我理解你的权利 此时它需要数字所在的源单元和目标单元,它是输出表的左上单元格。目前的分隔符是","和";"分别用于行和列。
您可以从这一点修改sub以满足您的需求。

<!-- first pagination instance -->
<ul>
    <li dir-paginate="customer in customers | itemsPerPage: 10" pagination-id="cust">{{ customer.name }}</li>
</ul>

<dir-pagination-controls pagination-id="cust"></dir-pagination-controls>

<!-- second pagination instance -->
<ul>
    <li dir-paginate="branch in branches | itemsPerPage: 10" pagination-id="branch">{{ customer.name }}</li>
</ul>

<dir-pagination-controls pagination-id="branch"></dir-pagination-controls>

P.S。我可能错了,但这将是最接近你用UDF修改细胞的方法 P.P.S. Welp,我的功能实际上会返回令人惊讶的结果,我会尽快修复它,但是你有其他的建议。