我找到了这个自定义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
我想调整它以显示它组合的每个单元格之间的逗号以创建列表。
答案 0 :(得分:8)
您发现的UDF存在一些问题:
这是我的版本。默认情况下,如果将第二个参数留空,则将每个单元格分隔“,”(例如= 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
答案 1 :(得分:5)
看起来它已使用可选的delimiter
参数执行此操作。
就这样称呼它:
=JOIN(A1:A100,",")