删除重复项时串联细胞(删除第一个逗号)

时间:2019-01-31 18:08:24

标签: excel vba

因此,我能够找到如何在删除重复项的同时连接一行行/列的单元格。

但是,尽管等式在连接列表时会用逗号将其开头,但看起来还是很不干净。

”,a,b,c,d,e”,而不只是“ a,b,c,d,e”

Public Function MakeList(myRange As Range)
    Application.Volatile
    Dim c As Range

    With CreateObject("Scripting.Dictionary")
        For Each c In myRange
            If Rows(c.Row).Hidden = False Then .Item(c.Value) = c.Value
        Next c
        MakeList = Join(.keys, " ,")
    End With
End Function

如果可以的话,我希望能够摆脱第一个逗号。谢谢。

1 个答案:

答案 0 :(得分:0)

您不应该得到一个领先的分隔符,除非你开始一个空白单元格。

Public Function MakeList(myRange As Range)
    Application.Volatile
    Dim c As Range

    With CreateObject("Scripting.Dictionary")
        For Each c In myRange
            If not c.parent.Rows(c.Row).Hidden and trim(c.value) <> "" Then .Item(c.Value) = c.Value
        Next c
        MakeList = Join(.keys, " ,")
    End With
End Function

另外,你可以返回连接字符串之前删除领先分隔符。

Public Function MakeList(myRange As Range, _
                         optional d as string = " ,")
    Application.Volatile
    Dim c As Range

    With CreateObject("Scripting.Dictionary")
        For Each c In myRange
            If c.parent.Rows(c.Row).Hidden = False Then .Item(c.Value) = c.Value
        Next c
        MakeList = Join(.keys, d)
        if left(makelist, len(d) = d then makelist = mid(makelist, len(d)+1)
    End With
End Function