且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将范围转换为逗号分隔的字符串

更新时间:2023-11-07 18:30:22

您可以使用 Join()函数使用定界符将一维数组的所有元素连接在一起.

You can use the Join() function to join all the elements of a 1 dimensional array with a delimiter.

下面使用 Transpose()函数形成维数组(此方法适用于单列或单行).

The Transpose() function is used below to form the dimensional array (this approach works on a single column or row).

Sub Main()
    Dim arr
    arr = Join(Application.Transpose(Range("A2:A5").Value), ",")
    MsgBox arr
End Sub

或作为UDF

Public Function Merge(r As Range) As String
    Merge = Join(Application.Transpose(r.Value), ",")
End Function