私は次の問題を抱えています(そしてそれを克服したいという切実な願望:))。特定の値が見つかるまで、行を通過するようにループを作成する必要があります。私のコードで何が必要かをさらに詳しく説明しましょう。
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」の最初の空のセルが表示されるまで行をループし、すべてのコピーと貼り付けを実行してから停止するループです。それを達成するために何ができますか?
私の質問が漠然としていたり、理解しにくい場合はお知らせください。
mehow が提案したように、質問を更新して、私の試みのプレゼンテーションを行います:
変更はコメントでマークされます
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