【发布时间】:2011-08-29 20:18:50
【问题描述】:
我找到了这个自定义 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
我想对其进行调整以在它组合以创建列表的每个单元格之间显示一个逗号。
【问题讨论】:
-
我不会将该函数命名为“Join”,因为已经有一个具有该名称的 VBA 函数,它在数组上执行相同的工作(但不是在 Ranges 上)。
标签: vba excel user-defined-functions