gpt4 book ai didi

excel - 将变量数组转换为字符串

转载 作者:行者123 更新时间:2023-12-02 03:44:11 24 4
gpt4 key购买 nike

我正在尝试使用 vba 将变量数组转换为字符串。我尝试了两种方法,但都不起作用,它们似乎都在同一点上阻塞。

    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/en-us/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, "//")

关于excel - 将变量数组转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34202578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com