VBA MkDir Function

VBA MkDir Function in Excel

VBA MkDir function in Excel is categorized as File and Directory function. This built-in VBA MkDir function creates a new folder or directory in Excel VBA. If the folder or the directory already exists, returns an error.

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

Syntax of VBA MkDir Function

The syntax of the MkDir Function in VBA is

MkDir(Path)

The MkDir Function doesn’t return any value. It creates a new folder or directory.

Parameters or Arguments:

The MkDir function/statement has one argument in Excel VBA.
where
Path: It is a mandatory string parameter. The path argument represents the folder or directory to create.

Where we can apply or use VBA MkDir Function?

We can use this MkDir 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: Create a New Folder

Here is a simple example of the VBA MkDir function. This below example create a new foler and displays message.

'Create a New Folder
Sub VBA_MkDir_Function_Ex1()

    'Variable declaration
    Dim sPath As String
    
    sPath = "C:\Someswari\VBAF1\VBA Functions\VBA Text Functions\Test\"
    
    MkDir sPath
        
    MsgBox "Folder has created : " & vbCrLf & sPath, vbInformation, "VBA MkDir Function"
    
End Sub

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

Example 2: Check and Create a New Directory

Here is a simple example of the VBA MkDir function. This below example checks for the direcetory exists or not. If it doesn’t exists, creates a new directory.

'Check and Create a New Directory
Sub VBA_MkDir_Function_Ex2()

    'Variable declaration
    Dim sPath As String
        
    sPath = "C:\Test"
    
    If Len(Dir(sPath, vbDirectory)) = 0 Then
       MkDir sPath
       MsgBox "Directory Created Successfully : " & vbCrLf & sPath, vbInformation, "VBA MkDir Function"
    End If
        
End Sub

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

Example 3: Create Directory in the Current Drive

Here is a simple example of the VBA MkDir function. This below example checks for the drive exists or not. If it doesn’t exists, creates a folder in the current drive.

'Create Directory in the Current Drive
Sub VBA_MkDir_Function_Ex3()

    'Variable declaration
    Dim sPath As String
        
    sPath = "Test_Drive"
    
    If Len(Dir(sPath, vbDirectory)) = 0 Then
       MkDir sPath
       MsgBox "Created Directory in the current drive : " & vbCrLf & sPath, vbInformation, "VBA MkDir Function"
    End If
        
End Sub

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

Example 4: Create a New Folder(Returns an Error)

Here is a simple example of the VBA MkDir function. This below example returns an error. Because the folder is already exists.

'Create a New Folder
Sub VBA_MkDir_Function_Ex4()

    'Variable declaration
    Dim sPath As String
    
    sPath = "C:\Someswari\VBAF1\VBA Functions\VBA Text Functions\Test\"
    
    'Note: Folder is already available.
    MkDir sPath
        
    MsgBox "Folder doesn't create : " & vbCrLf & sPath, vbInformation, "VBA MkDir Function"
    
End Sub

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