Active workbook

J

Jack Wolf

Is there a way to tell if the workbook containing a macro
is the active workbook? I am calling a subrouting using
Application.OnTime (and calling itself in another interval
until it is stopped by using the Scheduled argument in
another OnTime call) but I would like it to do something
different if the workbook is not active. I know that the
macro continues to call itself if another workbook is
active or even if another application is active.

Thanks
JW
 
C

Chip Pearson

Jack,

ActiveWorkbook always refers to the workbook that is active
regardless of whether it contains the running code. ThisWorkbook
always refers to the workbook that contains the code, regardless
of what workbook happens to be active. Therefore, you can simply
compare the Name properties to see if ThisWorkbook is the
ActiveWorkbook. E.g.,

If ActiveWorkbook.Name = ThisWorkbook.Name Then
' active workbook contains the running code
Else
' active workbook does not contain running code
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com (e-mail address removed)
 
J

Jack Wolf

Chip-

Thanks for the response. The crucial question that I still
have is what happens when OnTime calls the procedure when
the user is working in another application. Will
ActiveWorkbook refer to the last workbook being used
before switching out of Excel or will it return null or
empty or something that will indicate that Excel does not
have the current focus?

Jack
 
C

Chip Pearson

Jack,

It will work as if the user never switched out of Excel, i.e.,
Excel is still the active application. It doesn't matter if the
user switches to another application.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com (e-mail address removed)
 

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