VBA code to save only the visible rows to a PDF after filtering the sheet

peteharrison86

New Member
Joined
Apr 28, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello everyone

After I've filtered my spreadsheet I want to save only the visible rows of data to a PDF (only save the results of the filtering to a PDF, and exclude empty columns outside my "table of data") Ideally also bringing up the Save As dialogue so you can choose where to save the PDF.

Is anyone able to provide the VBA code to do this?

Much appreciated,
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
This should work if your data starts in A1 and there is at least 1 empty column separating your 'table of data' from columns you want to exclude from the PDF.

VBA Code:
Public Sub Save_Visible_As_PDF()

    Dim PDFfilename As Variant
   
    PDFfilename = Application.GetSaveAsFilename(InitialFileName:=ThisWorkbook.Path & "\", FileFilter:="PDF, *.pdf", Title:="Save As PDF")

    If PDFfilename <> False Then
        ActiveSheet.Range("A1").CurrentRegion.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfilename, Quality:=xlQualityStandard, _
                                                       IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False    
    End If
   
End Sub
 
Upvote 0
Solution
Hi John

Thank you so much that actually works perfectly! I assume it must be "Current.Region" doing the magic there and only exporting the results of your filters to PDF. I wasn't aware of the Current.Region function / property.

Thank you again,
Pete.
 
Upvote 0
Yes, CurrentRegion, from a base cell or range, is a range bounded by any combination of blank rows and blank columns.
 
Upvote 0

Forum statistics

Threads
1,214,533
Messages
6,120,076
Members
448,943
Latest member
sharmarick

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