Excel教程:替自定义函数设置可选参数

本文为《别怕,Excel VBA其实特别简单(第3版)》随书问题参考答案

Function JoinText(Rng As Range, Optional Txt As String = ",") As String

Dim c As Range

For Each c In Rng

JoinText = JoinText & c.Value & Txt

Next c

JoinText = Left(JoinText, Len(JoinText) - 1)

End Function

或者

Function JoinText(Rng As Range, Optional Txt As Variant) As String

Dim c As Range

If IsMissing(Txt) Then Txt = ","

For Each c In Rng

JoinText = JoinText & c.Value & Txt

Next c

JoinText = Left(JoinText, Len(JoinText) - 1)

End Function

AI助手