我正在尝试使用vba将变量数组转换为字符串。我尝试了2种方法,但是它们都不起作用,它们似乎都在同一点上出现。
Dim cell As Range
Dim val As Variant
For Each cell In Range("packing_list[Code]")
val = cell.Value
Next cell
MsgBox Join(val, "//")
和
Dim oSh As Worksheet
Dim CodeRange As Variant
Set oSh = ActiveSheet
CodeRange = oSh.Range("packing_list[Code]").Value
MsgBox Join(CodeRange , "//")
它们都在MsgBox行上出错。我做错了什么?
谢谢
您尝试加入的值不是字符串数组。Join应该用于数组
这是指向Microsoft说明的链接:https : //msdn.microsoft.com/zh-cn/library/b65z3h4h%28v=vs.90%29.aspx
他们的例子是:
Dim TestItem() As String = {"Pickle", "Pineapple", "Papaya"}
Dim TestShoppingList As String = Join(TestItem, ", ")
您的代码应类似于:
Dim i As Integer
Dim cell As Range
Dim val() As Variant '() indicate it is an array
i = 0
For Each cell In Range("packing_list[Code]")
ReDim Preserve val(0 to i) As Variant 'must resize array to fit number of items
val(i) = cell.Value 'i is the position of the item in the array
i = i + 1 'increment i to move to next position
Next cell
'Now that you have an array of values (i.e. ("String1", "String2", ...) instead of just "String" you can:
MsgBox Join(val, "//")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句