Skip to main content

Excel Macro: Create, Copy, and Delete Folders

We can create, copy and delete specified folders using Folder object or FileSystemObject object in VBA.

Create, Copy, and Delete Folders

'---------------- Modules ----------------
Sub OperateFolder()
    Dim objFso As Object
    Dim objFolder As Object
    Dim strPath As String
    strPath = ThisWorkbook.path & Application.PathSeparator
    On Error GoTo errHandle
    Set objFso = CreateObject("Scripting.FileSystemObject")
    If objFso.FolderExists(strPath & "Test") = True Then
        objFso.CreateFolder strPath & "Test\TestA"
        objFso.CreateFolder strPath & "Test\TestA\TestB"
        Set objFolder = objFso.GetFolder(strPath & "Test\Test1")
        objFolder.SubFolders.Add "Test2"
        objFolder.Copy strPath & "Test\TestA\TestB"
        objFso.CopyFolder strPath & "Test\Test1\Test2", strPath & "Test\Test2"
        objFso.MoveFolder strPath & "Test\TestA\TestB", strPath & "Test\Test3"
        objFolder.Delete
        objFso.DeleteFolder strPath & "Test\T*"
        objFso.CreateFolder strPath & "Test\Test1"
    End If
    Set objFolder = Nothing
    Set objFso = Nothing
    Exit Sub
errHandle:
    MsgBox Err.Description
End Sub

How This Macro Works

Line 10 code: uses the CreateFolder method of the FSO object to create a subfolder "TestA" in the "Test" folder.

Line 11 code: uses the CreateFolder method of the FSO object to create a subfolder "TestB" in the "TestA" folder.

Line 13 code: uses the Add method of the Folder object to create a subfolder "Test2" in the "Test\Test1" folder.

Line 14 code: uses the Copy method of the Folder object to copy Test1 folder (and subfolder Test2 of Test1) to TestA folder and rename Test1 with TestB.

Line 15 code: uses the CopyFolder method of the FSO object to copy Test2 folder from Test1 folder to Test folder.

Line 16 code: uses the MoveFolder method of the FSO object to move TestB folder from TestA folder to Test folder and rename it with Test3.

Line 17 code: uses the delete method of the Folder object to delete Test1 folder in Test folder.

Line 18 code: uses the DeleteFolder method of the FSO object to delete all folders in the "Test" folder whose names begin with "T" or "t".

Line 19 code: uses the CreateFolder method of the FSO object to create a subfolder "Test1" in the "Test" folder.

Macro Download

Create Copy and Delete Folders

Most VBA code should be placed in Standard Modules unless specified.

If you see a comment '------------------ Modules------------------ in the code header that means put the code in a Standard Module. For more information, learn this course: Where should I put the Excel VBA code?

The following steps teach you how to put VBA code into a Standard Module:

  1. Activate the Visual Basic Editor by pressing ALT + F11.
  2. Right-click the project/workbook name in the Project Window.
  3. Choose Insert -> Module.
  4. Type or paste the code in the newly created module. You will probably need to change the sheet name, the range address, and the save location.
  5. Click Run button on the Visual Basic Editor toolbar.
  6. For more information, learn this course: Programming with Excel VBA

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>