VBA Create Folder on Desktop

VBA Create Folder on Desktop

VBA Create Folder on Desktop. In the below specified example we are using the Environment variable UserProfile to find the desktop location. You can change the folder path which you want to create on the desktop according to your requirement.

We are checking specified folder is already exists on the desktop. If it is not available then we are creating using MkDir VBA function. In this tutorial we have explained examples and step by step instructions for your reference.

VBA Create Folder on the Desktop

Let us see an example macro VBA Create Folder on the Desktop. In the following example we use FolderExists method. It is method of FileSystemObject(FSO) in Excel VBA.

'VBA Create Folder on the Desktop
Sub VBAF1_Create_Folder_on_Desktop()

    'Variable declaration
    Dim sFolderName As String
    Dim sDesktopPath As String, sFolderPath As String
    
    'Find Desktop path location
    sDesktopPath = Environ("USERPROFILE") & "\Desktop\"
    
    'Define folder name to create on the desktop
    sFolderName = "Test Folder"
    
    'Folder Path
    sFolderPath = sDesktopPath & sFolderName
        
    'Create FSO Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    
    'Check Specified Folder exists or not
    If oFSO.FolderExists(sFolderPath) Then
        'If folder is available on the desktop
        MsgBox "Specified folder already exists on the desktop!", vbInformation, "VBAF1"
        Exit Sub
    Else
        'Create Folder
        MkDir sFolderPath
     
        'Diplay messafe on the screen
        MsgBox "Folder has created : " & vbCrLf & vbCrLf & sFolderPath, vbInformation, "VBAF1"
    End If
     
End Sub

Output: You can find following output screenshot for your reference.

VBA Create Folder on the Desktop
VBA Create Folder on the Desktop

If we are not checking folder is exists or not, then it throws following error when folder is already exists. The error is File/Path access error. It is a run time error in VBA.

File or Path access error
Note: You can find list of the environment variables in VBA by clicking on the following link.

Environment Variables in VBA

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays VBA Text Files VBA Tables

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog

Leave a Comment