Grab workbook name

CaterinaAnna

Board Regular
Joined
Jun 15, 2009
Messages
85
Sorry, another simple question from a novice ...

I want to use the name of an active workbook as a string later in a piece of code, but can't get the right syntax to find it and set it as a variable.

Name = ThisWorkbook.Name doesn't work because the code is in a different, master workbook and I don't really want to copy it to each individual book I want to process.

Thanks in advance for any help
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

CaterinaAnna

Board Regular
Joined
Jun 15, 2009
Messages
85
That's what I thought, but it didn't work the first time I tried it: just returned the name of the book storing the macro. It's OK now though ...

Thank you.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,703
Office Version
  1. 365
Platform
  1. Windows
That's what I thought, but it didn't work the first time I tried it: just returned the name of the book storing the macro. It's OK now though ...
It returns the name of whatever the ACTIVE workbook is. So if it returned the name of the workbook storing the macro, then that is because when it was run, the macro workbook was the active workbook at the time.

I find the best time to "capture" the name of the other workbook is when you first open it. Then you should be assured of having the correct workbook active and capture the correct name.
 

CaterinaAnna

Board Regular
Joined
Jun 15, 2009
Messages
85
It returns the name of whatever the ACTIVE workbook is. So if it returned the name of the workbook storing the macro, then that is because when it was run, the macro workbook was the active workbook at the time..

Oh I'm aware of that, which is why it confused me: the one I wanted did seem to be active, but then it decided that no, what I was doing was running the macro, therefore that one was active. Might have been a stray cell select left behind that I later cleared out or something. Never mind, sorted now!

I find the best time to "capture" the name of the other workbook is when you first open it. Then you should be assured of having the correct workbook active and capture the correct name.

That is indeed where the command resides :)
 

Watch MrExcel Video

Forum statistics

Threads
1,133,564
Messages
5,659,567
Members
418,508
Latest member
deepakfer

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
Top