Not saving correctly

  • Sometimes my macro saves correctly, and other times, they save to a different file. Any idea why these are sending documents to the incorrect folder sometimes and the correct one other times? I'm just trying to save the workbook in the same folder that it is already in and add the date at the end - one in PDF format and the other in excel format.


    Thank y ou!


  • You're not specifying the path when you save, so they will end up in whatever the current directory is - there is no guarantee that that will be the same as the folder the workbook is currently in. You should always include the path when doing a SaveAs or export.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Thanks! So what would that look like? I thought the newFullpath was specifying where to save it.

  • You're not actually using newFullPath for the filename argument though...

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Well, don't ask another question. Continue with the original.


    Quote


    Sorry. It didn't work when I tried it.

    That comment does nothing to help solve your problem.


    What error message did you get?

    What didn't work?


    Respond to this in the original question and attach your workbook with the code in that doesn't work.

  • I don't get an error message. It just still saves to random folders. Just used the code below and it saved to the Documents folder on my computer, not the folder the file is in.


  • You still haven't put the path into the SaveAs line.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Thanks, Rory! Where do I put it in the SaveAs line? I put it after SaveAs, but it errored out.

  • Try changing

    Code
    newFullPath = WBPath & "\" & WBName
    ThisWorkbook.SaveAs fileName:=CustomerName & "_" & PricingAnalysis & "_" & Format(Date, "MM.DD.YYYY") & ".xlsm"

    to

    Code
    newFullPath = WBPath & "\" & WBName & "\"
    ThisWorkbook.SaveAs fileName:=newFullPath & CustomerName & "_" & PricingAnalysis & "_" & Format(Date, "MM.DD.YYYY") & ".xlsm"

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Does the folder specified in "newFullPath" always exist, and is it in the same folder as "ThisWorkbook"?

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!