VBA "Save As", no error but file does not save

theDrew

Board Regular
Joined
May 6, 2003
Messages
104
Hi all,

I have written the following simple macro to import some data into a worksheet and then prompt the user to save the file in Excel 2003 format (the system to which we will upload this data does not accept formats later than 2003). The template is in "*.xlsm" format.

The code executes without error, but when the user hits the "Save" button in the "Save As" dialog box, nothing happens. The "Save As" box closes, but no file is saved.

Help?

Code:
Private Sub cmdImportData_Click()

Dim sFName As String

'On Error Resume Next

   PrepData
   CopyData
   FormatColumns

'prompt the user to save the file in "*.xls" format
sFName = Application.GetSaveAsFilename("upload", "Excel files (*.xls), *.xls")

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
GetSaveAsFileName gets you a file name but doesn't save anything. You have to write that code too (note that you should handle cases where the user hits cancel, which might happen if they get confused and don't enter a file name):

Code:
[COLOR="Navy"]Sub[/COLOR] foo()
[COLOR="Navy"]Dim[/COLOR] sFName [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]

    sFName = Application.GetSaveAsFilename("upload", "Excel files (*.xls), *.xls")
    
    [COLOR="Navy"]If[/COLOR] sFName <> "False" [COLOR="Navy"]Then[/COLOR]
        ActiveWorkbook.SaveAs sFName, 56
    [COLOR="Navy"]Else[/COLOR]
        [COLOR="SeaGreen"]'//Do something - user cancelled[/COLOR]
    [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]

[COLOR="Navy"]End Sub[/COLOR]

Possibly a simpler way to go would be to use the same file name with an .xls extension and file type? Or assign the file name automatically if it has a standard name that should be used?
 
Last edited:
Upvote 0
GetSaveAsFilename only returns a string - it doesn't actually execute the SaveAs command. So you have the sFName, then you just need to execute SaveAs:

Code:
If sFName<>"False" Then ActiveWorkbook.SaveAs sFName
 
Upvote 0

Forum statistics

Threads
1,221,618
Messages
6,160,873
Members
451,674
Latest member
TJPsmt

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