Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the excel file name / path in VBA

Tags:

excel

vba

Say, I'm writing a VBA inside my excel file sample.xls. Now I want to get the full path of sample.xls in my VBA. How do I do it?

like image 202
Veera Avatar asked Dec 13 '09 05:12

Veera


People also ask

How do you define file names in VBA?

Use VBA DIR function when you want to get the name of the file or a folder, using their path name. To give you an example, if you have an Excel file in a folder, you can use the VBA DIR function to get the name of that Excel file (or any other type of file).


3 Answers

If you mean VBA, then you can use FullName, for example:

strFileFullName = ThisWorkbook.FullName

(updated as considered by the comments: the former used ActiveWorkbook.FullName could more likely be wrong, if other office files may be open(ed) and active. But in case you stored the macro in another file, as mentioned by user @user7296559 here, and really want the file name of the macro-using file, ActiveWorkbook could be the correct choice, if it is guaranteed to be active at execution time.)

like image 51
Fionnuala Avatar answered Sep 26 '22 13:09

Fionnuala


this is a simple alternative that gives all responses, Fullname, Path, filename.

Dim FilePath, FileOnly, PathOnly As String

FilePath = ThisWorkbook.FullName
FileOnly = ThisWorkbook.Name
PathOnly = Left(FilePath, Len(FilePath) - Len(FileOnly))
like image 27
APW Avatar answered Sep 25 '22 13:09

APW


   strScriptFullname = WScript.ScriptFullName 
   strScriptPath = Left(strScriptFullname, InStrRev(strScriptFullname,"\")) 
like image 8
Mitch Wheat Avatar answered Sep 26 '22 13:09

Mitch Wheat