Created Macro to save file dynamically failing

MrPink1986

Active Member
Joined
May 1, 2012
Messages
252
Hi there,

I have created a macro which saves a sheet from a workbook in a location that that changes daily. This is captured in the macro by using formulas in excel. The macro seems to create the file as expected however I get to the following line and it fails to save
Code:
 .SaveAs Filename:=CheminDest & fNAME & ".xlsx"
- I get a runtime error 1004.

The CheminDest string created seems to be fine and I cant seem to figure it out.


My code is below

Code:
Sub Save_Upload()

Dim WS As Worksheet, CheminDest As String, fNAME As String


    On Error Resume Next
    CheminDest = "C:\Data\4XXX\" & Sheets("Manual Price Assets").Range("R11") & "\" & Sheets("Manual Price Assets").Range("R13") & "\" & Sheets("Manual Price Assets").Range("R14") & "\"
    MkDir CheminDest
    On Error GoTo 0
    fNAME = Sheets("Manual Price Assets").Range("R20")
    
    Sheets(Array("Upload File")).Copy
    Sheets("Upload File").Name = "Upload File"


    With ActiveWorkbook
        For Each WS In .Worksheets
        Next WS


        .SaveAs Filename:=CheminDest & fNAME & ".xlsx"
        .Close Fasle
    End With
    
     MsgBox "Text file has been saved ", vbInformation, "Data backup"
    
    End Sub

Any help greatly appreciated.
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Upvote 0
thanks got it - I created the folders separately and then combined them in my last statement - code is below.....

Code:
Sub Save_Upload()

Dim WS As Worksheet, CheminDest As String, fNAME As String


    On Error Resume Next
    CheminDest = "C:\Data\4XXX\" & Sheets("Manual Price Assets").Range("R11") & "\"
    MkDir CheminDest
    CheminDest2 = CheminDest & Sheets("Manual Price Assets").Range("R13") & "\"
    MkDir CheminDest2
    CheminDest3 = CheminDest2 & Sheets("Manual Price Assets").Range("R14") & "\"
    MkDir CheminDest3
    On Error GoTo 0
    fNAME = Sheets("Manual Price Assets").Range("R20")
    
    Sheets(Array("Upload File")).Copy
    Sheets("Upload File").Name = "Upload File"


    With ActiveWorkbook
        For Each WS In .Worksheets
        Next WS


        .SaveAs Filename:=CheminDest3 & fNAME & ".xlsx"
        .Close Fasle
    End With
    
     MsgBox "Text file has been saved ", vbInformation, "Data backup"
    
    End Sub
 
Upvote 0
Hi Glad you got it sorted.

As windows has a path length limit of 255 characters then MkDir would fail for a folder over that limit.
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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