Save copy to newly created directory in the existing folder structure

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
I thought this wouldn't be too hard, but it's defeating me...
I want to create a copy of a file in a folder created by the VBA code. I've tried this with limited success:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


    Dim fName, cPath, nPath, fDate As String
    On Error Resume Next
    fName = ThisWorkbook.Name
    cPath = ThisWorkbook.Path
    fDate = Format(Date, "yymmdd")
    
    nPath = cPath & "\" & fDate & " " & fName
    
    MkDir nPath
    ChDir nPath


   Application.Dialogs(xlDialogSaveAs).Show fName & ".xlsm"
   
End Sub
The folder is created, but the default "save to" location is still the parent folder. Also, I want to be able to save it as an .xlsm format, but it's defaulting to .xlsx. Thanks in advance...
 
The directory is created by npath, but you don't reference it in you save as location, try adding it into
Application.Dialogs(xlDialogSaveAs).Show npath & "\" & fName & ".xlsm"
 
Upvote 0
Thanks for your response. I tried this, and once again the new folder is created but the default "save as" path is the parent folder. Also, the file name is not populated in the dialog box, it's just blank, and the default file format is ".xls" I'm running Excel 2013, by the way...
 
Upvote 0

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