Get File Name with Excel VBA

Sometimes it is useful to get the file name using Excel VBA. Two methods will either give the file name, or the full file path and file name.  When I use either of these methods I tend to assign them to a variable as a string.

Get File Name using VBA

Using VBA I would assign the file name to the variable thus so. Assuming you want to capture the filename of the workbook currently open I would add two lines of code.  One to declare the variable, and one to assign the file name to the variable.

Dim FileName as String

FileName = ThisWorkbook.Name

Get File Path & File Name using VBA
To get the file path and file name of the active workbook I would use the same method as above, but change the second line ever so slightly.

Dim FullFileName as String

FullFileName = ThisWorkbook.FullName

So using the examples above will generate the following variables.  These are examples only, and file paths will differ:

FileName: Workbook.xls

FullName: C:\ExcelGeek\VBA\Learning\Workbook.xls

2 thoughts on “Get File Name with Excel VBA

  1. cozmoz

    Hi! I just like to add that the VBA script for getting the filename into a cell should not be placed in the ThisWorkbook but in a Module, otherwise you would receive the #NAME error.


  2. Wojciech

    I got my macros in personal workbook to have them aviable in all Excell files. this is giving me only path to Personal.xls and wrong name regardles the workbook name i’m running it on

Leave a Reply

Your email address will not be published. Required fields are marked *