tableq,
This macro accomplishes what you described. It uses a loop (which I don't really recommend, but its much easier to follow for beginners than autofilter code) and can easily be adapted to fit your requirements. I commented the code as much as possible in order to explain what was going on.
Sub tgr()
'Declare variables
Dim lLastRow As Long 'Will be used to store the row number of the last row
Dim rIndex As Long 'Will be used to loop backwards from the last row to row 2
Dim lCalc As XlCalculation 'Will be used to store the workbook's calculation state
'Disable items to allow macro to run faster
With Application
lCalc = .Calculation 'Store workbook's calculation state
.Calculation = xlCalculationManual 'Set workbook calculation to manual
.EnableEvents = False 'Turn off events (prevents event triggered code from running)
.ScreenUpdating = False 'Turn off screen updates (prevents screen flickering while macro runs)
End With
'Assume code will fail and put in an error handler
On Error GoTo CleanExit
'This section is the workhorse of the code
With ActiveWorkbook.Sheets("Report") 'Make sure the code runs on the correct sheet
lLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 'Get the last used row in column A
For rIndex = lLastRow To 2 Step -1 'Loop through the rows starting at the last row and going backwards to row 2
If Len(Trim(.Cells(rIndex, "BO").Value)) = 0 Then 'Check if cell BO in the current row is empty
.Rows(rIndex).Delete xlShiftUp 'Found it is empty, delete the row
Else
.Cells(rIndex, "HE").FormulaR1C1 = "=CODE(RC[-211])" 'Found it is not empty, enter the formula in cell HE of the current row
If .Cells(rIndex, "HE").Value = 32 Then .Rows(rIndex).Delete xlShiftUp 'If the formula results in 32, delete the row
End If
Next rIndex 'Advance the loop
End With 'Done working with the sheet
'If there was an error at any point, the code will automatically go straight to this section, skipping the rest
'If there was no error, the code goes to this section anyway
'This section is designed to allow the macro to cleanly exit
CleanExit:
'Re-enable the items we turned off earlier
With Application
.Calculation = lCalc 'Set the workbook calculation back to its original state
.EnableEvents = True 'Turn events back on
.ScreenUpdating = True 'Turn screen updates back on
End With
'If there was an error, display the error message and clear the error
If Err > 0 Then
MsgBox Err.Description, , "Error: " & Err.Number
Err.Clear
End If
End Sub
EDIT: Good catch on those, WasWodge, code has been updated with the corrections.
Bookmarks