Greetings, I'm currently running this code when the sheet deactivates to delete empty rows from a worksheet:
Private Sub Worksheet_Deactivate()
Application.ScreenUpdating = False
Sheets("Records").Unprotect Password:="secret"
With Worksheets("Records")
.AutoFilterMode = False
With Range("B2", Range("B" & Rows.Count).End(xlUp))
.AutoFilter 1, "*Nil*"
On Error Resume Next
.Offset(1).SpecialCells(12).EntireRow.Delete
End With
Sheets("Records").AutoFilterMode = False
End With
Sheets("Records").Protect Password "secret"
Application.ScreenUpdating = True
End Sub
The problem is, if there are less than two records in the worksheet, it will delete the heading row, breaking my dynamic named ranges and tables based on same.
I tried adding a check like this:
If ActiveSheet.UsedRange.Rows.Count < 3 Then Exit Sub
But this doesn't seem to work. Appreciate code / ideas to help!
EDIT: I should add that a new row is generated automatically when the last record has been completed, in anticipation of the user needing to enter another record. But if they don't actually need to enter anything, then I have to remove that blank row so that my dynamic named ranges don't include it in the range, thus messing up tables/pivot tables.
Bookmarks