Creating, deleting and renaming folders is a common requirement when automating processes with VBA. The code snippets below should be sufficient to complete the most common folder tasks.
All the code examples below use the built-in Dir() function and its derivatives. The File System Object methods are not covered in this post but will be covered at a future point.
Check if a folder exists
Referencing a folder which does not exist will result in an error, therefore it is often necessary to check if a folder exists before carrying out any other actions.
'Check if a folder exists Dim folderPath As String folderPath = "C:\Users\marks\Documents\Folder" If Dir(folderPath, vbDirectory) <> "" Then 'Insert action for if the folder exists 'This example prints to the immediate window Debug.Print folderPath & " exists." Else 'Insert actions if the folder does not exist 'This example prints to the immediate window Debug.Print folderPath & " does not exist." End If
vbDirectory is the attribute of a folder. The code above can be adapted to check for other types of files too.
VBA Name of attribute | Enumerator | Description |
vbNormal | 0 | Files with no attributes (default setting) |
vbReadOnly | 1 | Read-only files |
vbHidden | 2 | Hidden files |
vbSystem | 4 | System files |
vbVolume | 8 | Volume label |
vbDirectory | 16 | Directories |
For checking folder existence within another procedure, it is often easier to have a reusable function, which can be called upon when required.
'Reusable function to check if a folder exists Function doesFolderExist(folderPath) As Boolean DoesFolderExist = Dir(folderPath, vbDirectory) <> "" End Function
The following VBA code calls the doesFolderExist function from above and prints True (the folder exists) or False (the folder does not exist) to the Immediate window.
'Call the reusable function to check for folder existence
Debug.Print doesFolderExist("C:\Users\marks\Documents\Folder")
The following VBA code calls the doesFolderExist function from above for use within an If statement.
'Check if a folder exists calling the doesFolderExist function Dim folderPath As String folderPath = "C:\Users\marks\Documents\Folder" If doesFolderExist(folderPath) = True Then 'Insert action for if the folder exists 'This example prints to the immediate window Debug.Print folderPath & " exists." Else 'Insert actions if the folder does not exist 'This example prints to the immediate window Debug.Print folderPath & " does not exist." End If
Create a new folder
The VBA code below will create a new folder. If the folder already exists, it will not overwrite it, but it will display an error. The function will only create the last folder in the file path, all the parent folders must already exist.
'Create a new folder
MkDir "C:\Users\marks\Documents\New folder"
The avoid an error, the code below will check if a folder exists before trying to create it.
'Create a folder if it does not already exist, if it does, do nothing Dim folderPath As String folderPath = "C:\Users\marks\Documents\New Folder" 'Check if the folder exists If Dir(folderPath, vbDirectory) = "" Then 'Folder does not exist, so create it MkDir folderPath End If
As the Dir() function will only create a single folder, the code below loops through the individual folder names in the path and calls the Dir() function to create any missing folders and subfolders.
'Create all the folders in a folder path Dim folderPath As String Dim individualFolders() As String Dim tempFolderPath As String Dim arrayElement As Variant 'The desired folder path folderPath = "C:\Users\marks\Documents\New Folder\New Folder\New Folder\New Folder" 'Split the folder path into individual folder names individualFolders = Split(folderPath, "\") 'Loop though each individual folder name For Each arrayElement In individualFolders 'Build string of folder path tempFolderPath = tempFolderPath & arrayElement & "\" 'If folder does not exist, then create it If Dir(tempFolderPath, vbDirectory) = "" Then MkDir tempFolderPath End If Next arrayElement
Delete a folder
The RmDir function will delete a folder. However, it is limited as it will only delete an empty folder. All the files within the folder will need to be deleted first. Using the File System Object method (which is not covered in this post) it is possible to delete folders and their contents.
'Delete a folder Dim folderPath As String folderPath = "C:\Users\marks\Documents\Delete Folder" 'Ensure the folder path as a "\" at the end of the string 'Required for deleting the files using wildcards If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\" 'Use wildcards to delete all the files in the folder Kill folderPath & "*.*" 'Delete the now empty folder RmDir folderPath
If the folder does not exist the RmDir function will display an error. Refer to the first section of this post to check for existence
Rename a folder
The VBA code below will re-name a folder, and even move the contents of the entire folder to another location.
'Rename a folder
Name "C:\Users\marks\Documents\Folder" As "C:\Users\marks\Documents\Renamed Folder"
To use this example code, it may be necessary to check if the old folder name exists and the new folder name does not exist.
Discover how you can automate your work with our Excel courses and tools.
Excel Academy
The complete program for saving time by automating Excel.
Excel Automation Secrets
Discover the 7-step framework for automating Excel.
Office Scripts: Automate Excel Everywhere
Start using Office Scripts and Power Automate to automate Excel in new ways.
Hi, I tried mixing the search with the delete routine. therefore i look for a file, if it doesnt exist it creates it and if it does it deletes the file and it creates it again. Through when it finds the files, it deletes all the documets inside but then the RmDim function returns the error 75. How do i solve it?
@ Filippo
To get rid off error 75 you may wish to try this:
1. Close all windows explorer windows manually of programmatically
2. Copy old folder to new folder (as above)
3. If command is launched from xlsm file then you may wish to relink cells to files in new folder or break links
4. Kill files in old folder (as above)
5. Remove old folder (as above)
I hope it helps
how can i close a folder. Thank you
To close a folder would require some Windows API calls – which are quite advanced. Why would you need to close a folder using VBA?