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
viriato74 Posts 4 Registration date Friday August 20, 2021 Status Member Last seen August 21, 2021 - Aug 21, 2021 at 10:19 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 replies

vcoolio Posts 1362 Registration date Thursday July 24, 2014 Status Moderator Last seen September 29, 2022 250
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.
2
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
viriato74 Posts 4 Registration date Friday August 20, 2021 Status Member Last seen August 21, 2021
Aug 21, 2021 at 12:05 PM
This is correct?
C:\Users\" & Environ("username") & "\Desktop\MyFolder\
0
vcoolio Posts 1362 Registration date Thursday July 24, 2014 Status Moderator Last seen September 29, 2022 250
Aug 21, 2021 at 09:03 PM
You're welcome Viriato. I'm glad to have been able to assist.

As to your last query, yes, that is correct so it should look something like this:-
FolderName = "C:\Users\" & Environ("username") & "\Desktop\MyFolder"


Cheerio,
vcoolio.
0
viriato74 Posts 4 Registration date Friday August 20, 2021 Status Member Last seen August 21, 2021 > vcoolio Posts 1362 Registration date Thursday July 24, 2014 Status Moderator Last seen September 29, 2022
Aug 21, 2021 at 10:19 PM
Thanks.
0