VBA MkDir Function

VBA MkDir Function

Overview of VBA MkDir Function:


VBA MkDir function is categorized as File and Direcetory 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 can be used 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 crrent 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 use Macro Codes

Here are the step by step instructions to use the MkDir 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 *