VBA Create Folder
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.
VBA Create a Folder Example
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.
Instructions to use VBA Macro Code
Here are the instructions to use above macro in Visual basic editor.
- Open Visual Basic Editor(VBE) by clicking Alt +F11
- Go to code window by clicking F7
- Copy above specified macro or procedure
- Paste above copied code in code window
- Run macro by clicking F5 or Run command
- you can find above specified input and output screenshot for your reference and better understand.
Related Articles
You can also learn complete details like syntax, example and etc by clicking on the following buttons.