VBA CurDir Function

VBA CurDir Function in Excel

VBA CurDir function in Excel is categorized as File and Directory function. This built-in VBA CurDir function returns the current path in Excel VBA.

This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA CurDir Function in any number of times in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the CurDir function, where we can use this CurDir Function and real-time examples in Excel VBA.

Syntax of VBA CurDir Function

The syntax of the CurDir Function in VBA is

CurDir([Drive])

The CurDir Function changes the current drive name .

Parameters or Arguments:

The CurDir function/statement has one argument in Excel VBA.
where
Drive: It is an optional parameter. The drive argument represents the drive name. If ignored, considers the current drive.

Where we can apply or use VBA CurDir Function?

We can use this CurDir Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.

Example 1: Display the Current Drive Path

Here is a simple example of the VBA CurDir function. This below example displays default current drive.

'Display the Current Path
Sub VBA_CurDir_Function_Ex1()

    'Variable declaration
    Dim sDrive As String
             
    sDrive = CurDir()
   
    'Display output message
    MsgBox "Current drive is : " & sDrive, vbInformation, "VBA CurDir Function"
    
End Sub

Output: Here is the screen shot of the first example output.
VBA CurDir Function

Example 2: Display the Current Path on ‘C’ drive

Here is a simple example of the VBA CurDir function. This below example displays current path in ‘C’ drive.

'Display the Current Path on 'C' drive
Sub VBA_CurDir_Function_Ex2()
    
    'Variable declaration
    Dim sDrive As String
    Dim sOutput As String
    
    sDrive = "C"
      
    sOutput = CurDir(sDrive)
   
    'Display output message
    MsgBox "Current Path on 'C' drive is : " & sOutput, vbInformation, "VBA CurDir Function"
    
End Sub

Output: Here is the screen shot of the second example output.
VBA CurDir Function

Example 3: Display the Current Path on ‘G’ Drive

Here is a simple example of the VBA CurDir function. This below example displays current path in ‘G’ drive. It displays an error. Because my system doesn’t have G drive. It displays an error as ‘Run-time error: Device unavailable’

'Display the Current Path on 'G' Drive
Sub VBA_CurDir_Function_Ex3()
    
    'Variable declaration
    Dim sDrive As String
    Dim sOutput As String
    
    sDrive = "G"
      
    sOutput = CurDir(sDrive)
   
    'Display output message
    MsgBox "Current Path on 'G' drive is : " & sOutput, vbInformation, "VBA CurDir Function"
    
End Sub

Output: Here is the screen shot of the third example output.
VBA CurDir Function

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 in Excel Blog

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers

Leave a Reply