将范围转换为逗号分隔的字符串 [英] Convert range to comma delimited string

查看:56
本文介绍了将范围转换为逗号分隔的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的专栏:

Col1
abc
def
ghi
jkl

如何将其转换为这样的字符串?:

How can I convert it to a string like this?:

"abc,def,ghi,jkl"

推荐答案

您可以使用 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

这篇关于将范围转换为逗号分隔的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆