簡體   English   中英

Excel VBA For next and if then delete row

[英]Excel VBA For next and if then delete row

我正在嘗試編寫一個代碼,如果有一個 IR 診所已完成但所有活檢條目都被取消了相同的 ID,那么我想刪除 IR 所在的行。 第一行是標題。 B 列是 ID,L 列是“活檢”或“IR 診所”。 列 C 是“已完成”或“已取消”

For i = 2 To LastRow
    If Range("L" & i).Value = "IR Clinic" And Range("C" & i).Value = "Completed" Then
        For j = 2 To i
            If  Range("B" & j).Value = Range("B" & i).Value And _
                Range("L" & j).Value = "Biopsy" And _
                Range("C" & j).Value = "Canceled" Then

                    Selection.Rows(i).EntireRow.Delete
                    LastRow = Cells(Rows.Count, 2).End(xlUp).Row
                    i = i - 1
                    Exit For

            End If
        Next j
    End If
Next i

此代碼的問題是如果至少有一個活檢已取消,它會刪除 IR 行。 我真正需要的是,如果取消了同一 ID 的所有活檢,它會刪除它,但如果至少完成一個活檢,它不會刪除 IR。 感謝您提前提供任何幫助。

For...Next Loop (簡化)的缺點

代碼

Option Explicit

Sub Test()
    i = 2
    Do
    'For i = 2 To LastRow ' LastRow is stored; you cannot effectively modify it
        If Range("L" & i).Value = "IR Clinic" _
                And Range("C" & i).Value = "Completed" Then
            For j = 2 To i
                ' Note the change in this logic!
                If Range("B" & j).Value = Range("B" & i).Value _
                        And Range("L" & j).Value = "Biopsy" _
                        And Range("C" & j).Value <> "Canceled" Then ' or '= "Completed"'
                    Exit For
                End If
            Next j
            If j > i Then ' if the loop was uninterrupted; all canceled; none completed
                Rows(i).Delete
                LastRow = LastRow - 1
                i = i - 1
            'Else ' the loop was interrupted; not all canceled; at least one was completed
            End If
        End If
        i = i + 1
    Loop Until i > LastRow
End Sub

外環

  • 這說明了為什么代碼中的行LastRow = Cells(Rows.Count, 2).End(xlUp).Row是多余的(您可能已使用LastRow = LastRow - 1達到相同的效果)。
  • 無論您對frlr做什么,它都只會循環 5 次。 在進入循環之前,VBA 已經將數字frlr存儲在某處。
Sub ForNext1()
    Dim fr As Long: fr = 2 ' irrelevant
    Dim lr As Long: lr = 6
    Dim i As Long
    For i = fr To lr
        ' do your stuff
        fr = 2 * i ' irrelevant
        lr = 3 * i
        Debug.Print "i = " & i, "fr = " & fr, "lr = " & lr
    Next i
End Sub
  • 使用Do...Loop模仿ForNext1的行為。
Sub DoLoop()
    Dim fr As Long: fr = 2 ' irrelevant
    Dim lr As Long: lr = 6
    Dim i As Long: i = fr
    Do
        ' do your stuff
        fr = 2 * i ' irrelevant
        lr = 3 * i
        Debug.Print "i = " & i, "fr = " & fr, "lr = " & lr
        ' Next
        i = i + 1
        lr = 6 ' Gotcha! Here we can effectively change 'lr'!!!
    Loop Until i > lr
End Sub

內循環

  • 這與代碼中If j > i Then行的使用有關。
  • 當不間斷退出時, j等於i + 1 ( 6 ),而不是i ( 5 )。
Sub ForNextTEST2()
    Dim i As Long: i = 5
    Dim j As Long
    For j = 1 To i
    Next j
    Debug.Print "j = " & j, "i = " & i
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM