Here's another approach:
Code:Sub srr797() Dim lr As Long Dim i As Long lr = Cells(Rows.Count, 5).End(xlUp).Row For i = lr To 2 Step -1 If Range("E" & i) <> "IP-0000063409" Then Range("E" & i).EntireRow.Delete End If Next i End Sub
That's odd - it worked fine for me
Are there any trailing spaces either before and/or after each entry in Col E? If you also want to include Row 1, simply change the 'lngRowStart' variable.
Thanks... this method works, it just takes quite a while... is there any way to make it run quicker?
Thanks again!
Sub srr797()
Dim lr As Long
Dim i As Long
Application.Screenupdating = False
lr = Cells(Rows.Count, 5).End(xlUp).Row
For i = lr To 2 Step -1
If Range("E" & i) <> "IP-0000063409" Then
Range("E" & i).EntireRow.Delete
End If
Next i
Application.Screenupdating = True
End Sub
For that size, try this:
RobertCode:Sub Macro2() '//Declare variables// Dim varDelItem As Variant Dim lngRowStart As Long, _ lngRowLast As Long, _ lngRowActive As Long Dim strMyCol As String Dim rngDelRange As Range '//Set variables// varDelItem = "IP-0000063409" lngRowStart = 2 'Initial data row. Change to suit. strMyCol = "E" 'Column containing relevant data. Change to suit. lngRowLast = Cells(Rows.Count, strMyCol).End(xlUp).Row Application.ScreenUpdating = False For lngRowActive = lngRowStart To lngRowLast If Cells(lngRowActive, strMyCol) <> varDelItem Then 'Cater for initial setting of 'rngDelRange' range If rngDelRange Is Nothing Then Set rngDelRange = Cells(lngRowActive, strMyCol) Else Set rngDelRange = Union(rngDelRange, Cells(lngRowActive, strMyCol)) End If End If Next lngRowActive 'If the 'rngDelRange' range has been set (i.e. has something in it), then... If Not rngDelRange Is Nothing Then '...delete the rows within it. rngDelRange.EntireRow.Delete xlShiftUp 'Else... Else '...inform the user that no rows are to be deleted as there was no _ matching criteria in the dataset. MsgBox "No rows were deleted as every Row in Column " & strMyCol & " matched """ & varDelItem & """.", vbExclamation, "Delete Row Editor" End If Application.ScreenUpdating = True End Sub
Sub Macro2()
'//Declare variables//
Dim varDelItem1 As Variant, _
varDelItem2 As Variant
Dim lngRowStart As Long, _
lngRowLast As Long, _
lngRowActive As Long
Dim strMyCol As String
Dim rngDelRange As Range
'//Set variables//
varDelItem1 = "IP-0000063407"
varDelItem2 = "IP-0000063408"
lngRowStart = 2 'Initial data row. Change to suit.
strMyCol = "E" 'Column containing relevant data. Change to suit.
lngRowLast = Cells(Rows.Count, strMyCol).End(xlUp).Row
Application.ScreenUpdating = False
For lngRowActive = lngRowStart To lngRowLast
If Trim(Cells(lngRowActive, strMyCol)) <> varDelItem1 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem2 Then
'Cater for initial setting of 'rngDelRange' range
If rngDelRange Is Nothing Then
Set rngDelRange = Cells(lngRowActive, strMyCol)
Else
Set rngDelRange = Union(rngDelRange, Cells(lngRowActive, strMyCol))
End If
End If
Next lngRowActive
'If the 'rngDelRange' range has been set (i.e. has something in it), then...
If Not rngDelRange Is Nothing Then
'...delete the rows within it.
rngDelRange.EntireRow.Delete xlShiftUp
'Else...
Else
'...inform the user that no rows are to be deleted as there was no _
matching criteria in the dataset.
MsgBox "No rows were deleted as every applicable row in Column " & strMyCol & " matched """ & varDelItem1 & """" & " and """ & varDelItem2 & """.", vbExclamation, "Delete Row Editor"
End If
Application.ScreenUpdating = True
End Sub
Hi srr797,
Try this (initially on a copy of your data in case the results are not as expected) which automatically removes any trailing spaces and uses two criteria. Note that even on large datasets this code will be quite fast as it deletes all the required rows in one go as opposed to a row at a time:
HTHCode:Sub Macro2() '//Declare variables// Dim varDelItem1 As Variant, _ varDelItem2 As Variant Dim lngRowStart As Long, _ lngRowLast As Long, _ lngRowActive As Long Dim strMyCol As String Dim rngDelRange As Range '//Set variables// varDelItem1 = "IP-0000063407" varDelItem2 = "IP-0000063408" lngRowStart = 2 'Initial data row. Change to suit. strMyCol = "E" 'Column containing relevant data. Change to suit. lngRowLast = Cells(Rows.Count, strMyCol).End(xlUp).Row Application.ScreenUpdating = False For lngRowActive = lngRowStart To lngRowLast If Trim(Cells(lngRowActive, strMyCol)) <> varDelItem1 And _ Trim(Cells(lngRowActive, strMyCol)) <> varDelItem2 Then 'Cater for initial setting of 'rngDelRange' range If rngDelRange Is Nothing Then Set rngDelRange = Cells(lngRowActive, strMyCol) Else Set rngDelRange = Union(rngDelRange, Cells(lngRowActive, strMyCol)) End If End If Next lngRowActive 'If the 'rngDelRange' range has been set (i.e. has something in it), then... If Not rngDelRange Is Nothing Then '...delete the rows within it. rngDelRange.EntireRow.Delete xlShiftUp 'Else... Else '...inform the user that no rows are to be deleted as there was no _ matching criteria in the dataset. MsgBox "No rows were deleted as every applicable row in Column " & strMyCol & " matched """ & varDelItem1 & """" & " and """ & varDelItem2 & """.", vbExclamation, "Delete Row Editor" End If Application.ScreenUpdating = True End Sub
Robert
Works perfectly! Thanks so much for your time and help! It also works much faster than some of the other codes I received. Thanks again
Sub Macro2()
'//Declare variables//
Dim varDelItem1 As Variant, _
varDelItem2 As Variant, _
varDelItem3 As Variant, _
varDelItem4 As Variant, _
varDelItem5 As Variant, _
varDelItem6 As Variant, _
varDelItem7 As Variant, _
varDelItem8 As Variant
Dim lngRowStart As Long, _
lngRowLast As Long, _
lngRowActive As Long
Dim strMyCol As String
Dim rngDelRange As Range
'//Set variables//
varDelItem1 = "Level 1"
varDelItem2 = "Level 2"
varDelItem3 = "Level 3"
varDelItem4 = "Level 4"
varDelItem5 = "Level 5"
varDelItem6 = "Level 6"
varDelItem7 = "Level 7"
varDelItem8 = "Level 8"
lngRowStart = 2 'Initial data row. Change to suit.
strMyCol = "AB" 'Column containing relevant data. Change to suit.
lngRowLast = Cells(Rows.Count, strMyCol).End(xlUp).Row
Application.ScreenUpdating = False
For lngRowActive = lngRowStart To lngRowLast
If Trim(Cells(lngRowActive, strMyCol)) <> varDelItem1 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem2 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem3 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem4 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem5 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem6 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem7 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem8 Then
'Cater for initial setting of 'rngDelRange' range
If rngDelRange Is Nothing Then
Set rngDelRange = Cells(lngRowActive, strMyCol)
Else
Set rngDelRange = Union(rngDelRange, Cells(lngRowActive, strMyCol))
End If
End If
Next lngRowActive
'If the 'rngDelRange' range has been set (i.e. has something in it), then...
If Not rngDelRange Is Nothing Then
'...delete the rows within it.
rngDelRange.EntireRow.Delete xlShiftUp
'Else...
Else
'...inform the user that no rows are to be deleted as there was no _
matching criteria in the dataset.
MsgBox "No rows were deleted as every applicable row in Column " & strMyCol & " matched """ & varDelItem1 & """" & " and """ & varDelItem2 & """.", vbExclamation, "Delete Row Editor"
End If
Application.ScreenUpdating = True
End Sub
Option Explicit
Sub Macro2()
'//Declare variables//
Dim varDelItem1 As Variant, _
varDelItem2 As Variant, _
varDelItem3 As Variant, _
varDelItem4 As Variant, _
varDelItem5 As Variant, _
varDelItem6 As Variant, _
varDelItem7 As Variant, _
varDelItem8 As Variant
Dim lngRowStart As Long, _
lngRowLast As Long, _
lngRowActive As Long
Dim strMyCol As String
Dim rngDelRange As Range
'//Set variables//
varDelItem1 = "Level 1"
varDelItem2 = "Level 2"
varDelItem3 = "Level 3"
varDelItem4 = "Level 4"
varDelItem5 = "Level 5"
varDelItem6 = "Level 6"
varDelItem7 = "Level 7"
varDelItem8 = "Level 8"
lngRowStart = 2 'Initial data row. Change to suit.
strMyCol = "AB" 'Column containing relevant data. Change to suit.
lngRowLast = Cells(Rows.Count, strMyCol).End(xlUp).Row
Application.ScreenUpdating = False
For lngRowActive = lngRowStart To lngRowLast
If IsError(Cells(lngRowActive, strMyCol)) = True Then
'Cater for initial setting of 'rngDelRange' range
If rngDelRange Is Nothing Then
Set rngDelRange = Cells(lngRowActive, strMyCol)
Else
Set rngDelRange = Union(rngDelRange, Cells(lngRowActive, strMyCol))
End If
Else
If Trim(Cells(lngRowActive, strMyCol)) <> varDelItem1 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem2 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem3 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem4 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem5 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem6 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem7 And _
Trim(Cells(lngRowActive, strMyCol)) <> varDelItem8 Then
'Cater for initial setting of 'rngDelRange' range
If rngDelRange Is Nothing Then
Set rngDelRange = Cells(lngRowActive, strMyCol)
Else
Set rngDelRange = Union(rngDelRange, Cells(lngRowActive, strMyCol))
End If
End If
End If
Next lngRowActive
'If the 'rngDelRange' range has been set (i.e. has something in it), then...
If Not rngDelRange Is Nothing Then
'...delete the rows within it.
rngDelRange.EntireRow.Delete xlShiftUp
'Else...
Else
'...inform the user that no rows are to be deleted.
MsgBox "No rows were deleted.", vbExclamation, "Delete Row Editor"
End If
Application.ScreenUpdating = True
End Sub