Macro to check if folder/subfolder exist for any user if not create them and save file

1nk3d

Board Regular
Joined
May 31, 2016
Messages
51
Hello all.

I am looking for a 3 in one here. I have a macro that will save a file automatically when ran. However, I am looking to use a new macro for a workbook.

I do not have any code right now, as everything I found has not worked.

What I want to do is check if a folder/subfolder exist on a users desktop, however everyone who will have access, has their own user.

Ideally it would be saved on the desktop under Reports/Feb (I can change the macro each Month as needed.

If that path does not exist, I want the macro to create it

The last part would be saving it as todays date & report.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
See if this Test macro gets you started. It creates the Desktop subfolder "\Reports\Feb" if it doesn't exist and saves the workbook as "< today's date in the format yyyy-mm-dd ><today's date="" in="" the="" format="" yyyy-mm-dd=""> Report.xlsm" in the subfolder.

Code:
Public Sub Test()

    Dim desktopSubfolder As String
    
    desktopSubfolder = Create_Desktop_Subfolder("\Reports\Feb")
    ThisWorkbook.SaveCopyAs desktopSubfolder & "\" & Format(Date, "yyyy-mm-dd") & " Report" & Mid(ThisWorkbook.Name, InStrRev(ThisWorkbook.Name, "."))
    
End Sub


Private Function Create_Desktop_Subfolder(subfoldersPath As String) As String

    Dim desktopPath As String
    Dim folders As Variant
    Dim i As Long
    
    desktopPath = Get_SpecialFolderPath("Desktop")
    folders = Split(subfoldersPath, "\")
    For i = 0 To UBound(folders)
        If folders(i) <> "" Then
            desktopPath = desktopPath & "\" & folders(i)
            If Dir(desktopPath, vbDirectory) = vbNullString Then MkDir desktopPath
        End If
    Next

    Create_Desktop_Subfolder = desktopPath
    
End Function


Private Function Get_SpecialFolderPath(specialFolderPath As String) As String

    Dim WSshell As Object
     
    Set WSshell = CreateObject("WScript.Shell")
    Get_SpecialFolderPath = WSshell.SpecialFolders(specialFolderPath)
    Set WSshell = Nothing
    
End Function
</today's>
 
Last edited:
Upvote 0
A silly Microsoft 'gotcha'. If the argument to SpecialFolders is the String variable "Desktop", it returns the Public Desktop folder, otherwise if it's a Variant, it returns the User's Desktop folder. Therefore change this line as shown:
Code:
    Get_SpecialFolderPath = WSshell.SpecialFolders(CVar(specialFolderPath))
 
Upvote 0
Which line do I replace? I am getting a variety of errors and trying different lines...


A silly Microsoft 'gotcha'. If the argument to SpecialFolders is the String variable "Desktop", it returns the Public Desktop folder, otherwise if it's a Variant, it returns the User's Desktop folder. Therefore change this line as shown:
Code:
    Get_SpecialFolderPath = WSshell.SpecialFolders(CVar(specialFolderPath))
 
Upvote 0
Thank you so much! What if I want to add a message box to pop up indicating where the file was saved and the file name? Is that possible? Or just one that says report saved
 
Upvote 0
Try this version of the Test macro:

Code:
Public Sub Test()

    Dim desktopSubfolder As String
    Dim fullFileName As String
    
    desktopSubfolder = Create_Desktop_Subfolder("\Reports\" & Format(Date, "Mmm"))
    fullFileName = desktopSubfolder & "\" & Format(Date, "yyyy-mm-dd") & " Report" & Mid(ThisWorkbook.Name, InStrRev(ThisWorkbook.Name, "."))
    ThisWorkbook.SaveCopyAs fullFileName
    MsgBox "Saved " & fullFileName
    
End Sub
Another change is that the macro now saves the file in the subfolder for the current month, so there is no need to edit the code from month to month.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top