Consulting

Results 1 to 13 of 13

Thread: Macro to save spreedsheet as new file

  1. #1
    VBAX Regular
    Joined
    Sep 2016
    Location
    Peru
    Posts
    28
    Location

    Macro to save spreedsheet as new file

    Hello friends,


    This is the case: I have an excel book with two sheets: one called "begin" and the other called "Report". What I need is to place a button on the "begin" sheet and assign it a macro to save the "Report" sheet as a new file in the same folder and with the same format, with the name "report + date today"


    If someone can help me, I will thank you very much

    Marcela

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Sub test()
        
        ThisWorkbook.Worksheets("Report").Copy
        ActiveWorkbook.SaveAs ThisWorkbook.Path & "\report" & Format(Date, "ddmmyyyy") & ".xlsx"
            
    End Sub

  3. #3
    VBAX Regular
    Joined
    Sep 2016
    Location
    Peru
    Posts
    28
    Location
    Thanks very much Mana...

    It does not work.... Could you give me an example please? I´m sorry to ask that, but I´m really a beginner about macros...

    Marcela

  4. #4
    "It does not work" does not tell us anything at all.
    I'll take the liberty to add an example. I hope mana does not mind.
    Attached Files Attached Files

  5. #5
    VBAX Regular
    Joined
    Sep 2016
    Location
    Peru
    Posts
    28
    Location
    Thanks so much jolivanes... I will explain myself better in future queries ... How could I add to this macro a MsgBox "Saved successfully"?

  6. #6
    Just before
    End Sub
    add this
    MsgBox "Workbook saved in " & ThisWorkbook.Path & " as ""Report for " & Format(Date, "dd-mm-yyyy") & ".xlsx"""
    or just this
    MsgBox "Saved successfully!"
    so you'll end up with something like this
    Sub test()
        Application.ScreenUpdating = False
        ThisWorkbook.Worksheets("Report").Copy
            ActiveWorkbook.SaveAs ThisWorkbook.Path & "\Report for " & Format(Date, "dd-mm-yyyy") & ".xlsx"
        ActiveWorkbook.Close
        Application.ScreenUpdating = True
        MsgBox "Workbook saved in " & ThisWorkbook.Path & " as ""Report for " & Format(Date, "dd-mm-yyyy") & ".xlsx"""
    End Sub

  7. #7
    VBAX Regular
    Joined
    Sep 2016
    Location
    Peru
    Posts
    28
    Location
    Thanks so much Jolivanes and Mana

    bests regards

    Marcela

  8. #8
    VBAX Regular
    Joined
    Sep 2016
    Location
    Peru
    Posts
    28
    Location
    Dear friends, I come back to you on this same question to know how I can make this macro work when the "Report" sheet is hidden
    Thank you very much

  9. #9
    Before the line where you copy Worksheet("Report"), add this line
        ThisWorkbook.Worksheets("Report").Visible = True
    and after the line where you save the Workbook as an .xlsx file, add this line
        ThisWorkbook.Worksheets("Report").Visible = False
    You don't show us the actual code you're working with so it's a guess.

  10. #10
    VBAX Regular
    Joined
    Sep 2016
    Location
    Peru
    Posts
    28
    Location
    Hi Jolivanes,

    I´m working with this code:

    Sub test()
    Application.ScreenUpdating = False
    ThisWorkbook.Worksheets("Report").Copy
    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\Report for " & Format(Date, "dd-mm-yyyy") & ".xlsx"
    ActiveWorkbook.Close
    Application.ScreenUpdating = True
    MsgBox "Workbook saved in " & ThisWorkbook.Path & " as ""Report for " & Format(Date, "dd-mm-yyyy") & ".xlsx"""
    End Sub


    Tranks for helping me

  11. #11
    Please add code tags around your code. Select (highlight) your code and click on the pound (#) sign.
    Sub test()
    Application.ScreenUpdating = False
    ThisWorkbook.Worksheets("Report").Visible = True
    ThisWorkbook.Worksheets("Report").Copy
    ActiveWorkbook.SaveAs ThisWorkbook.Path & "\Report for " & Format(Date, "dd-mm-yyyy") & ".xlsx"
    ActiveWorkbook.Close
    ThisWorkbook.Worksheets("Report").Visible = False
    Application.ScreenUpdating = True
    MsgBox "Workbook saved in " & ThisWorkbook.Path & " as ""Report for " & Format(Date, "dd-mm-yyyy") & ".xlsx"""
    End Sub
    Obviously you don't need a password otherwise you would have mentioned it.

  12. #12
    VBAX Regular
    Joined
    Sep 2016
    Location
    Peru
    Posts
    28
    Location
    That´s great, thanks Jolivanes


    Marcela

  13. #13
    Good luck with it Marcela.
    And please remember the code tags next time.
    Have a pleasant weekend

Posting Permissions

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