Get current working directory using VBA 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.
Let us see the macro to find current working directory using Excel VBA. In this example we are using CurDir VBA function.
'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 the case study about current directory.
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
Here are the instructions to use above macro procedure in Visual basic editor.
- Open Visual Basic Editor(VBE) by clicking Alt +F11
- Go to code window by clicking F7
- Copy above specified macro or procedure
- Paste above copied code in code window
- Run macro by clicking F5 or Run command
- You can see output on the screen
You may also find more details about ChDir and CurDir functions.