How to suppress an error message

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. 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
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try using

Code:
Private Sub cmdDeleteRow_Click()
'ActiveSheet.Unprotect Password:="npssadmin"
   Dim ans As Long
    With ActiveSheet.ListObjects("tblCosting").DataBodyRange
        ans = .Rows.Count
        If ans=0 then exit sub
        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
 
Upvote 0
Try using

Code:
Private Sub cmdDeleteRow_Click()
'ActiveSheet.Unprotect Password:="npssadmin"
   Dim ans As Long
    With ActiveSheet.ListObjects("tblCosting").DataBodyRange
        ans = .Rows.Count
        If ans=0 then exit sub
        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

Still says no cells found and highlights this code:
Code:
.Rows(1).Cells.SpecialCells(xlCellTypeConstants).ClearContents
 
Upvote 0
shouldn't this be
Code:
.Rows(1).Cells.SpecialCells(xlCellTypeConstants).ClearContents
Code:
.Rows([color=red]ans[/color]).Cells.SpecialCells(xlCellTypeConstants).ClearContents
 
Upvote 0

Forum statistics

Threads
1,221,525
Messages
6,160,329
Members
451,637
Latest member
hvp2262

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top