No cells were found

maxx21

New Member
Joined
Aug 6, 2002
Messages
27
Hi everyone,

the following code highlights particular cells in my spreadsheet, but sometimes this sheet doesn't contain any cells, just 2 header rows. Then it says "Runtime error 1004, no cells were found":

<pre>Sub highlight()
Dim Rng As Range, cell As Range
Set Rng = Range(Range("K3"), Range("K3").End(xlDown)).SpecialCells(xlCellTypeConstants)
For Each cell In Rng
If cell < cell.Offset(0, -1) Then cell.Font.ColorIndex = 3
Next cell
End Sub</pre>
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Mudface

MrExcel MVP
Joined
Feb 18, 2002
Messages
3,339
Try this instead: -

Code:
Sub highlight()
Dim Rng As Range, cell As Range
    On Error Resume Next
    Set Rng = Range(Range("K3"), Range("K3").End(xlDown)).SpecialCells(xlCellTypeConstants)
    If Err <> 0 Then MsgBox "No cells found!": Exit Sub
    On Error GoTo 0
  For Each cell In Rng
      If cell < cell.Offset(0, -1) Then cell.Font.ColorIndex = 3
  Next cell
End Sub
 

Forum statistics

Threads
1,181,338
Messages
5,929,389
Members
436,665
Latest member
MonkeyNotDonkey

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
Top