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"
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
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?
 
Upvote 0
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"
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,895
Messages
6,122,128
Members
449,066
Latest member
Andyg666

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