Create folder in desktop with vba, if exist, ignore

Solved/Closed
viriato74 Posts 4 Registration date Friday August 20, 2021 Status Member Last seen August 21, 2021 - Aug 20, 2021 at 10:29 PM
 praveen - Aug 12, 2023 at 04:03 PM
Hello,

How to create folder in desktop with vba whenever the excel book is opened, if exist, ignore.

Message if create new folder, silent if the folder exist.

System Configuration: Windows / Chrome 92.0.4515.159
Related:

2 responses

vcoolio Posts 1409 Registration date Thursday July 24, 2014 Status Moderator Last seen May 23, 2024 262
Updated on Aug 21, 2021 at 11:18 AM
Hello Viriato,

The following VBA code should work for you:-

Private Sub Workbook_Open()

        Dim cOb As Variant
        Dim FolderName As String, FolderExists As String
        FolderName = "C:\Users\AAAAA\Desktop\A New Folder" '---->Change folder name to suit. Change the AAAAA to your requirement.
        FolderExists = Dir(FolderName, vbDirectory)
    
Application.ScreenUpdating = False
       
       If FolderExists = vbNullString Then
            MsgBox "The desktop folder doesn't exist. Creating a new folder now.", vbExclamation, "INFORMATION"
            cOb = CreateObject("wscript.shell").specialfolders("Desktop") & "\" & "A New Folder" '--->Change folder name to suit.
            MkDir cOb
            Else: Exit Sub
       End If
    
Application.ScreenUpdating = True

End Sub


It will create a folder on your desktop named "A New Folder" with a message box warning. As it will exist thereafter, nothing else will happen each time your workbook is opened thereafter.

To implement this code:-

- Right click on any sheet tab.
- Select "View Code" from the menu that appears.
- In the Project Explorer which then appears, over to the left, double click on "ThisWorkbook".
- Over to the right in the big white code field, paste the above code.

You will need to change the folder path to suit your requirements.

I hope that this helps.

Cheerio,
vcoolio.
viriato74 Posts 4 Registration date Friday August 20, 2021 Status Member Last seen August 21, 2021
Aug 21, 2021 at 11:58 AM
Works without any error.
Thank you so much for your help.
0

is there any code to pick the shared path from excel FolderName = "C:\Users\AAAAA\Desktop\A New Folder" ' FolderName =Sheets("Sheet1").Range("a1")

0