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

4 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.

    /cozmoz

  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

  3. BestAlejandrina

    I see you don’t monetize your site, don’t waste your traffic,
    you can earn additional bucks every month because you’ve got hi quality content.
    If you want to know how to make extra bucks, search for: Ercannou’s essential adsense alternative

  4. BestEtsuko

    I see you don’t monetize your website, don’t waste your traffic,
    you can earn extra cash every month. You can use the best adsense alternative for any type of website (they approve all
    websites), for more info simply search in gooogle: boorfe’s tips monetize your
    website

Leave a Reply

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