Save as using names in cells

  • Hi Folks


    I'm trying to get the following code to work but keep running into a Run-time eorror '1004' error. Can anyone please give me some guidance as to where I'm going wrong?


    Code
    Sub SaveAs2()
    Dim part1 As String
    Dim part2 As String
    Dim part3 As String
    part1 = Range("C5").Value
    part2 = Range("C7").Value
    part3 = Range("C11").Value
    ActiveWorkbook.SaveAs Filename:= _
    "FilePath Here\" & part1 & " " & part2 & part3, FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=FalseEnd Sub


    I've also tried seperating the 3 parts with hyphens just in case:



    But no joy on either. I'd really appreciate some help please?


    Kind regards


    DezB

  • Re: Save as using names in cells


    Hi Folks


    OK, I'm making headway.... (I think!!)


    Cell C11 contains a date which is formatted as "dd-mmm-yyyy" This is where it all seems to be falling down. If I change C11 to text or just a number it works fine, but a date, (in any format), well, it just doesn't like it.


    Do I have to change the .Value in part3 = Range("C11").Value to .????? and if so can anyone advise what it should be changed to please?


    Kind regards


    DezB

  • Re: Save as using names in cells


    Ya, youre on the right track I think...


    Either one of Part1, Part2, or Part3 contains an invalid filename character, for example a forward slash "/" would be an invalid file name character and this is sometimes used in dates 10/June/2016 etc.


    However, you said your dates are 10-June-2016, and honestly that should be fine.


    If C11 is actually formatted as a date you could try this:


    Code
    part3 = format(Range("C11").Value ,"dd_mm_YYYY")


    if C11 is a string you could try this:

    Code
    part3 = replace(Range("C11").Value,"/","_")


    replaces "/" in the date with "_" (underscore)


    Also, Double check the value filename... by putting this just before the saveas....


    msgbox "filepath here" & part1 & "-" & part2 & "-" & part3 & "-"


    Is that the correct path?


    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Save as using names in cells


    Hi Ger Plante


    Phew, I thought I was slowly drifting into madness :(


    I'll have a go at your suggestion and let you know how I get on.


    Thanks for then advice.


    Kind regards.


    DezB

  • Re: Save as using names in cells


    Hi Ger Plante


    Absolutely spot on, worked a treat. As an extra on it, it it possible that once it saves the document it automatically closes the workbook?


    Kind regards


    DezB

  • Re: Save as using names in cells


    Good...

    Code
    activeworkbook.close false


    FALSE means to NOT save the file.

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: Save as using names in cells


    Hi Ger Plante


    Again, absolutely perfect, much appreciated and I've learn't a good deal from it.


    Kind regards.


    DezB

Participate now!

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