Create folder in desktop with vba, if exist, ignore

[Solved]
Report
Posts
4
Registration date
Friday August 20, 2021
Status
Member
Last seen
August 21, 2021
-
Posts
4
Registration date
Friday August 20, 2021
Status
Member
Last seen
August 21, 2021
-
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

2 replies

Posts
1318
Registration date
Thursday July 24, 2014
Status
Moderator
Last seen
September 20, 2021
238
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
Thank you

Glad we were able to help! Love us? Write us a review! Rate CCM

CCM 2821 users have said thank you to us this month

Posts
4
Registration date
Friday August 20, 2021
Status
Member
Last seen
August 21, 2021

Works without any error.
Thank you so much for your help.
Posts
4
Registration date
Friday August 20, 2021
Status
Member
Last seen
August 21, 2021

This is correct?
C:\Users\" & Environ("username") & "\Desktop\MyFolder\
Posts
1318
Registration date
Thursday July 24, 2014
Status
Moderator
Last seen
September 20, 2021
238
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.
Posts
4
Registration date
Friday August 20, 2021
Status
Member
Last seen
August 21, 2021
>
Posts
1318
Registration date
Thursday July 24, 2014
Status
Moderator
Last seen
September 20, 2021

Thanks.