Display file path to the current workbook
In order to create a simple subroutine that shows a path to the active workbook follows these steps.
- Open VBA Editor (Alt + F11),
- Insert the following code into your project.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Sub ShowCurrentPath() Dim full_path As String Dim directory_path As String Dim file_name As String full_path = ActiveWorkbook.FullName directory_path = ActiveWorkbook.Path file_name = ActiveWorkbook.Name MsgBox full_path MsgBox directory_path MsgBox file_name End Sub |
It will display three dialog boxes:
- the full file path,
- directory_path,
- file_name + extension
You can also create functions to show you data inside a cell. First, create a module for your worksheet. This easy function will show you a path to the current file inside a cell.
1 2 3 |
Function ShowCurrentPath() ShowCurrentPath = ActiveWorkbook.FullName End Function |
As you can see, the function name is inserted into the cell.