构建逗号分隔的字符串 [英] Build a Comma Delimited String

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

问题描述

我想从Range A1:A400 建立一个逗号分隔的字符串。

I want to build a comma delimited string from Range A1:A400.

这样做最好的方法是什么?我应该使用循环?

What is the best way of doing this? Should I use a For loop?

推荐答案

最懒惰的方法是

s = join(Application.WorksheetFunction.Transpose([a1:a400]), ",")

这是因为多单元范围的 .Value 属性返回2D数组,和加入期望1D数组,而 Transpose 正试图太有帮助,所以当它检测到2D数组时只有一列,它将其转换为1D阵列。

This works because .Value property of a multicell range returns a 2D array, and Join expects 1D array, and Transpose is trying to be too helpful, so when it detects a 2D array with just one column, it converts it to a 1D array.

在生产中,建议至少使用一点点懒惰选项,

In production it is advised to use at least a little bit less lazy option,

s = join(Application.WorksheetFunction.Transpose(Worksheets(someIndex).Range("A1:A400").Value), ",")

否则将始终使用活动工作表。

otherwise the active sheet will always be used.

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

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