Macros with multiple workbooks open

gbullr

New Member
Joined
Mar 2, 2011
Messages
2
Suppose I have two workbooks open. In one (File1) I have a macro that runs every x minutes and a sheet called sheet1. In the other (File2) I have other macros but not a sheet called sheet1. If my active workbook is File2 but the macro that runs is the one from File1 I will get a runtime error for obvious reasons. Is there a way to have the macro that runs every x minutes to run ONLY on File1?

I could do if ThisWorkbook.Name <> "File1.xlsm" then ____

What is the code that makes File1 the activeworkbook.

Thank you.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try something like this

Code:
Dim s As Workbook
Set s = ThisWorkbook

Then when you refer to a sheet name you would use

Code:
s.Sheets("YourTabNameHere")

Does that answer your question?
 
Upvote 0
If there is only one file open it works fine because the macro only runs on that one file. When I have two files open and I am working on the other file w/ the macro working in the background it runs on the file that I have active and consecuently gives me a run-time error.
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,856
Members
452,948
Latest member
UsmanAli786

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