可以扩展一下,对ParamArray 进行分类:字符串、单元格、区域、数组,等等,
这个函数一般不会只是一个单元格一个单元格去加,会是一片区域,如A1:A10,这样的。
供参考
[Visual Basic] 纯文本查看 复制代码 Function TextUnion(delimiter As String, ignore_empty As Boolean, ParamArray arr() As Variant) As String
Application.Volatile '设置为易失性函数,可以即时更新
If IsMissing(arr()) Then TextUnion = "": Exit Function
Dim Rg As Range, Tmp As Variant
For a = LBound(arr()) To UBound(arr())
If Not IsMissing(arr(a)) Then
Select Case TypeName(arr(a))
Case "Range"
For Each Rg In arr(a)
If Len(Rg.Value) > 0 Or (Len(Rg.Value) = 0 And Not ignore_empty) Then TextUnion = TextUnion & delimiter & Rg.Value
Next
Case "Variant()"
For Each Tmp In arr(a)
If Len(Tmp) > 0 Or (Len(Tmp) = 0 And Not ignore_empty) Then TextUnion = TextUnion & delimiter & Tmp
Next
Case Else
If Len(arr(a)) > 0 Or (Len(arr(a)) = 0 And Not ignore_empty) Then TextUnion = TextUnion & delimiter & arr(a)
End Select
End If
Next
TextUnion = Mid(TextUnion, Len(delimiter) + 1)
End Function
|