0 excel vba excel-vba user-defined-functions
我找到了这个自定义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
Run Code Online (Sandbox Code Playgroud)
我想调整它以显示它组合的每个单元格之间的逗号以创建列表.
你找到的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
Run Code Online (Sandbox Code Playgroud)
小智 5
看起来它已经使用可选delimiter参数执行此操作.
只需将其称为:
=JOIN(A1:A100,",")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
575 次 |
| 最近记录: |