在列表底部/第一个空白单元格处停止'= Combine'代码

时间:2019-05-23 13:06:52

标签: excel vba string-concatenation

我正在尝试将TaskID代码之间用“,”串在一起。当前代码如下。

我希望代码停止在活动列表的底部/第一个空单元格中。另外,要使输出在最后一个活动单元格之后不包含“,”。

Function Combine(WorkRng As Range, Optional Sign As String = ", ") As String

'Update 20130815

Dim Rng As Range

Dim OutStr As String

For Each Rng In WorkRng

 If Rng.Text <> " " Then

 OutStr = OutStr & Rng.Text & Sign

 End If

Next

Combine = Left(OutStr, Len(OutStr) - 1)

End Function

1 个答案:

答案 0 :(得分:0)

您可以提前检查空度并退出循环。 如果您剪切的字符不是一个字符,而是剪切符号的长度,则应该没问题,最后不要加“,”。

Function Combine(WorkRng As Range, Optional Sign As String = ", ") As String
Dim Rng As Range
Dim OutStr As String
    For Each Rng In WorkRng
        If Rng.Text = vbNullString Then Exit For
        OutStr = OutStr & Rng.Text & Sign
    Next
Combine = Left(OutStr, Len(OutStr) - Len(Sign))
End Function