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

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
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,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

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