简体   繁体   English

ReDim在循环内部不起作用,不重述数组大小

[英]ReDim not working inside for loop, no restatement of array size

I have the following code below, the issue is the array size "varray" is not updated everytime a new row is inserted, i know this by checking the value of varray everytime a new row is inserted via the code in Excel. 我下面有以下代码,问题是每次插入新行时数组大小“ varray”不会更新,我每次通过Excel中的代码插入新行时都通过检查varray的值来知道这一点。 This means the code will stop at the end of a fixed array. 这意味着代码将在固定数组的末尾停止。 I tried to redim in multiple points to see if that worked but it has not. 我试图在多个方面进行重塑,以查看是否可行,但没有成功。

Sub test()

Dim i As Long
Dim numbers() As Integer
Dim varray As Integer

varray = Sheets("Original").Cells(Rows.Count, "A").End(xlUp).Row
ReDim Preserve numbers(varray)

For i = 10 To varray
ReDim numbers(varray)
    If Cells(i, 16).Value <> "" Then
        Cells(i + 1, 16).EntireRow.Insert
        Cells(i + 1, 1).EntireRow.Value = Cells(i, 1).EntireRow.Value
        Cells(i + 1, 6).Value = Cells(i, 16).Value
        Cells(i + 1, 1).Value = 20305
        Cells(i + 1, 11).Value = ""
        Cells(i + 1, 12).Value = ""
        Cells(i + 1, 15).Value = ""
        Cells(i + 1, 16).Value = ""
        i = i + 1
        ReDim numbers(varray)
    End If
    ReDim numbers(varray)
Next i

End Sub

You've never changed the value of varray , so your ReDim statements are always resizing the array to its initial value. 您从未更改过varray的值,因此您的ReDim语句始终将数组的大小调整为其初始值。

varray has been assigned by: varray已分配给:

varray = Cells(Rows.Count, "A").End(xlUp).Row

The value of this variable does not change simply by inserting cells/rows in the worksheet. 仅在工作表中插入单元格/行不会更改此变量的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM