+ Reply to Thread
Results 1 to 21 of 21

Saveas Macro with current date

  1. #1
    Registered User
    Join Date
    03-04-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Red face Saveas Macro with current date

    How do I create a macro that will save a workbook to a specific location using a specific name along with the current date and be in a specific format (.xls)?

    This is what I have so far:
    Private Sub SaveFile_Click()
    ActiveDocument.SaveAs Filename:="C:\Documents and Settings\All Users\Shared Documents\VendPerf " _
    & Format(Date, "mm-dd-yy"), FileFormat:=56
    End Sub

    All it does though, is generate an error that says "Cannot run the macro "Vendor Performance.xls'!ThisWorkbook.SaveFile'. The macro may not be available in this workbook or all macros may be disabled."

    I have verified that the macros for the workbook are running. Do I have this worded wrong or is it in the wrong place? Thanks!

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Saveas Macro with current date

    Hi valkok,

    Which code module is the procedure sitting in? ie. A standard code module, a sheet class module, a userform class module, the ThisWorkbook class module?

    How are you calling the procedure? eg. From an forms button?
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Registered User
    Join Date
    03-04-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Saveas Macro with current date

    I have tried it in "ThisWorkbook" and in "Sheet2". Sheet2 is where I have my instructions and another macro that calls up the userform. Is there another location that I should try?

    Edit: oops, forgot to reply about how I'm calling it. I have a forms button on Sheet2.

  4. #4
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Saveas Macro with current date

    It depends on what SaveFile is.
    Please Login or Register  to view this content.
    If it is an ActiveX control sitting on a userform or a sheet, then the code needs to go in that userform or sheet's module.

    If it is a forms control sitting on a sheet, then the code needs to go in a standard code module.

  5. #5
    Registered User
    Join Date
    03-04-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Saveas Macro with current date

    I'm really new to this. I just started learning how to do programming last week. What is the difference between an ActiveX control and a forms control? How do I know which one I'm using? The button that I want to attach to the macro is on Sheet 2. Does that help?

  6. #6
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Saveas Macro with current date

    I'm pretty sure you have a Forms control on the sheet, so my suggestion would be for you to put the code in a standard code module (in the Modules folder).

  7. #7
    Registered User
    Join Date
    03-04-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Saveas Macro with current date

    I tried moving the code over to Module1. That didn't help. It still gives me the same error. Do I need to initialize that module? or do something else to it?

  8. #8
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Saveas Macro with current date

    Please can you attach the file to the thread (clear out any confidential information)?

  9. #9
    Registered User
    Join Date
    03-04-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Saveas Macro with current date

    I just tried to re-attach the macro to my button to see if that would help and I noticed that it's not in the macro list. Could I have some syntax wrong?
    Attached Files Attached Files

  10. #10
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Saveas Macro with current date

    Okay, you need to do two things.

    Firstly, make the procedure public by changing the 'private' to 'public' like this:
    Please Login or Register  to view this content.
    Then, right click on the Save File Button > Assign Macro > choose your SaveFile procedure from the list.


    Also, in my post above I've used code tags so that my code looks nice in my post. The forum rules here ask you to do the same (you should have in post #1). To see how to use the code tags click on the quote button to the bottom right of my post.

  11. #11
    Registered User
    Join Date
    03-04-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Saveas Macro with current date

    It's a start, but now I have a compile error. Variable not defined. It highlighted the ActiveDocument portion of the code.

    Should that ActiveDocument be ActiveWorkbook?
    Last edited by valkok; 03-04-2011 at 06:58 PM.

  12. #12
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Saveas Macro with current date

    Yes, that's a separate issue. In Excel, we use ActiveWorkbook rather than ActiveDocument (you would use that in, for example, Microsoft Word).

  13. #13
    Registered User
    Join Date
    03-04-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Saveas Macro with current date

    Okay, that got me a bit further. Now I'm running into an error where Excel can't access this location: C:\Documents and Settings\All Users\Shared Documents\

    I have to have each person save to a place on their own harddrive because we don't have a shared drive that everyone has access to. Each user has their own ID specific folder that My Documents resides in and I don't know how to create a path for that. Is there a way to save it to the desktop? Or create a folder on the desktop?

  14. #14
    Registered User
    Join Date
    03-04-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Saveas Macro with current date

    Nevermind...I can look up how to create a folder on the C: drive. I will do that. Thank you very much for your help!

  15. #15
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Saveas Macro with current date

    The code to save to the workbook to the desktop is a bit more complicated. You would replace your current, standard module code with this.
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    03-04-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Saveas Macro with current date

    Would it make it easier to create a folder on the C: drive and save it there? One other thing that I need to consider is that these same people will be saving multiple times. I only want to create the new folder once (if it isn't there already). After that, I just want them to save to the already existing folder.
    Last edited by valkok; 03-04-2011 at 07:43 PM.

  17. #17
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Saveas Macro with current date

    Sure, it would... but, now I've shown you the code to locate the desktop, are you happy with that?

  18. #18
    Registered User
    Join Date
    03-04-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Saveas Macro with current date

    I don't know where my other comment went...

    Would it be easier to create a folder on the C:\ drive and save to there? One other thing to consider is that multiple files will be saved. I only want to create the new folder once. After the folder is created, I just want to save to that location. Is there a way to check to see if the folder exists and only create it if it doesn't?

  19. #19
    Registered User
    Join Date
    03-04-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Saveas Macro with current date

    Sorry, my PC is slow. I missed your comment. I believe that it would be better to put it on the C:\ drive so as to not clutter up the desktop. You've gone to a lot of trouble writing that code though and I hate to ask more of you.

  20. #20
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Saveas Macro with current date

    Ah, no problem. Give this a try. Change VendorPerformanceReports to whatever folder name you want. It will only create the folder once.
    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    03-04-2011
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Saveas Macro with current date

    You completely ROCK! That worked. Thank you so much! If I knew where you were, I'd buy you a cookie. As it is, can only offer my sincere thanks. Thanks!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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