将分隔符添加到串联列表

发布于 2024-12-01 21:13:01 字数 862 浏览 0 评论 0原文

我发现了这个自定义 Excel 函数:

Function Join(source As Range, Optional delimiter As String) As String
'
' Join Macro
' Joins (concatenates) the values from an arbitrary range of cells,
' with an optional delimiter.
'

'optimized for strings
'   check len is faster than checking for ""
'   string Mid$ is faster than variant Mid
'   nested ifs allows for short-circuit + is faster than &

    Dim sResult As String
    Dim oCell As Range

    For Each oCell In source.Cells
        If Len(oCell.Value) > 0 Then
            sResult = sResult + CStr(oCell.Value) + delimiter
        End If
     Next

    If Len(sResult) > 0 Then
        If Len(delimiter) > 0 Then
            sResult = Mid$(sResult, 1, Len(sResult) - Len(delimiter))
        End If
    End If

    Join = sResult
End Function

我想调整它以在它组合创建列表的每个单元格之间显示逗号。

I found this custom Excel Function:

Function Join(source As Range, Optional delimiter As String) As String
'
' Join Macro
' Joins (concatenates) the values from an arbitrary range of cells,
' with an optional delimiter.
'

'optimized for strings
'   check len is faster than checking for ""
'   string Mid$ is faster than variant Mid
'   nested ifs allows for short-circuit + is faster than &

    Dim sResult As String
    Dim oCell As Range

    For Each oCell In source.Cells
        If Len(oCell.Value) > 0 Then
            sResult = sResult + CStr(oCell.Value) + delimiter
        End If
     Next

    If Len(sResult) > 0 Then
        If Len(delimiter) > 0 Then
            sResult = Mid$(sResult, 1, Len(sResult) - Len(delimiter))
        End If
    End If

    Join = sResult
End Function

I would like to tweak it to show a comma between each cell it combines to create a list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2024-12-08 21:13:01

您发现的 UDF 有几个问题:

  • 应使用“&”进行串联。不是“+”。
  • 使用范围内的单元格比使用变体数组并且纯粹在 VBA 内部工作要慢。每次对 Excel 的调用都会对性能产生微小的影响,而这种影响可能会累积起来。
  • 如果连接正确完成,则无需转换为字符串。
  • 应优化串联,以便首先连接较小的部分,然后将其添加到结果中,否则结果将被复制两次以执行每个串联。
  • 名称不应该是 Join,因为 VBA 有一个具有该名称的函数。
  • 不需要检查分隔符的 LEN,因为它是一个字符串。默认情况下,如果不存在,则为 LEN(0),您可以从 len(result) 中减去 0,无需担心。
  • 没什么大不了的,但检查不平等<>比 > 稍快。

这是我的版本。默认情况下,如果将第二个参数留空,它将用“,”分隔每个单元格(例如=ConcatenateRange(A1:A100)

Function ConcatenateRange(ByVal cell_range As range, _
                    Optional ByVal seperator As String = ", ") As String

Dim cell As range
Dim newString As String
Dim vArray As Variant
Dim i As Long, j As Long

vArray = cell_range.Value

For i = 1 To UBound(vArray, 1)
    For j = 1 To UBound(vArray, 2)
        If Len(vArray(i, j)) <> 0 Then
            newString = newString & (seperator & vArray(i, j))
        End If
    Next
Next

If Len(newString) <> 0 Then
    newString = Right$(newString, (Len(newString) - Len(seperator)))
End If

ConcatenateRange = newString

End Function

There are a couple things wrong with that UDF you found:

  • Concatenation should be done with "&" not "+".
  • Working with cells in a range is slower than a variant array and working purely from inside VBA. Each call to Excel produces a small hit in performance that could add up.
  • The casting to string is unnessesary if the concatenation was done properly.
  • Concatenation should be optimized so that the smaller parts are joined first, then added to the result, otherwise the result is copied twice to do each concatenation.
  • Name should not be Join since VBA has a function of that name.
  • There should be no need to check for LEN of delimiter since it's a string. By default it will be LEN(0) if not existing and you can subtract 0 from the len(result) without any worry.
  • Not a big deal but checking for inequality <> is slightly faster than >.

Here's my version. By default it will seperate each cell by ", " if you leave the second argument empty (ex. =ConcatenateRange(A1:A100)

Function ConcatenateRange(ByVal cell_range As range, _
                    Optional ByVal seperator As String = ", ") As String

Dim cell As range
Dim newString As String
Dim vArray As Variant
Dim i As Long, j As Long

vArray = cell_range.Value

For i = 1 To UBound(vArray, 1)
    For j = 1 To UBound(vArray, 2)
        If Len(vArray(i, j)) <> 0 Then
            newString = newString & (seperator & vArray(i, j))
        End If
    Next
Next

If Len(newString) <> 0 Then
    newString = Right$(newString, (Len(newString) - Len(seperator)))
End If

ConcatenateRange = newString

End Function
夏有森光若流苏 2024-12-08 21:13:01

看起来它已经使用可选的 delimiter 参数执行此操作。

就像这样称呼它:

=JOIN(A1:A100,",")

It looks like it already does this with the optional delimiter parameter.

Just call it like this:

=JOIN(A1:A100,",")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文