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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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
 
Upvote 0

Forum statistics

Threads
1,221,241
Messages
6,158,734
Members
451,513
Latest member
EbenAgya

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