简体   繁体   English

Excel VBA For next and if then delete row

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

I am trying to write a code that if there is an IR clinic completed but all Biopsy entries are canceled for the same ID, then I want to delete the row where the IR is.我正在尝试编写一个代码,如果有一个 IR 诊所已完成但所有活检条目都被取消了相同的 ID,那么我想删除 IR 所在的行。 Row 1 is headers.第一行是标题。 Column B is IDs, column L is either "Biopsy" or "IR clinic". B 列是 ID,L 列是“活检”或“IR 诊所”。 Column C is either "Completed" or "Canceled"列 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

The problem with this code is if there is at least one Biopsy Canceled, it deletes the IR row.此代码的问题是如果至少有一个活检已取消,它会删除 IR 行。 What I really need is if ALL biopsy for same ID are canceled it deletes it, but if at least one is completed, it does not delete the IR.我真正需要的是,如果取消了同一 ID 的所有活检,它会删除它,但如果至少完成一个活检,它不会删除 IR。 Thank you for any help in advance.感谢您提前提供任何帮助。

The Shortcomings of (Simplifications in) a For...Next Loop For...Next Loop (简化)的缺点

The Code代码

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

The Outer Loop外环

  • This illustrates why the line LastRow = Cells(Rows.Count, 2).End(xlUp).Row in your code is redundant (you might have used LastRow = LastRow - 1 to the same effect).这说明了为什么代码中的行LastRow = Cells(Rows.Count, 2).End(xlUp).Row是多余的(您可能已使用LastRow = LastRow - 1达到相同的效果)。
  • Whatever you do to fr or lr , it will only loop 5 times.无论您对frlr做什么,它都只会循环 5 次。 Before entering the loop, VBA has stored the numbers fr and lr somewhere.在进入循环之前,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
  • Mimic the behavior of ForNext1 by using a Do...Loop .使用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

The Inner Loop内循环

  • This relates to the utilization of the line If j > i Then in the code.这与代码中If j > i Then行的使用有关。
  • When exiting uninterrupted, j is equal to i + 1 ( 6 ), not i ( 5 ).当不间断退出时, 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