Current running macro's workbook or worksheet name


Posted by Mindy on August 28, 2001 10:31 AM

How should I get the current running (executed) macro's workbook or worksheet name?

Thanks in advance

Posted by Barrie Davidson on August 28, 2001 10:45 AM

I assume you are using variables to hold the workbook and/or worksheet name. This will work for you (assumes variables named ThisWorkbook and ThisSheet).

ThisWorkbook = ActiveWorkbook.Name
ThisWorksheet = ActiveSheet.Name

Regards,
Barrie

Posted by Jerid on August 28, 2001 10:47 AM

Mindy, Collect the Activeworkbook.name and Activesheet.name in variables before your macro changes the activebook or sheet.

[EXAMPLE]

sub myMacro
dim sMyBook as String
dim sMySheet as String

'FIRST THING - Collect the book and sheet names
sMyBook = Activeworkbook.name
sMySheet = Activesheet.name

'The rest of your code


'If you want to go back to the original book and sheet
Workbooks(sMyBook).activate
Worksheets(sMySheet).activate
end sub

Hope this helps, Jerid

Posted by Mindy on August 28, 2001 11:56 AM

Occur on only onkey function

I actually keep the workbook name in the variable.
However, I have two procedure to execute the macro.

One will be executed when the workbook_open. This is one is fine.

The other one to execute the same macro is when F5 press, which is where it loose the original workbook name.

Is that matter where I set the Application.onKey function? Even I place the assigning workbook name at the procedure where the onKey function called, it still keep lossing the name.
Any idea of what do I do wrong?

Thank you very much

Mindy



Posted by Jerid on August 28, 2001 2:04 PM

Re: Occur on only onkey function

Mindy, Are you storing the Activeworkbook.Name in a Public Variable? Are you getting any error messages? Are you using Excel97?

There is a bug in Excel97, that if you have a runtime error you lose all Public variables.

A sample of your code might help us help you.

Jerid