Display file path to the current workbook
In order to create a simple subroutine that shows a path to the active workbook follows this steps.
- Open VBA Editor (Alt + F11),
- Insert the following code into your project.
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 with:
- full file 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.
Function ShowCurrentPath() ShowCurrentPath = ActiveWorkbook.FullName End Function
As you can see, the function name is inserted into the cell.