Problem with Save-As Macro

klarowe

Active Member
Joined
Mar 28, 2011
Messages
389
I am setting up this macro to automatically bring up the Save-As box upon opening.
Here is the code I have:
Code:
Application.DisplayAlerts = False
        template_file = ActiveWorkbook.FullName
        fileSaveName = Application.GetSaveAsFilename( _
        InitialFileName:="S:\SERVICE\Shop Teardown Reports\Job-Number teardown" & ".xls", _
        fileFilter:="Microsoft Office Excel Workbook (*.xls), *.xls")
    If fileSaveName = False Then MsgBox "Save-As has been cancelled!"
    Exit Sub
        Application.DisplayAlerts = True

To test it I just have it going to a button and as of now, the box will come up and go to the correct folder, I can type the filename I want, but when I hit save it seems to just exit out and not do anything. The workbook name does not change and the file is not saved (from what I can tell).
So what am I missing here???

The cancel message box is working correctly.

Thanks yet again.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
A better solution (if possible) would be to have a macro that when opened, would come up with an InputBox "Enter Job Number". When a job number was entered, it would save-as that file in S:\SERVICE\Shop Teardown Reports as the filename: "(Number from inputbox) teardown.xls"

These last few odds and ends on this project are racking my brain.. lol.
 
Upvote 0
ActiveWorkBook.saveAs filename=S:\SERVICE\Shop Teardown Reports as the filename: " & inputbox("Enter Value") & " teardown.xls"
 
Upvote 0
ActiveWorkBook.saveAs filename=S:\SERVICE\Shop Teardown Reports as the filename: " & inputbox("Enter Value") & " teardown.xls"

When I enter that in, I get:
Compile error:
Expected: line number or label or statement or end of statement
with the \ in "S:\" highlighted.
 
Upvote 0
I was able to use what you posted though to change my original code... but it still just ends up closing out instead of saving when I press "save"

Code:
Sub SaveAs()
        Application.DisplayAlerts = False
        template_file = ActiveWorkbook.FullName
        fileSaveName = Application.GetSaveAsFilename( _
        InitialFileName:="S:\SERVICE\Shop Teardown Reports\" & InputBox("Enter Job Number") & " teardown.xls", _
        fileFilter:="Microsoft Office Excel Workbook (*.xls), *.xls")
    If fileSaveName = False Then MsgBox "Save-As has been cancelled!"
    Exit Sub
        Application.DisplayAlerts = True
End Sub

But I would still like it to automatically save without bringing up the save-as box, but if the cancel button is pressed, it will open as normal.
 
Last edited:
Upvote 0
Ok... last comment from me for a while.. lol. I got it almost all figured out. The only problem left is that if I hit "Cancel" it saves it anywas as just " teardown.xls". I want it to cancel the save all together.
Thanks again.

Current Code:
Code:
Private Sub Workbook_Open()
    ActiveWorkbook.SaveAs Filename:="S:\SERVICE\Shop Teardown Reports\" & InputBox("Save-As: Enter Job Number") & " teardown.xls"
End Sub
 
Upvote 0
Ok, I know I'm probably getting annoying now, but it seems like I fight to figure it out on my own and can't figure it out so I post it up here and then not a few minutes later I figure it out... and its too late to edit the last post.

So here is the latest update:
I have it all set up pretty well now. I changed it around so that it will only activate on open if the workbook is read only. And then its set so that if cancel is pressed it ends the sub. Each If component is working perfectly however when I put them together I lose one of them (depending on the order). So I know it is something to do with it having multiple if statements but I can't find a solution.
Here is the new code:
Code:
Sub SaveAs()
    If ActiveWorkbook.ReadOnly Then
            Response = InputBox("Enter Job Number", "Save-As")
         If Response = False Then Exit Sub
            ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\tbisvkl2\Desktop\Test\" & Response & " teardown.xls"
    Else: Exit Sub
    End If
End Sub

Please help...again. lol.
 
Upvote 0
Almost there.
Code:
Sub TestSave()
    If ActiveWorkbook.ReadOnly = True Then GoTo 1
    If ActiveWorkbook.ReadOnly = False Then GoTo 2
1:  Response = Application.InputBox("Enter Job Number", "Save-As")
    If Response = False Then Exit Sub
    On Error GoTo 0
    ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\tbisvkl2\Desktop\Test\" & Response & " teardown.xls"
2:  Exit Sub
End Sub

Now everything is working great with the exception of if the file happens to already exist, the default "this file already exists, replace existing file? Yes, No, Cancel". If you select no or cancel, the on error goto returns to the input box, but if the error is repeated it then brings up the Macro Error box
"run time error '1004':
Method 'SaveAs' of object '_workbook' failed.

How can I get the on error GoTo to just continue to repeat until the user enters a filename that will not cause an error?
 
Upvote 0
****... missed the edit time limit by about 30 seconds... but I have SUCCESS!!!
I finally got all the bugs worked out and everything working like I want. It may not be the most simplified, but it does its job.
Code:
Private Sub Workbook_Open()
    If ActiveWorkbook.ReadOnly = True Then GoTo 1
    If ActiveWorkbook.ReadOnly = False Then GoTo 2
1:  Response = Application.InputBox("Enter Job Number", "Save-As")
    If Response = False Then Exit Sub
    On Error GoTo Error
    ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\tbisvkl2\Desktop\Test\" & Response & " teardown.xls"
2:  Exit Sub
Error: Resume 1
End Sub

Thanks for all the help!!!
 
Upvote 0

Forum statistics

Threads
1,224,526
Messages
6,179,322
Members
452,906
Latest member
Belthazar

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