VBA - Saving

plost33

Well-known Member
Joined
Oct 2, 2008
Messages
866
I am wondering if anyone knows how i might be able to have VBA save my file in its existing location with its current name as well as save it a second time in another location with a diffrent name whenever the "save" icon, the little blue disk, is clicked on the upper left hand corner in Excel 2010?

I am basic with VBA and i am not sure how this code might look. Hope someone can assist.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
here is the code i have now:

Code:
Sub SaveTwice()
Dim ProjectNumber As String
Dim ProjectName As String
Dim SaveLocation As String
Dim XXX As String
ProjectNumber = Sheets("Budget").Range("A3").Value
ProjectName = Sheets("Budget").Range("A4").Value
SaveLocation = "C:\Users\tphythian\Documents\WWCI Cost Tracking Module\Archive"
XXX = Format(Date, "[$-409]ddmmmyyyy;@")
With ActiveWorkbook
        .Save
        .SaveAs SaveLocation & ProjectNumber & " - " & ProjectName & " - " & XXX & ".xlsx"
End With

End Sub
[\code]
 
 
I believe there is a problem with the .saveas line but i can't figure it out.  i am getting an error message that says:
 
 "this extension can not be used with the selected file type.  change the file extension in the file name text box or select a diffrent file type by changing the Save as type."
 
 
I am also trying to have the code above run whenever the "save" button is clicked on the top ribbin bar in Excel 2010.  To try and get it to execute when that button is clicked i am using the following code:
 
[code]

Private Sub workbook_save()
Application.******* "save", "savetwice"
End Sub
[\code]
 
 
 
anyone have suggestions on getting this up and running?
 
Upvote 0
Looks like you need to terminate the save folder path with a backslash. Try this (all the code goes in the ThisWorkbook module). I've used SaveCopyAs instead of SaveAs, so that the active workbook keeps its original name.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Save_Copy
End Sub

Sub Save_Copy()
    Dim ProjectNumber As String
    Dim ProjectName As String
    Dim SaveLocation As String
    Dim XXX As String
    ProjectNumber = Sheets("Budget").Range("A3").Value
    ProjectName = Sheets("Budget").Range("A4").Value
    SaveLocation = "C:\Users\tphythian\Documents\WWCI Cost Tracking Module\Archive\"
    XXX = Format(Date, "[$-409]ddmmmyyyy;@")
    With ActiveWorkbook
        .SaveCopyAs SaveLocation & ProjectNumber & " - " & ProjectName & " - " & XXX & ".xls"
    End With
End Sub
My file extension in the code above is .xls because I'm using Excel 2003. For Excel 2010, try a manual Save As with the macro recorder running and look at the code generated to see what file extension it uses.
 
Upvote 0
is that code saving it two times? I want to save it once in its current location and another time in the folder the code shows.
 
Upvote 0
Yes, the code is saving it twice. The Workbook_BeforeSave is called when the disk save icon is clicked and saves it automatically in its current location and file name, and it calls Save_Copy which saves it in the specified folder and file name.
 
Upvote 0
John w provides an elegant solution to the save twice, cudos. John w's solution fixed another error in your original code. You were trying to save VBA code in a ".xlsx" Excel file extension. As far as I can tell, the ".xlsx" extension does not allow macros (VBA code) to be saved with the worksheet. John w's code changed the extension to "xls", making it backward compatible with Excel 97-2003, also removing the error you reported.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,334
Members
452,907
Latest member
Roland Deschain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top