【问题标题】:Excel 2010: VB delete rows that do not contain certain criteriaExcel 2010:VBA 删除不包含某些条件的行
【发布时间】:2016-04-27 19:13:47
【问题描述】:

我正在尝试根据不包含特定条件的行删除整行,这是我目前拥有的:

Sub Delete_consultants()
  Last = Cells(Rows.Count, "H").End(xlUp).Row
   For i = Last To 1 Step -1
    If (Cells(i, "H").Value) = "Faith Jones" Then
        Cells(i, "A").EntireRow.Delete
    End If

    If (Cells(i, "H").Value) = "Cathy Robbs" Then
        Cells(i, "A").EntireRow.Delete
    End If

    If (Cells(i, "H").Value) = "Nick farmer" Then
        Cells(i, "A").EntireRow.Delete
    End If

     If (Cells(i, "H").Value) = "Jane Till" Then
        Cells(i, "A").EntireRow.Delete
    End If

     If (Cells(i, "H").Value) = "Jack White" Then
        Cells(i, "A").EntireRow.Delete
    End If

     If (Cells(i, "H").Value) = "Dylan Smith" Then
        Cells(i, "A").EntireRow.Delete
    End If

     If (Cells(i, "H").Value) = "Emma Long" Then
        Cells(i, "A").EntireRow.Delete
    End If

     If (Cells(i, "H").Value) = "Nick Winter" Then
        Cells(i, "A").EntireRow.Delete
    End If

     If (Cells(i, "H").Value) = "Niel West" Then
        Cells(i, "A").EntireRow.Delete
    End If

Next i

End Sub

问题是这目前正在删除我想保留的人。但我不知道如何删除其他我发现的所有帖子只有 1 或 2 个标准,你可以在我需要 9 的地方设置!另外,如果可能的话,我似乎无法解决,如果包含这些名称的行从 DirectLink 移动到 Infomation(它们在同一个工作簿中)。

【问题讨论】:

    标签: vba excel delete-row


    【解决方案1】:

    我会将测试放在一个单独的函数中以缩短代码。这是我的建议,你喜欢的就拿去吧

    Function IsMember(v As Variant, vArray As Variant) As Boolean
        Dim vLoop As Variant
        For Each vLoop In vArray
            If v = vLoop Then
                IsMember = True
                Exit Function
            End If
        Next vLoop
    End Function
    
    Sub Delete_Consultants()
        Dim lLast As Long, i As Long
        Dim vConsultants As Variant
        lLast = Cells(Rows.Count, "H").End(xlUp).Row
        vConsultants = Array("Faith Jones", "Cathy Robbs", "Nick Farmer", "Jane Till", _
            "Jack White", "Dylan Smith", "Emma Long", "Nick Winter", "Niel West")
        For i = lLast To 1 Step -1
            If IsMember(Cells(i, "H"), vConsultants) Then
            'if you want to do something with the others use this instead
            'If Not IsMember(Cells(i, "H"), vConsultants) Then
                Cells(i, "A").EntireRow.Delete
                'or to copy
                'Cells(i, "A").EntireRow.Copy Sheets("Information").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            End If
        Next i
    End Sub
    

    【讨论】:

    • 太好了,在我使用之前完美运行:'If Not IsMember(Cells(i, "H"), vConsultants) 然后............然后错误删除方法范围类失败了!
    • @Lozza1234 - 嗯,如果一个有效,另一个应该也有效。您是切换了Ifs 还是只是取消注释第二个?如果是后者,则将第一个注释掉。此外,由于您使用的是对 Cells 的不合格调用,因此您需要确保在运行宏时位于正确的工作表上。
    • 这很奇怪,因为一旦它实际完成就会发生错误,所以我最终得到了我想要的重复行,然后它出错了。
    【解决方案2】:

    考虑:

    Sub Delete_consultants()
      Last = Cells(Rows.Count, "H").End(xlUp).Row
      Dim v As Variant
       For i = Last To 1 Step -1
        v = Cells(i, "H").Value
        With Cells(i, "H")
        If v = "Faith Jones" Then
            .EntireRow.Delete
            GoTo botttom
        End If
    
        If v = "Cathy Robbs" Then
            .EntireRow.Delete
            GoTo botttom
        End If
    
        If v = "Nick farmer" Then
            .EntireRow.Delete
            GoTo botttom
        End If
    
         If v = "Jane Till" Then
            .EntireRow.Delete
            GoTo botttom
        End If
    
         If v = "Jack White" Then
            .EntireRow.Delete
            GoTo botttom
        End If
    
         If v = "Dylan Smith" Then
            .EntireRow.Delete
            GoTo botttom
        End If
    
         If v = "Emma Long" Then
            .EntireRow.Delete
            GoTo botttom
        End If
    
         If v = "Nick Winter" Then
            .EntireRow.Delete
            GoTo botttom
        End If
    
         If v = "Niel West" Then
            .EntireRow.Delete
            GoTo botttom
        End If
    botttom:
        End With
    Next i
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 2016-08-28
      • 2020-03-19
      • 1970-01-01
      • 2018-03-14
      相关资源
      最近更新 更多