Get current working directory using VBA in Excel explained with examples. We use CurDir VBA function to find current working directory or folder. It displays system default directory. It can be changed using ChDir function.
Here we see the VBA macro to get current working directory using VBA in Excel. In this example we are using ‘CurDir’ VBA function to find the working directory.
'Find Get current working directory using VBA Sub Find_Get_current_working_directory_using_VBA() 'Variable declaration Dim sDir As String 'Get current directory sDir = CurDir 'Display output on the screen MsgBox "Current Directory is " & sDir End Sub
Here is the output screenshot for your reference.
Let us see another example to Get current working directory in Excel VBA.
Please find the following two statements.
The first one is
'Get Active Workbook Path sWBPath = ActiveWorkbook.Path
Output: Active Workbook path is C:\Someswari\VBAF1
The second one is
'Get current directory sDir = CurDir
Output: Current Directory is C:\VBAF1
If you want to change current directory from C:\VBAF1 to C:\Someswari\VBAF1\, you can use ChDir VBA function in the following way.
i.e ChDir ActiveWorkbook.Path or ChDir C:\Someswari\VBAF1\
Now the below mentioned two statements displays same output.
Statement 1: sWBPath = ActiveWorkbook.Path
Statement 2: sDir = CurDir
You can refer the following link for the step by step instructions.
Click on the following links of the useful resources. These helps to learn and gain more knowledge.