VBA CurDir Function

VBA CurDir Function

Overview of VBA CurDir Function:


VBA CurDir function is categorized as File and Direcetory 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 use Macro Codes

Here are the step by step instructions to use the CurDir Function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

VBA File & Directory Functions List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *