In Excel VBA (Visual Basic for Applications), Application.ActiveWorkbook.Path is a property that allows you to retrieve the file path (directory location) of the currently active or open workbook. Here's a breakdown of what it does:
Application This is an object in VBA that represents the Excel application itself. It's essentially a reference to the Excel program running on your computer.
ActiveWorkbook This is a property of the Application object. It refers to the workbook that is currently active or in focus within the Excel application. In other words, it's the workbook that you are currently working with or have selected.
.Path: This is a property of the ActiveWorkbook object. It allows you to access and retrieve the file path of the workbook.
Note: When you use Application.ActiveWorkbook.Path in your VBA code, it will return a string that represents the directory location (folder path) where the active workbook is currently saved or located on your computer's file system. You can use this path in your code for various purposes, such as saving files in the same directory, referencing files in that location, or performing other file-related operations.
Example:
Sub FilePath()
'Will create a message box tha shows the path of the active workbook Path
MsgBox Application.ActiveWorkbook.Path
'Returns file Path C\User\*****\Desktop\xlblueprint
End Sub
Application.ActiveWorkbook.FullName will return back the file path includeing active workbook:
Example
Sub FilePath()
'Will create a message box tha shows the path of the active workbook and Path
MsgBox Application.ActiveWorkbook.FullName
'Returns file Path C\User\*****\Desktop\xlblueprint\ExampleSheet.xlsm
End Sub
Note: If you attempt to run Application.ActiveWorkbook.Path or Application.ActiveWorkbook.FullName on a workbook that has not been saved (i.e., a new, unsaved workbook), Excel will return nothing (""). So you will need to be able to handel this and an exception in your code.
Saved Location: If you want to check to see if the excel wrkbook is saved in a particual location or folder you could use the below.
Sub FilePath()
Dim FilePath As String
'File path of the active workbook
FilePath = Application.ActiveWorkbook.FileFormat
'Checks to see if the Filepath conatians "AppData" in it
If InStr(1, sRowPath, "AppData", vbTextCompare) > 0 Then
'Do something
Else
'Do other thing
End If
End Sub
Default File Path : You can also use Application.DefaultFilePath to find the "Default" file path. This is going to point back to the the mail location the folder is saved in, like Desktop, Downloads, Documents and so on.
Example
Sub FilePath()
'Will create a message box tha shows the Default File Path of the saved workbook
MsgBox Application.DefaultFilePath
'Returns file Path C\User\*****\Desktop
End Sub