dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,375
- Office Version
- 365
- 2016
- Platform
- Windows
I have the following code that deletes rows in a table. When I get down to nothing in the last row and press the button again, I get an error message saying there are no cells found. How do I suppress that message so nothing will happen?
Code:
Private Sub cmdDeleteRow_Click()
'ActiveSheet.Unprotect Password:="npssadmin"
Dim ans As Long
With ActiveSheet.ListObjects("tblCosting").DataBodyRange
ans = .Rows.Count
If ans > 1 Then .Rows(ans).Delete
If ans = 1 Then .Rows(1).Cells.SpecialCells(xlCellTypeConstants).ClearContents
End With
'Selection.ListObject.ListRows(6).Delete
'ActiveSheet.Protect Password:="npssadmin"
Application.EnableEvents = True
End Sub