How to get the path of current worksheet in VBA?

ExcelVba

Excel Problem Overview


I wrote a macro as an add-in, and I need to get the path of the current worksheet on which it is being executed. How do I do this? How do I get the file path (just the directory)?

Excel Solutions


Solution 1 - Excel

Use Application.ActiveWorkbook.Path for just the path itself (without the workbook name) or Application.ActiveWorkbook.FullName for the path with the workbook name.

Solution 2 - Excel

Always nice to have:

Dim myPath As String     
Dim folderPath As String 

folderPath = Application.ActiveWorkbook.Path    
myPath = Application.ActiveWorkbook.FullName

Solution 3 - Excel

If you want to get the path of the workbook from where the macro is being executed - use

Application.ThisWorkbook.Path

Application.ActiveWorkbook.Path can sometimes produce unexpected results (e.g. if your macro switches between multiple workbooks).

Solution 4 - Excel

The quickest way

path = ThisWorkbook.Path & "\"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex GordonView Question on Stackoverflow
Solution 1 - ExcelBradCView Answer on Stackoverflow
Solution 2 - ExcelAlex22View Answer on Stackoverflow
Solution 3 - Excelavalanche1View Answer on Stackoverflow
Solution 4 - ExcelPablo VilasView Answer on Stackoverflow