vba 将变体数组转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22456244/
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-08 17:58:50  来源:igfitidea点击:

Convert Variant Array to String

vbavariant

提问by user3428722

I am trying to take a variant variable and convert it into a string so that I can run a split function on the data. However, whenever I try to redefine the variant I get a type mismatch error. I have used the CStr(), Str(), and ToString functions. None work.

我正在尝试获取一个变体变量并将其转换为字符串,以便我可以对数据运行拆分函数。但是,每当我尝试重新定义变体时,都会出现类型不匹配错误。我使用过 CStr()、Str() 和 ToString 函数。没有工作。

Anything I am missing?

我缺少什么吗?

Function FlatLine(ByVal lines As Variant)

Dim flat() As String
ReDim Preserve flat(i)

For i = 0 To UBound(lines)
    flat(UBound(flat)) = lines(i)
    ReDim Preserve flat(LBound(flat) To UBound(flat) + 1)
Next i

Dim flat2 as String
flat2 = Cstr(flat)

^ errors there.

采纳答案by bugmagnet

The for is useless, as far as I can see. Better ReDim flat and generate flat2 as below

就我所见, for 没用。更好的 ReDim flat 并生成 flat2 如下

ReDim flat(UBound(lines))
flat2 = Join(flat,"|")

in fact, given that lines is coming in as ByVal you could probably

事实上,考虑到线路是作为 ByVal 进来的,你可能会

flat2 = Join(lines,"|")