Save As VBA Code

Gavin Harrison

New Member
Joined
May 2, 2017
Messages
34
Hi.

I have the below code that works fine to save my workbook as a PDF where the user specifies the location but the filename is specific. I now require if to still be an excel file.

I would also like to add in to the code the ability to delete several of the worksheets within the new workbook to reduce the file size as this file will be saved over and over again.

This is my original code:

Sub Save_Excel_as_PDF()

strfile = ThisWorkbook.Path & "" & strfile
myfile = Application.GetSaveAsFilename _


Application.ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:=Sheets("Great Rooms Report").Range("AI3").Text _

MsgBox "Your report has been saved to the selected folder named as: " & Range("AI3").Value & "."

End Sub


Many thanks
Gavin
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
ok, so this is what I have so far, which completes the save as bit, but doesn't allow the user to select a location.

Sub Export()
ActiveWorkbook.SaveAs Filename:=Sheets("Great Rooms Report").Range("AI3").Text, FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:=Password, WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
MsgBox "Your report has been saved to the selected folder named as: " & Range("AI3").Value & "."
End Sub

I did try to add at the beginning....

Application.Dialogs(xlDialogSaveAs).Show

but this then saves 2 copies of the document.

I have also tried to add at the end to delete sheets....

Application.DisplayAlerts = False
Sheets("Sheet1").Delete
Sheets("Sheet2").Delete
Sheets("Sheet3").Delete
Application.DisplayAlerts = True

But it doesn't like that either.

Any help greatly appreciated.

Thanks
Gavin
 
Upvote 0
How about
Code:
Sub Save_Excel_as()

    Dim MyFile As String

    MyFile = Application.GetSaveAsFilename(, "Excel Files (*.xlsm), *.xlsm")

    Application.DisplayAlerts = False
    Sheets("Sheet1").Delete
    Sheets("Sheet2").Delete
    Sheets("Sheet3").Delete
    Application.DisplayAlerts = True

    ActiveWorkbook.SaveAs Filename:=MyFile, FileFormat:=52, Password:="Password", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False


End Sub
 
Upvote 0
Thanks for the reply, I have just tried a different approach, which involves copying the sheets I wanted to save to a new workbook. The below code also allows the user to select the location of the file to be saved at.

Sub Export()
strfile = ThisWorkbook.Path & "" & strfile
myfile = Application.GetSaveAsFilename _
Worksheets(Array("Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thur", "Great Rooms Report", "Time Summary", "Time Statistics")).Copy
Set wbNew = ActiveWorkbook
With wbNew
ActiveWorkbook.SaveAs Filename:=Sheets("Great Rooms Report").Range("AI3").Text, FileFormat:=xlOpenXMLWorkbookMacroEnabled
.Close
End With
End Sub
 
Upvote 0
Glad you got it sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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