Option Explicit
Sub REMOVE_LEVEL()
'//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
The data in the cells are all vlookup data if that matters!
rngDelRange.EntireRow.Delete xlShiftUp
rngDelRange.Delete (xlShiftUp)
Option Explicit
Sub Macro2()
Const lngStartRow As Long = 2 'Starting row number for your data. Change to suit.
Dim lngMyCol As Long, _
lngLastRow As Long
Dim xlnCalcMethod As XlCalculation
Dim varDelItem As Variant
With Application
xlnCalcMethod = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
varDelItem = 3351 'If this item is not found within the text of Col. H, that row(s) will be deleted. Change to suit.
lngMyCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
lngLastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
With Columns(lngMyCol)
With Range(Cells(lngStartRow, lngMyCol), Cells(lngLastRow, lngMyCol))
.Formula = "=IF(ISERROR(SEARCH(""" & varDelItem & """,H" & lngStartRow & ")),NA(),"""")"
.Calculate
.Value = .Value
End With
On Error Resume Next 'Turn error reporting off - OK to ignore 'No cells found' message
.SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
On Error GoTo 0 'Turn error reporting back on
.Delete
End With
With Application
.Calculation = xlnCalcMethod
.ScreenUpdating = True
End With
MsgBox "All entries in Col. H not containing """ & varDelItem & """ have now been deleted.", vbInformation
End Sub