Macro to save and close, then copy file


N

NewSysAdmin

I've created a button with a macro assigned to it. I'm trying to save and
close an Excel file, then close the application. Then I would like to copy
it to another location (to do a type of sync operation). The user would
click the button each time they make changes. The code below is not copying
the file to the new location. Can anyone help me? Thank you so much.

Sub Rectangle1_Click()

Dim FromFileName As String
Dim ToFileName As String

Workbooks("Book1.xls").Save
Workbooks("Book1.xls").Close
Application.Quit

FromFileName = "C:\Ole\Book1.xls"
ToFileName = "C:\Blue\Book1.xls"
FileCopy Source:=FromFileName, Destination:=ToFileName


End Sub
 
Ad

Advertisements

Ad

Advertisements

L

Luke M

You're doing things out of order. Assuming your coding is in the workbook,
once you close the workbook, the coding is gone, and so it never reaches your
final steps.


Sub Rectangle1_Click()

Dim FromFileName As String
Dim ToFileName As String

Workbooks("Book1.xls").Save

FromFileName = "C:\Ole\Book1.xls"
ToFileName = "C:\Blue\Book1.xls"
FileCopy Source:=FromFileName, Destination:=ToFileName

'Since closing XL closes your workbook,
'no need for two lines of coding
Application.Quit

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top