Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 将单元格范围存储到数组中_Arrays_Vba_Excel - Fatal编程技术网

Arrays 将单元格范围存储到数组中

Arrays 将单元格范围存储到数组中,arrays,vba,excel,Arrays,Vba,Excel,我正在使用数组,很抱歉,我对它有点陌生,仍然感到困惑。我已经有了这段代码来存储数组中某个范围内的值,如果我运行它,它将是空的 attPresent = ws.Range("H4:H" & lastrow) ReDim attPresent(1 To UBound(attPresent)) For k = LBound(attPresent) To UBound(attPresent) MsgBox attPresent(k) Next 有人能告诉

我正在使用数组,很抱歉,我对它有点陌生,仍然感到困惑。我已经有了这段代码来存储数组中某个范围内的值,如果我运行它,它将是空的

attPresent = ws.Range("H4:H" & lastrow)
    ReDim attPresent(1 To UBound(attPresent))
    For k = LBound(attPresent) To UBound(attPresent)
        MsgBox attPresent(k)
    Next
有人能告诉我哪里错了吗?我读过其他帖子,也收集了一些想法,但仍然不起作用。

参考资料

当您
Redim
阵列时,您正在擦除所有值。使用
ReDim Preserve
调整数组最后一个维度的大小,同时仍保留数组的值

分配动态数组后(第一次使用Redim),您只能调整数组的最后一个维度的大小,并且不能更改维度的数量。在您的代码中,您试图将二维数组转换为一维数组,但不能这样做

您应该观看以下完整系列:。这是相关视频:

参考

当您
Redim
阵列时,您正在擦除所有值。使用
ReDim Preserve
调整数组最后一个维度的大小,同时仍保留数组的值

分配动态数组后(第一次使用Redim),您只能调整数组的最后一个维度的大小,并且不能更改维度的数量。在您的代码中,您试图将二维数组转换为一维数组,但不能这样做

您应该观看以下完整系列:。这是相关视频:

你可以这样走

Dim attPresent as Variant
attPresent = ws.Range("H4:H" & lastrow).Value '<-- this will automatically size the array to a 2dimensional array of as many rows as the range ones and one column
For k = LBound(attPresent,1) To UBound(attPresent,1)
    MsgBox attPresent(k,1)
Next
Dim att作为变体出现
attPresent=ws.Range(“H4:H”和lastrow).Value'您可以这样做

Dim attPresent as Variant
attPresent = ws.Range("H4:H" & lastrow).Value '<-- this will automatically size the array to a 2dimensional array of as many rows as the range ones and one column
For k = LBound(attPresent,1) To UBound(attPresent,1)
    MsgBox attPresent(k,1)
Next
Dim att作为变体出现

attPresent=ws.Range(“H4:H”&lastrow)。值“很抱歉,youtube被我们的组织阻止了:”(我无法查看它..如果我执行了
ReDim Preserve
操作,它将返回一个错误:
下标超出范围
。很抱歉,youtube被我们的组织阻止了:'(我无法查看它。)如果我执行了
ReDim Preserve
操作,它将返回一个错误:
下标超出范围
。为什么变量后面有1?谢谢“变量后面的1”指的是什么?
(目前,1)
变量我的意思是:)它告诉
LBound()
UBound())< /代码>考虑数组的索引,即在这种情况下,2D数组的行索引。实际上,可以忽略第一个索引作为默认索引,但是在多个数组中,我总是显式声明它现在刚刚注意到,第一次运行代码时,它运行,但是如果我重新运行它,它将返回一个在该LIN中的类型错误匹配错误。e:
对于k=LBound(attPresent,1)到UBound(attPresent,1)
为什么变量后面有1?谢谢“变量后面有1”你指的是什么?
(attPresent,1)
变量我的意思是:)它告诉
LBound()
UBound()< /代码>考虑数组的索引,即在这种情况下,2D数组的行索引。实际上,可以忽略第一个索引作为默认索引,但是在多个数组中,我总是显式声明它现在刚刚注意到,第一次运行代码时,它运行,但是如果我重新运行它,它将返回一个在该LIN中的类型错误匹配错误。e:
对于k=LBound(attPresent,1)到UBound(attPresent,1)
两件事显然是错误的。你的“数组”将不是数组而是范围。并且redim在使用自身的当前UBound时不会重新加密。您需要将其声明为变量,并让VBA将实际边界排序为赋值语句的一部分。有两件事显然是错误的。您的“数组”将不是数组而是范围。并且redim使用自身的当前UBound时不会重新生成。您需要将其声明为变量,并让VBA将实际边界排序为赋值语句的一部分。
    'Try this code example to suit your case
Sub StoreRangeofCellsToAnArray()
        'presumes optionbase=0
        'presume Range("a1:c3") contain number 1 to 9
        Dim MyRange, MyArray(9), n
        n = 0
        For Each c In Range("a1:c3")
        MyArray(n) = c
        n = n + 1
        Next

        'testing: reprint array
        For n = 0 To 8
        Debug.Print MyArray(n)
        Next
    End Sub