以下のコードでは、2つの日付値をチェックしようとしています。存在する場合は、列BGの間の日数を計算します。存在しないか結果が0より小さい場合は、行を削除します。VBA Forループ反復問題
私が抱えている問題は、いったん行を削除すると、次のIを使用し、行のすぐ後をスキップすることです。例:行1 & 2に日付がありません。行1が削除されます。行2は行1にプッシュアップされ、次に私は2行目になり(3だった)、行2の結果はスキップされます。 i = i-1を使用すると、プログラムがクラッシュするようです。また、コードを効率的にして、アイテムのすぐ後を繰り返し処理できるようにするための方法はありますか?
Sub Func4()
Dim N As Long, i As Long, j As Long, cnt As Long, date1 As Date, date2 As Date, date3 As Long ', iold As Long
N = Cells(Rows.Count, "A").End(xlUp).Row
j = 2
cnt = 0
For i = 2 To N 'main
j = j + 1
'iold = i
If Not IsEmpty(Cells(i, "AB").Value) And Not IsEmpty(Cells(i, "AE").Value) Then
date1 = Cells(i, "AB").Value 'AB=Entry Date
date2 = Cells(i, "AE").Value 'AE=Rec'd
date3 = Work_Days(date2, date1)
cnt = cnt + 1
If date3 >= 0 Then
Cells(i, "BG").Value = date3
Else
Rows(i).EntireRow.Delete
'i = i - 1 'HERE
End If
Else
Rows(i).EntireRow.Delete
'i = i - 1 'HERE
End If
'End If
'If i = iold Then
Next i
'Else
'Next
'End If
End Sub
決議WORKINGのANSWER:
Sub Func4()
Dim N As Long, i As Long, j As Long, cnt As Long, date1 As Date, date2 As Date, date3 As Long
N = Cells(Rows.Count, "A").End(xlUp).Row
j = 2
For i = N To 2 Step -1
j = j + 1
If Not IsEmpty(Cells(i, "AB").Value) And Not IsEmpty(Cells(i, "AE").Value) Then
date1 = Cells(i, "AB").Value 'AB=Entry Date
date2 = Cells(i, "AE").Value 'AE=Rec'd
date3 = Work_Days(date2, date1)
cnt = cnt + 1
If date3 >= 0 Then
Cells(i, "BG").Value = date3
Else
Rows(i).EntireRow.Delete
End If
Else
Rows(i).EntireRow.Delete
End If
Next i
End Sub
。 – Comintern
コミンテルンが言ったように 'For i = N To 2 Step -1' –
チップをありがとう!私はそのように考えなかった。私はそれが完璧に動作するように見えた!あなたの簡単な回答を回答として提出してください。私はあなたにポイントを与えるでしょう! – Josh