VBA: Get Active Workbook Name

nsontag

New Member
Joined
Jan 3, 2011
Messages
5
Hi All,

I need to return the name of an active workbook, but I cannot use ThisWorkbook because my code resides in an add-in.

I have managed to make a function to accomplish this, but it seems messier than it ought to be. Does any one know of a slicker way to do this?

I'm using Excel 2010 and windows 7

The code for my current function is below:

Function GetBook(myRange As Range) As String
' Returns name of the workbook in which a range is located as a String.

Dim address As String
Dim nameSplit As Variant
Dim bookName As String

address = myRange.address(external:=True)

nameSplit = Split(address, "]")

bookName = Replace(nameSplit(0), "'", "", 1)

GetBook = Replace(bookName, "[", "", 1)

End Function


Thanks,

Nolan
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try:
Code:
Function GetBook() As String
GetBook = ActiveWorkbook.Name
End Function
 
Upvote 0
GetBook = MyRange.Parent.Parent.Name

The first .Parent is the worksheet of MyRange. The second .Parent is the workbook of the worksheet.
 
Upvote 0
Thanks guys.

JoeMo: your method works, but only if the book is active (I realize that is exactly what I asked for, but I also need it for instances in which I'm working in an inactive book...my bad!)

Alpha: Exactly what I was looking for. Thanks for reading beyond my poor communication skills!
 
Upvote 0

Forum statistics

Threads
1,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

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
Back
Top