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 formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,215,433
Messages
6,124,861
Members
449,194
Latest member
HellScout

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