我有以下问题(以及克服它的迫切愿望:))。我需要让我的循环遍历行,直到找到某个值。让我在我的代码中更详细地演示我需要什么:
For x = 1 To 1000
If Cells(x, "O").Value = "P" Or Cells(x, "O").Value = "R" Then
Dim i As Integer
For i = 1 To 121
If Worksheets(Cells(x, "P").Value).Cells(Cells(x, "Q").Value + i, "C") = "" Then
With Worksheets(Cells(x, "P").Value)
.Cells(Cells(x, "Q").Value + i, "A").Resize(, 20).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("F" & x, "H" & x).Copy
.Cells(Cells(x, "Q").Value + i, "E").PasteSpecial xlPasteAll
.Cells(Cells(x, "Q").Value + i, "C") = "Pur"
Range("AI" & x).Copy
.Cells(Cells(x, "Q").Value + i, "O").PasteSpecial xlPasteAll
End With
End If
Next i
End If
此代码只是遍历行,当指定的单元格(在本例中为“C”列中的单元格)为空时,它会执行所有复制和粘贴。但!它执行的时间与我所指出的一样多(对于 i = 1 到 121)。我需要的是一个循环,它将循环遍历行,直到出现“D”列中的第一个空单元格,然后执行所有复制和粘贴,然后停止。我能做些什么来实现这一目标?
如果我的问题含糊不清或难以理解,请告诉我。
正如我所建议的那样,我通过演示我的尝试来更新我的问题: Changes aremarked
with comments
Dim a As Integer 'I introduced new variable
a = 121 'This is it
For x = 1 To 1000
If Cells(x, "O").Value = "P" Or Cells(x, "O").Value = "R" Then
Dim i As Integer
For i = 1 To a 'Changes
If Worksheets(Cells(x, "P").Value).Cells(Cells(x, "Q").Value + i, "C") = "" Then
With Worksheets(Cells(x, "P").Value)
.Cells(Cells(x, "Q").Value + i, "A").Resize(, 20).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("F" & x, "H" & x).Copy
.Cells(Cells(x, "Q").Value + i, "E").PasteSpecial xlPasteAll
.Cells(Cells(x, "Q").Value + i, "C") = "Pur"
Range("AI" & x).Copy
.Cells(Cells(x, "Q").Value + i, "O").PasteSpecial xlPasteAll
End With
a = i ' This way I wanted to end the loop sooner
End If
Next i
End If