Table of Contents
Automating file system operations is a fundamental requirement in advanced spreadsheet and application management. VBA (Visual Basic for Applications) provides powerful tools to manage folders and files directly from within Microsoft Office applications. This guide details the two primary, reliable methods for creating directories using VBA, ensuring your automation tasks are both efficient and robust.
The simplest approach involves using the built-in MkDir statement. This command is fast and requires only one parameter: the absolute path defining the location and name of the folder you wish to create. However, for more complex scenarios involving checking existence, deleting, or manipulating multiple file properties, developers often rely on the Scripting Runtime Library and its cornerstone, the FileSystemObject (FSO). Understanding both methods allows you to choose the perfect tool for any given automation challenge.
Method 1: Utilizing the Intrinsic MkDir Statement
The MkDir statement is the most straightforward command for generating a new folder in VBA. It is an intrinsic function, meaning it does not require setting references to external libraries, making it ideal for quick, simple directory creation tasks. The command executes instantly, attempting to create the directory structure specified by the provided string parameter.
To successfully execute the MkDir command, you must supply a complete and valid file path. This path must include the drive letter, all necessary parent directories, and the name of the new folder. If any of the parent directories in the specified path do not exist, the MkDir statement will fail and generate a runtime error, typically indicating the path was not found. Therefore, this method is best suited when you are certain the parent structure already exists.
Here is the fundamental syntax for utilizing the MkDir statement in practice. Notice how the command is executed directly, requiring minimal setup within the macro environment:
Sub CreateFolder() MkDir "C:UsersBobDesktopMy_Data" End Sub
The example above demonstrates a macro that will create a folder named My_Data directly on the user ‘Bob’s’ Desktop. This path must be an absolute path, meaning it specifies the location starting from the root of the file system (the C: drive).
Detailed Example: Creating a Folder Using MkDir
We can walk through a practical scenario to illustrate how the MkDir function works and what the resulting file system looks like. Suppose we begin with a simple Desktop environment that already contains a couple of existing folders, as shown in the image below.
The initial state of the target directory shows the existing structure:

Our objective is to use VBA to seamlessly introduce a new directory named My_Data into this location without manual intervention. This is achieved by defining the exact path within our macro execution.
The following macro utilizes the MkDir statement, pointing it precisely to the desired location on the Desktop:
Sub CreateFolder() MkDir "C:UsersBobDesktopMy_Data" End Sub
After successfully executing this macro, the VBA code interacts directly with the operating system’s file manager. Navigating to the Desktop verifies that the specified folder has been created, confirming the successful execution of the MkDir command.
The resulting structure confirms the creation of the new folder:

As clearly illustrated, the new folder called My_Data is now present in the exact location that was specified in the code string.
Crucial Considerations: Error Handling with MkDir
While the MkDir statement is powerful, it is also highly sensitive to pre-existing conditions and path availability. One of the most common issues encountered is attempting to create a directory that already exists. Unlike some scripting environments which might silently ignore this operation, VBA will halt execution and raise a runtime error.
If we were to run the preceding macro a second time without first deleting the My_Data folder, the MkDir function would trigger a fatal runtime error. This behavior highlights the need for effective error handling, especially in production environments where macro failure is unacceptable.
The specific error received when attempting to duplicate a folder is the Path/File access error (Error 75). This error message is slightly misleading in this context, as it generally relates to permission issues or locked files, but in the case of MkDir on an existing folder, it signifies that the attempted action cannot be completed because the resource already occupies the specified path.
The error message displayed upon attempting to recreate an existing folder looks like this:

The error box confirms that there is a Path/File access error. To prevent this macro failure, developers must implement conditional checks using functions like Dir or utilize the more robust FileSystemObject which offers native existence checks.
Method 2: Leveraging the FileSystemObject (FSO)
For complex file system management where checking for folder existence, handling errors gracefully, or creating multiple nested folders is required, the FileSystemObject (FSO) provides a superior framework. FSO is part of the Microsoft Scripting Runtime Library and must be explicitly referenced in the VBA editor (Tools > References > Microsoft Scripting Runtime).
The FSO model uses object-oriented programming principles. Instead of a simple command like MkDir, we instantiate a FileSystemObject, and then invoke its methods, such as CreateFolder. This approach provides greater control and allows access to other useful properties like FolderExists or methods for copying and moving files.
The primary advantage of using the FileSystemObject‘s CreateFolder method is its integration with error-checking mechanisms. By utilizing the FolderExists method beforehand, you can write code that checks if the path is available, thus preventing the runtime error encountered with the standalone MkDir statement.
Example: Creating a Folder Safely with FSO
To utilize FSO, we first declare and set a variable as a new FileSystemObject. The CreateFolder method is then called on this object, passing the path string as the argument. This structure enhances code readability and maintainability.
Here is how you would implement robust folder creation using FSO, including conditional logic to prevent the Path/File access error:
Sub CreateFolder_FSO_Safe() ' Requires reference to Microsoft Scripting Runtime Dim fso As Object Dim FolderPath As String FolderPath = "C:UsersBobDesktopMy_Data_FSO" Set fso = CreateObject("Scripting.FileSystemObject") If Not fso.FolderExists(FolderPath) Then fso.CreateFolder FolderPath Debug.Print "Folder created successfully." Else Debug.Print "Folder already exists. No action taken." End If Set fso = Nothing End Sub
This code snippet ensures that the CreateFolder method is only executed if the target directory does not already exist, completely sidestepping the dreaded runtime error. Although MkDir is simpler for basic tasks, the FSO method scales much better for enterprise-level VBA development.
Path Management: Absolute vs. Relative References
When defining the path for folder creation, it is crucial to understand the difference between absolute and relative paths. An absolute path provides the full route from the root directory (e.g., C:), guaranteeing that the folder is created in a fixed, known location, regardless of where the VBA macro is run from. The examples provided above utilize absolute paths (e.g., C:UsersBobDesktopMy_Data).
A relative path, conversely, specifies the location relative to the current working directory of the application running the VBA code (usually the workbook location). If you were to use only "New_Folder" as the argument for MkDir, VBA would typically attempt to create that folder within the directory of the Excel file or Access database hosting the macro. While relative paths offer flexibility, they introduce risks if the application’s current directory is volatile or unknown.
For robust automation, it is highly recommended to construct absolute paths dynamically. This involves using built-in VBA functions to retrieve known system locations (like Environ("USERPROFILE") for the user folder or ThisWorkbook.Path for the location of the current file) and then concatenating these with the desired folder name. This prevents unexpected folder creation in obscure system folders.
Best Practices for Robust Folder Creation
To ensure your folder creation macros are reliable and scalable, adhere to several best practices concerning security, error handling, and resource management.
-
Implement Error Trapping: Since both MkDir and FSO operations can fail due to permissions, network issues, or duplicate names, always use
On Error GoTostatements or conditional checks (likeFolderExistsin FSO) to manage potential runtime errors, especially the Path/File access error. -
Clear Object References: If utilizing the FileSystemObject, always ensure you release system memory by setting the object variable to
Nothingat the end of the subroutine (e.g.,Set fso = Nothing). This is essential for preventing memory leaks and ensuring efficient resource usage. -
Input Validation: Before attempting folder creation, validate the input path string. Check for invalid characters (like
?,*,|,<,>) that are forbidden by the operating system for folder names. -
Use Variables for Paths: Hardcoding paths, as seen in the simple examples (
"C:UsersBobDesktopMy_Data"), is bad practice for portability. Use variables (e.g.,Dim strPath As String) to store and construct paths dynamically. This makes code easier to debug and adapt across different user environments.
Cite this article
stats writer (2025). How to Easily Create Folders Using VBA. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-create-a-folder-with-vba/
stats writer. "How to Easily Create Folders Using VBA." PSYCHOLOGICAL SCALES, 19 Nov. 2025, https://scales.arabpsychology.com/stats/how-do-i-create-a-folder-with-vba/.
stats writer. "How to Easily Create Folders Using VBA." PSYCHOLOGICAL SCALES, 2025. https://scales.arabpsychology.com/stats/how-do-i-create-a-folder-with-vba/.
stats writer (2025) 'How to Easily Create Folders Using VBA', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-create-a-folder-with-vba/.
[1] stats writer, "How to Easily Create Folders Using VBA," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, November, 2025.
stats writer. How to Easily Create Folders Using VBA. PSYCHOLOGICAL SCALES. 2025;vol(issue):pages.
