VBA Save to new workbook

mrmrf

New Member
Joined
Jun 3, 2019
Messages
34
Hi.
Looking for VBA code to save the print area to a new excel document and to be prompted for the file path.
I have PDF sorted but can’t seem to get to export to excel document.
Thanks.


Code:
Private Sub CommandButton8_Click()
Dim sPath As String
Dim sFile As Variant
Dim ws As Workbook

On Error GoTo ErrHandle
sPath = ThisWorkbook.Path & "\" & ThisWorkbook.Sheets("BH1").Range("G10") 

sFile = Application.GetSaveAsFilename _
    (InitialFileName:=sPath, _
Filefilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and File Name to Save")
If sFile = "False" Then
    MsgBox ("Please Choose a File Name")
Exit Sub
End If

ActiveWorkbook.ExportAsFixedFormat _
    Type:=x1typePDF, _
    Filename:=sFile, _
    Quality:=q1qualitystandard, _
    Includedocproperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
Exit Sub
ErrHandle:
MsgBox ("Document Not Saved")

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Here is some clear generic code for you to adapt
Currently saves print range in "Sheet XXX" to sheet "PrintRange" in workbook named "MyFileName.xlsx" in folder "C:\folder\subfolder"

Code:
Sub SavePrintRange()
    Dim ws As Worksheet, rng As Variant, wb As Workbook, fPath As String, fName As String
[COLOR=#006400]'define path and file name (path ends with [/COLOR]\ [COLOR=#006400])[/COLOR]
    fPath = "[COLOR=#ff0000]C:\folder\subfolder\[/COLOR]"       
    fName = "[COLOR=#ff0000]MyFileName[/COLOR]"
[COLOR=#006400]'range to be copied[/COLOR]
    Set ws = Sheets("[COLOR=#ff0000]Sheet XXX[/COLOR]")
    rng = ws.PageSetup.PrintArea
    Set rng = ws.Range(rng)
[COLOR=#006400]'add a workbook[/COLOR]
    Set wb = Workbooks.Add
[COLOR=#006400]'copy the range[/COLOR]
    rng.Copy
[COLOR=#006400]'paste to new workbook,overwite with values, adjust column widths to match, rename the sheet ...[/COLOR]
     With wb.Sheets(1).Range("A1")
        .PasteSpecial (xlPasteAll)
        .PasteSpecial (xlPasteValues)
        .PasteSpecial (xlPasteColumnWidths)
        .Parent.Name = "[COLOR=#ff0000]PrintRange[/COLOR]"
    End With
[COLOR=#006400]'save and close the workbook[/COLOR]
     wb.SaveAs Filename:=fPath & fName
     wb.Close False
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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