+ Reply to Thread
Results 1 to 6 of 6

SaveAs does not work with variables for Path and Filename?

  1. #1
    Registered User
    Join Date
    09-10-2012
    Location
    Brisbane Australia
    MS-Off Ver
    Excel 2010
    Posts
    3

    Unhappy SaveAs does not work with variables for Path and Filename?

    Hi all.
    I am hoping somebody can help me, i have spent days trying to work out what i am doing wrong but still no success. All i am wanting to do is create a new .CSV file from a sheet in my workbook. This is the last part of my macro. I want the new file to be put in the same working folder as my workbook.
    When i use variables for the file name and path i get 'Runtime error 1004 method SaveAs of object_workbook failed.' If i hardcode in the path and filename it works fine. I need to use variables since the file name and path will vary.
    Hoping somebody can help Thanks
    Here is my code:

    Please Login or Register  to view this content.
    Last edited by steveJC; 10-03-2012 at 01:57 AM. Reason: did not use code tags

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,613

    Re: Does not work with variables for Path and Filename?

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Ben Van Johnson

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,613

    Re: SaveAs does not work with variables for Path and Filename?

    Please explain how & where CompanyName defined.

  4. #4
    Registered User
    Join Date
    09-10-2012
    Location
    Brisbane Australia
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: SaveAs does not work with variables for Path and Filename?

    Hi Ben. CompanyName is a public variable. When the macro is first run the user is presented with a form. The user selects one of 3 companys using a check box on this form. This choice will define which of 3 subs to run. It is here that variable 'CompanyName' will gets its string value depending on the selection made.. I hope this makes sence to you. Thank you.

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,613

    Re: SaveAs does not work with variables for Path and Filename?

    1. note that you cannot have slashes in a file name (this was the error causing the crashes)
    2. note the space between company name and date
    3. note the removal of the unnecessary parentheses
    4. You don't necessarily need to CHDir since the filename includes the drive and path
    5. If the active workbook has not been saved before the macro is run, then Pth = ActiveWorkbook.Path will be empty (another cause for error/crash)

    Please Login or Register  to view this content.
    Last edited by protonLeah; 10-03-2012 at 12:20 AM.

  6. #6
    Registered User
    Join Date
    09-10-2012
    Location
    Brisbane Australia
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: SaveAs does not work with variables for Path and Filename?

    Hi Ben.
    I have just read again your solution to the problem and noticed that a reply i sent you earlier is not shown. It was to thank you for your help and let you know that the macro now runs fine. I am not sure if you saw my reply so just in case you didn't thank you again for your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1