How to suppress an error message

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,375
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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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,223,889
Messages
6,175,226
Members
452,620
Latest member
dsubash

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