Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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
Vba 循环遍历动态行的最快方法_Vba_Excel_Loops - Fatal编程技术网

Vba 循环遍历动态行的最快方法

Vba 循环遍历动态行的最快方法,vba,excel,loops,Vba,Excel,Loops,我以前看过一篇关于这一点的帖子,并尝试应用它,但一直没有成功 Sub test() Dim i As Long Dim varray As Variant Sheets("Original").Select varray = Sheets("Original").Range("A10:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value For i = 10 To UBound(varray, 1) If Cells(i, 16)

我以前看过一篇关于这一点的帖子,并尝试应用它,但一直没有成功

Sub test()

Dim i As Long
Dim varray As Variant

Sheets("Original").Select
varray = Sheets("Original").Range("A10:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value

For i = 10 To UBound(varray, 1)
    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 = ""
    End If
Next

End Sub
子测试()
我想我会坚持多久
Dim varray作为变体
图纸(“原件”)。选择
varray=Sheets(“原始”).Range(“A10:A”)和Cells(Rows.Count,“A”).End(xlUp).Row.Value
对于i=10到UBound(varray,1)
如果单元格(i,16)。值为“”,则
单元格(i+1,16).EntireRow.Insert
单元格(i+1,1)。EntireRow.Value=单元格(i,1)。EntireRow.Value
单元格(i+1,6)。值=单元格(i,16)。值
单元格(i+1,1)。值=20305
单元格(i+1,11)。Value=“”
单元格(i+1,12)。Value=“”
单元格(i+1,15)。Value=“”
单元格(i+1,16)。Value=“”
如果结束
下一个
端接头
它跳过整个for循环并转到End Sub。有什么帮助吗


谢谢

这里的代码有一些问题。我试图在下面的脚本中解决这些问题。不幸的是,没有您使用的数据示例。看一看,如果有什么不起作用,请告诉我

Option Explicit

Sub test()

    'Get used to declaring your worksheet
    'and then reference that worksheet when wanting to access data form it.
    Dim OrigSht As Worksheet
    Set OrigSht = ThisWorkbook.Sheets("Original")

    Dim LastRowColA As Long
    LastRowColA = OrigSht.Cells(Rows.Count, "A").End(xlUp).Row

    'Not sure why you wanted to use an Array, seeing as you dont use it in the loop.
    'Unless you use it in some other code and this is a extract from other code.
    Dim varray As Variant
    varray = OrigSht.Range("A10:A" & LastRowColA).Value

    'Using UBound could present errors if there was less than 10 lines of data _
    it would then step the loop because the  to value is less than the start
    'Rather use the last row of column A as a end of the For loop

    'The problem with editing a list of data form the begining of a list _
    is that the list becomes longer as you add information, so when adding _
    or deleting lines you always want to start at the end og the list
    Dim i As Long
    For i = LastRowColA To 10 Step -1

        With OrigSht
            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 = ""
            End If

        End With
    Next

End Sub
选项显式
子测试()
'习惯于声明您的工作表
'然后在希望访问该工作表中的数据时引用该工作表。
模糊OrigSht作为工作表
Set OrigSht=ThisWorkbook.Sheets(“原件”)
暗淡的最后一排可乐一样长
LastRowColA=OrigSht.Cells(Rows.Count,“A”).End(xlUp).Row
'不确定为什么要使用数组,因为在循环中没有使用它。
'除非您在其他代码中使用它,并且这是其他代码的摘录。
Dim varray作为变体
varray=OrigSht.Range(“A10:A”&LastRowColA).Value
“如果数据少于10行,使用UBound可能会出现错误_
然后它将步进循环,因为to值小于start
'而是使用列A的最后一行作为For循环的结尾
'从列表的开头编辑数据列表的问题_
添加信息时列表会变长,因此在添加信息时_
或者删除始终希望从列表末尾开始的行
我想我会坚持多久
对于i=LastRowColA到10步骤-1
与奥里希特
如果.Cells(i,16).Value为“”,则
.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
.单元格(i+1,1)。值=20305
.Cells(i+1,11).Value=“”
.Cells(i+1,12).Value=“”
.Cells(i+1,15).Value=“”
.Cells(i+1,16).Value=“”
如果结束
以
下一个
端接头

您的代码有一些问题。我试图在下面的脚本中解决这些问题。不幸的是,没有您使用的数据示例。看一看,如果有什么不起作用,请告诉我

Option Explicit

Sub test()

    'Get used to declaring your worksheet
    'and then reference that worksheet when wanting to access data form it.
    Dim OrigSht As Worksheet
    Set OrigSht = ThisWorkbook.Sheets("Original")

    Dim LastRowColA As Long
    LastRowColA = OrigSht.Cells(Rows.Count, "A").End(xlUp).Row

    'Not sure why you wanted to use an Array, seeing as you dont use it in the loop.
    'Unless you use it in some other code and this is a extract from other code.
    Dim varray As Variant
    varray = OrigSht.Range("A10:A" & LastRowColA).Value

    'Using UBound could present errors if there was less than 10 lines of data _
    it would then step the loop because the  to value is less than the start
    'Rather use the last row of column A as a end of the For loop

    'The problem with editing a list of data form the begining of a list _
    is that the list becomes longer as you add information, so when adding _
    or deleting lines you always want to start at the end og the list
    Dim i As Long
    For i = LastRowColA To 10 Step -1

        With OrigSht
            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 = ""
            End If

        End With
    Next

End Sub
选项显式
子测试()
'习惯于声明您的工作表
'然后在希望访问该工作表中的数据时引用该工作表。
模糊OrigSht作为工作表
Set OrigSht=ThisWorkbook.Sheets(“原件”)
暗淡的最后一排可乐一样长
LastRowColA=OrigSht.Cells(Rows.Count,“A”).End(xlUp).Row
'不确定为什么要使用数组,因为在循环中没有使用它。
'除非您在其他代码中使用它,并且这是其他代码的摘录。
Dim varray作为变体
varray=OrigSht.Range(“A10:A”&LastRowColA).Value
“如果数据少于10行,使用UBound可能会出现错误_
然后它将步进循环,因为to值小于start
'而是使用列A的最后一行作为For循环的结尾
'从列表的开头编辑数据列表的问题_
添加信息时列表会变长,因此在添加信息时_
或者删除始终希望从列表末尾开始的行
我想我会坚持多久
对于i=LastRowColA到10步骤-1
与奥里希特
如果.Cells(i,16).Value为“”,则
.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
.单元格(i+1,1)。值=20305
.Cells(i+1,11).Value=“”
.Cells(i+1,12).Value=“”
.Cells(i+1,15).Value=“”
.Cells(i+1,16).Value=“”
如果结束
以
下一个
端接头

您的数据从哪一行开始?10?当您单步执行代码时,
varray
的值是多少?@tompreston yes它从第行开始10@SkipIntro该值为“空”,首先将工作表的一部分
A10:A100
(或最后一行的任何内容)放入数组,然后遍历该数组。然而,您不是在循环中使用/引用数组,而是在工作表上工作。这似乎没有道理。如果您想快速进行,那么您应该在阵列中(仅)执行所有操作,然后(在一次运行中)在工作表上执行所有更改。你在这里失去了阵列的速度优势。此外,有些代码不合格(例如
行。计数
或循环中的所有工作表引用)。数据从哪一行开始?10?当您单步执行代码时,
varray
的值是多少?@tompreston yes它从第行开始10@SkipIntro该值为“空”,首先将工作表的一部分
A10:A100
(或最后一行的任何内容)放入数组,然后遍历该数组。然而,您不是在循环中使用/引用数组,而是在工作表上工作。这似乎没有道理。如果您想快速进行,那么您应该在阵列中(仅)执行所有操作,然后(在一次运行中)在工作表上执行所有更改。你在这里失去了阵列的速度优势。此外,您的某些代码不合格(例如
行.Count
或所有工作表引用