我之前看到过有关此事的帖子,并试图应用它,但我一直不成功。
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
它跳过整个循环并转到End Sub。有什么帮助吗?
由于
答案 0 :(得分:0)
您的代码存在一些问题。我试图在下面的脚本中解决它们。很遗憾,您没有使用过您的数据示例。如果有什么东西不起作用,请查看并告诉我。
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