Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting Special Cells

Status
Not open for further replies.

batau65

Technical User
Nov 1, 2005
2
US
I've a macro to select in column D all constants and delete them. The macro runs fine whenever there are constants in this column, and is as follow:

Code:
Columns("D:D").Select
Selection.SpecialCells(xlCellTypeConstants, 1).Select
Selection.ClearContents

However when there are no constant, the macro ends with 'Run-time error 1004.' No cells were found.

How do I modify this macro to stop (or skip to the next macro line) if it does not find any constant, and if it does, delete them (as currently done)?
 

A simple way is to use error trapping (See the help file):
Code:
  Columns("D:D").Select
  On Error GoTo Cleared
  Selection.SpecialCells(xlCellTypeConstants, 1).Select
  Selection.ClearContents
Cleared:
  On Error GoTo 0
 
Perhaps this ?
Dim R As Range
On Error Resume Next
Set R = Columns("D:D").SpecialCells(xlCellTypeConstants, 1)
If Err.Number = 0 Then R.ClearContents
Err.Clear
On Error Resume Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thank you, Zathras and PHV for the super fast reply. I tried both method and both worked. Again, thx.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top