VBA逗号分隔的单元格,仅需要最后一个条目

时间:2016-09-09 10:41:02

标签: excel vba

所以我有一个数据集,其中包含同一单元格中的城市和地址,我想删除城市,这通常是最后一个逗号分隔值,并将其放入旁边的单元格中 example

1 个答案:

答案 0 :(得分:1)

我无法看到您的图片,但在您的解释之后,您正在寻找以下功能:

Function Remove_last_part(StrCommaSeparated As String) As String

    Dim arr() As String
    Dim i As Integer

    arr = Split(StrCommaSeparated, ",") ' make an array out of the comma separated string

    ReDim Preserve arr(UBound(arr) - 1) ' Remove the last array element, by redimensioning the array to it's total elements minus 1

    Remove_last_part = Join(arr, ",")  ' make a comma separated string out of the redimensionned array

End Function

如何使用它的示例:

Public Sub TestIt()

    Dim strTest As String

    strTest = "anything,anywhen,anyhow,New York"

    Debug.Print "BEFORE -->" & strTest
    Debug.Print "AFTER  -->" & Remove_last_part(strTest)

End Sub

将输出:

BEFORE -->anything,anywhen,anyhow,New York
AFTER  -->anything,anywhen,anyhow