将 VBA 数组元素输出到 Excel 中的一个单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18543517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:05:19  来源:igfitidea点击:

Output VBA Array Elements to One Cell in Excel

arraysexcel-vbaoutputvbaexcel

提问by user2734645

So i have a vba code that creates an array with multiple elements. I would like to output those elements into one cell in excel. Im able to output its elements to multiple cells but prefer it in one cell. Can this be done?

所以我有一个 vba 代码,它创建一个包含多个元素的数组。我想将这些元素输出到 excel 中的一个单元格中。我能够将其元素输出到多个单元格,但更喜欢将其输出到一个单元格中。这能做到吗?

回答by Andy G

If the array is declared as a String or Variant then you can use Join:

如果数组被声明为字符串或变体,那么您可以使用Join

Sub AllIntoOne()
    Dim arr(1 To 3) As Variant
    arr(1) = 4
    arr(2) = 54
    arr(3) = 3
    Range("A1") = Join(arr, ",")

End Sub

The delimiter "," defaults to a space if not supplied, but can be an empty string "" if no separation is required.

如果未提供分隔符“,”,则默认为空格,但如果不需要分隔符,则可以是空字符串“”。