+ Reply to Thread
Results 1 to 21 of 21

How to click "SAVE" to save a new file with file name derived from a cell?

  1. #1
    Registered User
    Join Date
    09-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    How to click "SAVE" to save a new file with file name derived from a cell?

    Hi,

    I came up with the program below to save a new file with the file name derived from cell A1.
    Sub SaveAs()

    Dim FName As String
    Dim FPath As String

    FPath = "C:\Documents and Settings\Test Folder"
    FName = Sheets("Sheet1").Range("A1").Text
    ThisWorkbook.SaveAs Filename:=FPath & "\" & FName

    End Sub
    It only works if I run the macro. What I really need is a program that will save the workbook as a new file with the file name derived from cell A1 when click "SAVE".

    Please help
    Last edited by vettenfun; 09-28-2011 at 03:46 PM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    First of all, please put your code within tags. It makes it easier to be copied for testing.

    You need to have a button called Save on your page and have the macro assigned to it. On 2nd thoughts, are you talking about running the macro when you are "saving" the macro file ?

  3. #3
    Registered User
    Join Date
    09-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    Can I use the "SAVE" button/icon from Excel? Yes, want to run the macro to save a new file once click "SAVE".

  4. #4
    Registered User
    Join Date
    09-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    I have found the below, but could not get it to work with my SaveAs macro (above).
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    SaveAs
    End Sub

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    You need the sub to be in the Workbook module.

    <Alt +F11> to open the VBE. Double click 'ThisWorkbook' in the Project explorer window (top left window). You will then see General at the top left of the Code window and Declarations at the top right of the Code window.

    Clicking on General drops down 'Workbook' and creates the default Open sub. Select Beforesave from the declarations dropdown and you'll see this:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    09-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    Thank you very much. I have followed your instruction, but I think I'm not doing it right. After saving saving the code from the VBE, a new Excel file was generated with file name derived from cell A1. However, Excel would crash. Please help.
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim FName As String
    Dim FPath As String

    FPath = "C:\Documents and Settings\Test Folder"
    FName = Sheets("Sheet1").Range("A1").Text
    ThisWorkbook.SaveAs Filename:=FPath & "\" & FName

    End Sub

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    In the code that you have given above, you have to add:

    Please Login or Register  to view this content.
    Then go back to your previous workbook

  8. #8
    Registered User
    Join Date
    09-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    I have added the Workbooks.Add command. When saving, it generates a new excel file with file name derived from cell A1 (great). But this new file does not have any contents from the original file. What did I do wrong? Can you help to add the Workbooks.Add command into my code? Thanks

    Quote Originally Posted by arlu1201 View Post
    In the code that you have given above, you have to add:

    Please Login or Register  to view this content.
    Then go back to your previous workbook

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    You have not included the code to copy the contents from the original file to the new workbook. Include that code before the saveas and you should be good to go.

  10. #10
    Registered User
    Join Date
    09-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    I'm almost there. With the code below, once click SAVE, a new file was generated with file name derived from cell A1. The only bug I'm having now is along with the new (correct) file another file named Book1 also came up. How to get rid of this Book1 file?

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim FName As String
    Dim FPath As String

    FPath = "C:\Documents and Settings\Test Folder"
    FName = Sheets("Sheet1").Range("A1").Text

    Workbooks.Add
    ThisWorkbook.SaveAs Filename:=FPath & "\" & FName

    End Sub

    Private Sub Workbook_Open()

    End Sub

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    Try one thing. Close all workbooks (save the macro file before closing). Then open the macro file and run again and see if the Book1 opens again. It could be opened in your previous runs of the macro.

  12. #12
    Registered User
    Join Date
    09-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    Please show me how... thanks

    Quote Originally Posted by arlu1201 View Post
    You have not included the code to copy the contents from the original file to the new workbook. Include that code before the saveas and you should be good to go.

  13. #13
    Registered User
    Join Date
    09-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    Book1 still came up :-(

    Quote Originally Posted by arlu1201 View Post
    Try one thing. Close all workbooks (save the macro file before closing). Then open the macro file and run again and see if the Book1 opens again. It could be opened in your previous runs of the macro.

  14. #14
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    Attach your workbook and let me know the fields you want copied and i can help you out.

  15. #15
    Registered User
    Join Date
    09-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    Here is the file. Please help. Thanks

    Quote Originally Posted by arlu1201 View Post
    Attach your workbook and let me know the fields you want copied and i can help you out.
    Last edited by vettenfun; 09-29-2011 at 12:07 PM.

  16. #16
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    I think your file got removed. What kind of file did you attach?

  17. #17
    Registered User
    Join Date
    09-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    I just reattached it. It's an xls file. Thanks

    Quote Originally Posted by arlu1201 View Post
    I think your file got removed. What kind of file did you attach?

  18. #18
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    Its removed again. I think there is some problem with the file. I get routed to the vbulletin message page to send a note to the administrator.

  19. #19
    Registered User
    Join Date
    09-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    File name is Test ABC.xls. It's still good. I could download it by double click on the file from my post.

    Quote Originally Posted by arlu1201 View Post
    Its removed again. I think there is some problem with the file. I get routed to the vbulletin message page to send a note to the administrator.

  20. #20
    Registered User
    Join Date
    09-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    Here is the file
    Attached Files Attached Files

  21. #21
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: How to click "SAVE" to save a new file with file name derived from a cell?

    Try this :

    Please Login or Register  to view this content.

+ 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