Excel 2010:VB删除不包含某些标准的行

我想删除整个行不包含一定的标准这是我现在有:

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 

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

我会把testing放在一个单独的函数中来缩短代码。 这是我的build议,从你的喜好

 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 

考虑:

 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