VBA Create Folder in Excel. We use MkDir VBA function to create a folder. We creates a folder in a specific directory or current directory. Once we create folder, we store file or files in it. If folder already exists then it throws an error message. In this tutorial we explained examples and step by step instructions.
Let us see an example how to create folder using VBA. In the below example we are trying to create a folder named ‘Files and Folders’. The created folder is empty.Now we can store files in it. You can change root folder path in the below example as per your requirement.
'VBA Creating Folder Sub VBAF1_Create_Folder() 'Variable declaration Dim sFolderPath As String 'Define root folder path to create folder sFolderPath = "C:\VBAF1\Files and Folders\" 'Create Folder MkDir sFolderPath 'Diplay messafe on the screen MsgBox "Folder has created : " & vbCrLf & vbCrLf & sFolderPath, vbInformation, "VBAF1" End Sub
Output: You can notice ‘Files and Folders’ new folder at ‘C:\VBAF1\’. You can create same folder in another root folder. But you can not create multiple times in the same folder. Please find the below screen shot for your reference.
Once we run above macro, it creates a folder. If we run again this macro, it throws ‘Path/File access error’ message on the screen. You can find below screenshot below for your reference.
You can refer the following link for the step by step instructions.
Click on the following links of the useful resources. These helps to learn and gain more knowledge.