How to know which workbook the macro was called from?

  • Thread starter Thread starter mika.
  • Start date Start date
M

mika.

Hello,

I have two workbooks open, and I want my macro to be able
to figure out which workbook it was called from. I want
the names of the two workboks to be anything, and for the
code to still work.

Basically in this macro, I loop through all the workbooks,
and do something to all the workbooks except the one which
I called the macro from.

I tried this
If Workbooks(Count - 1) <> Application.ActiveWorkbook Then
but to no avail. Any ideas? Thanks.
 
Hi,

Use this :

If Workbooks(x).Name <> ActiveWorkbook.Name Then



Where 'x' comes from your loop.
 
If you run in to problems where the active workbook is not
the one from which the macro is run, the ThisWorkbook
property can come in handy. ThisWorkbook always returns
the workbook in which the code is running. Its usage is
pretty much the same as ActiveWorkbook.

HTH,
MPC
 
Back
Top