通过dynamic行循环的最快方法

我曾经见过一个关于这个的post,试图应用它,但我没有成功。

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 

它跳过整个循环和去结束小组。 有什么协助?

谢谢

你在那里的代码有几个问题。 我试图在下面的脚本中解决它们。 不幸的是,您没有使用您的数据的例子。 看看,让我知道如果有什么不工作。

 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