VBA Help: "On Error Resume Next" ??

monirg

Well-known Member
Joined
Jan 11, 2005
Messages
629
Hello;

1) Please examine the following simple macro in a standard module:
Code:
Sub UpdateNow()
    If Range("j15") = "" Then Exit Sub
    Worksheets("Burrill-et-al").Range("F1").Value = Format(Now, "hh:mm:ss")  'use any cell on the active w/s
    Application.OnTime Now + TimeValue("00:00:01"), "UpdateNow"
 End Sub
It produces the error:
....."run-time error: '1004'
.....Application-defined or object-defined error"
with reference to the Worksheets statement.

2) By inserting "On Error Resume Next" before the Worksheets statement, there're no errors, and the macro and the entire algorithm appears to be working fine:
Code:
Sub UpdateNow()
    On Error Resume Next
    If Range("j15") = "" Then Exit Sub
    Worksheets("Burrill-et-al").Range("F1").Value = Format(Now, "hh:mm:ss")  'use any cell on the active w/s
    Application.OnTime Now + TimeValue("00:00:01"), "UpdateNow"
End Sub
3) My concern is that the "On Error Resume Next" (which I don't often use) maybe telling VBA to ignore errors I didn't mean to ignore and I shouldn't, since I couldn't figure out why the Worksheets statement produced the run-time error in the first place!

Can someone please identify the cause of the problem, and whether it is "generally" safe to use "On Error Resume Next" when the cause of the error is not known (to me) ??

Thank you kindly.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try these things first:

1. Make sure you have the name of the sheet spelled correctly.
2. Make sure you have no missing references.
 
Upvote 0
The references are correct, and the w/s name is correct (copy/paste).
Not only it is the ONLY w/s in the w/b, but also if the sheet's name was misspelled, I'd get the run-time error: "Subscript is out of range" (or something to that effect) instead of error: '1004'.

Regards.
 
Upvote 0
it is the ONLY w/s in the w/b
If the active sheet were a chart sheet rather than a worksheet, you'd get a 1004 error on this line:
Code:
If Range("j15") = "" Then Exit Sub
 
Upvote 0
There are NO charts in the workbook, and the ONLY w/s is the active sheet!

Regards.
 
Upvote 0
This worked fine for me when J15 is empty or not empty. I'm using Excel 2007:
Code:
Sub UpdateNow()
    If IsEmpty(Range("J15")) Then Exit Sub
    Sheets(1).Range("F1").Value = Format(Now, "hh:mm:ss")
    Application.OnTime Now + TimeValue("00:00:01"), "UpdateNow"
End Sub
If your sheet is the only sheet in the book, you don't need to define it in the code, this should also work:

Code:
Sub UpdateNow()
    If IsEmpty(Range("J15")) Then Exit Sub
    Range("F1").Value = Format(Now, "hh:mm:ss")
    Application.OnTime Now + TimeValue("00:00:01"), "UpdateNow"
End Sub
 
Upvote 0
JackDanIce;

IsEmpty w & w/o w/s reference made no difference (XL 2003). Same '1004' error unless I use "On Error Resume Next".

If it is not too much trouble, could you please try your code in XL 2007 with protected w/s and protected w/b and let me know if you got a run-time error ??
(cells F1 and J15 are locked and hidden)

Thank you.
 
Upvote 0
Why didn't you say anything about protected before? Why don't you unprotect your workbook and worksheet before running the test on the blank cell and then reprotecting it etc?
 
Upvote 0
If F1 is locked, how do you expect to change it?
 
Upvote 0

Forum statistics

Threads
1,224,621
Messages
6,179,938
Members
452,949
Latest member
beartooth91

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