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.
Macro to find current working directory using Excel VBA
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.
Case study on Get current working directory
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
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays VBA Text Files VBA Tables
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog