我有一個子過程刪除包含大約1000行的範圍中的行。 行在critera上被刪除。下面的代碼工作。Excel VBA刪除for循環中的行缺失行
但是,當我運行宏時,通常必須在刪除包含刪除條件的所有行之前運行它4次。
我想這是因爲for循環在刪除一行時突然消失的情況下會錯過它的索引。
我的第一個代碼看起來像這樣。
Set StatusRange = Range("B2", Range("B2").End(xlDown))
For Each StatusCell In StatusRange
If StatusCell = "FG" Then
StatusCell.EntireRow.Delete
ElseIf StatusCell = "QC" Then
StatusCell.EntireRow.Delete
ElseIf StatusCell = "CS" Then
StatusCell.EntireRow.Delete
Else
End If
Next StatusCell
當我嘗試更新範圍每個循環,它仍然無法正常工作。
Set StatusRange = Range("B2", Range("B2").End(xlDown))
For Each StatusCell In StatusRange
If StatusCell = "FG" Then
StatusCell.EntireRow.Delete
ElseIf StatusCell = "QC" Then
StatusCell.EntireRow.Delete
ElseIf StatusCell = "CS" Then
StatusCell.EntireRow.Delete
Else
End If
Set StatusRange = Range("B2", Range("B2").End(xlDown))
Next StatusCell
有沒有人知道這個問題?謝謝。
從德自下而上的工作。如果你刪除一行,一切都向上移動並昱歐上跳過該行下一次迭代 – Jeeped
這幾乎肯定是一個重複的問題,當我找到合適的重複項時,我會將其標記爲這樣。無論何時從集合中刪除項目,您都必須從Rows.Count中刪除1 Step -1 ''否則你會跳過行。 –
繼續前進,並認爲這@DavidZemens - 我已經通過顯示Case語句進行多重比較增加了一些價值,但這仍然是一個騙局。 – Jeeped