Stop code running in other workbooks

jjokol

Board Regular
Joined
Dec 21, 2008
Messages
213
The following code is used at the beginning of a Module to make it run every second. It works fine, but it also does the same to any other workbook that is open.

How do I restrict its use to only 1 workbook ("BA - Data.xls")?

Code:
dTime = Now + TimeValue("00:00:01")
Application.Workbooks("BA - Data.xls").OnTime dTime, "new_2009"
 

Some videos you may like

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
A workbook doesn't have an OnTime method does it? The method is a member of the Application object.
 

jjokol

Board Regular
Joined
Dec 21, 2008
Messages
213
Yes, Andrew, you're right that OnTime belongs to Applications. I've tried to specify the Workbook to make the code apply only to that one, but without success.

Is there a way I can restrict the OnTime code to one workbook?
 

jjokol

Board Regular
Joined
Dec 21, 2008
Messages
213

ADVERTISEMENT

I've had an idea. If I put an If statement around the code, as follows, then that might work. Problem is that I can't get the right syntax for the If statement (the True is not liked)

Code:
If ActiveWorkbook.Worksheets("Not In Play") Is True Then
dTime = Now + TimeValue("00:00:01")
Application.OnTime dTime, "new_2009"
 

jjokol

Board Regular
Joined
Dec 21, 2008
Messages
213
Thanks Andrew. My idea still doesn't work. As soon as another workbook is opened, the code stops working.

There must be a way round this.
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
I expect there is a way around it, but you still haven't said where your code is or what your new_2009 procedure is doing.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,660
Messages
5,626,148
Members
416,165
Latest member
hamburger138

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