Macro limitations

B

Bill Chatfield

I am using Excel 2002 - SP2.

I created several macros, stored them in one workbook (Races), and assigned
each to a custom button on the tool bar. Is there a way to have these macros
only run in the assigned (Races) workbook and not in all the other
workbooks?

TIA

Bill Chatfield
 
J

J.E. McGimpsey

One way:

Assuming you only want the macros to run if Races is Active, start
each macro with

If Not ThisWorkbook Is ActiveWorkbook Then Exit Sub


If instead, you want the macro to run with any workbook active, but
only affect Races, fully qualify your references, i.e, instead of

Sheets("Sheet1").Range("J10").Value = 1
Sheets("Sheet2").Range("A1").Value = 2

use

ThisWorkbook.Sheets("Sheet1").Range("J10").Value = 1
ThisWorkbook.Sheets("Sheet2").Range("A1").Value = 2

or, more efficiently

With ThisWorkbook
.Sheets("Sheet1").Range("J10").Value = 1
.Sheets("Sheet2").Range("A1").Value = 2
End with
 
B

Bill Chatfield

J.E.,

Thank you for the reply. I know very little about editing macros, but think
I understand what you are saying - time to learn more stuff - this is good.

Thank you again for taking time to help me

Bill Chatfield


One way:

Assuming you only want the macros to run if Races is Active, start
each macro with

If Not ThisWorkbook Is ActiveWorkbook Then Exit Sub


If instead, you want the macro to run with any workbook active, but
only affect Races, fully qualify your references, i.e, instead of

Sheets("Sheet1").Range("J10").Value = 1
Sheets("Sheet2").Range("A1").Value = 2

use

ThisWorkbook.Sheets("Sheet1").Range("J10").Value = 1
ThisWorkbook.Sheets("Sheet2").Range("A1").Value = 2

or, more efficiently

With ThisWorkbook
.Sheets("Sheet1").Range("J10").Value = 1
.Sheets("Sheet2").Range("A1").Value = 2
End with
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top