VBA ChDrive Function

VBA ChDrive Function in Excel

VBA ChDrive function in Excel is categorized as File and Directory function. This built-in VBA ChDrive statement changes the current drive in Excel.

This function can be used in either procedure or function in a VBA editor window in Excel. We can use this VBA ChDrive 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 ChDrive function, where we can use this ChDrive Function and real-time examples in Excel VBA.

Syntax of VBA ChDrive Function

The syntax of the ChDrive Function in VBA is

ChDir(Drive)

Note: The ChDrive Function changes the current drive name .

Parameters or Arguments:

The ChDrive function/statement has one argument in Excel VBA.
where
Drive: It is a mandatory string type parameter. The drive argument represents the drive that you want to change.

Where we can apply or use VBA ChDrive Function?

We can use this ChDrive 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: Change the Current Drive to ‘C’

Here is a simple example of the VBA ChDrive function. This below example sets the current drive to ‘C’.

'Change the Current Drive to "C"
Sub VBA_ChDrive_Function_Ex1()
    
    'Variable declaration
    Dim sDrive As String
    
    sDrive = "C"
      
    ChDrive sDrive
   
    'Display output message
    MsgBox "Current Drive is : " & sDrive, vbInformation, "VBA ChDrive Function"
    
End Sub

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

Example 2: Change the Current Drive to ‘D’

Here is a simple example of the VBA ChDrive function. This below example sets the current drive to ‘D’

'Change the Current Drive to "D"
Sub VBA_ChDrive_Function_Ex2()
    
    'Variable declaration
    Dim sDrive As String
    
    sDrive = "D"
      
    ChDrive sDrive
   
    'Display output message
    MsgBox "Current Drive is : " & sDrive, vbInformation, "VBA ChDrive Function"
    
End Sub

Output: Here is the screen shot of the second example output.
VBA ChDrive 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