0% found this document useful (0 votes)
14 views2 pages

Text

The document describes VBA code to export filtered data from an Excel worksheet to individual PDFs, then consolidate the PDFs into a single file. The code loops through filter criteria, applies each one, exports the filtered data as a PDF, then merges all the individual PDFs into one consolidated file.

Uploaded by

TANU AGRAWAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Text

The document describes VBA code to export filtered data from an Excel worksheet to individual PDFs, then consolidate the PDFs into a single file. The code loops through filter criteria, applies each one, exports the filtered data as a PDF, then merges all the individual PDFs into one consolidated file.

Uploaded by

TANU AGRAWAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Sub ExportPDFsAndConsolidate()

Dim ws As Worksheet
Dim filterCell As Range
Dim filterList As Range
Dim filterValue As Range
Dim folderPath As String
Dim consolidatedPDFPath As String
Dim pdfApp As Object
Dim pdfDoc As Object

' Specify the worksheet


Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name

' Specify the cell containing the filter criteria


Set filterCell = ws.Range("A1") ' Change "A1" to your cell address

' Specify the range of filter criteria


Set filterList = ws.Range("B1:B10") ' Change "B1:B10" to your range

' Specify the folder path to save individual PDFs and consolidated PDF
folderPath = GetFolderPath()
If folderPath = "" Then
MsgBox "Operation canceled. No folder path selected."
Exit Sub
End If

' Initialize Acrobat application object


On Error Resume Next
Set pdfApp = CreateObject("AcroExch.App")
If pdfApp Is Nothing Then
MsgBox "Unable to create Acrobat application object. Operation canceled."
Exit Sub
End If
On Error GoTo 0

' Loop through each filter criteria


For Each filterValue In filterList
If Not IsEmpty(filterValue.Value) Then
' Apply filter
filterCell.Value = filterValue.Value

' Export filtered data as PDF


ExportPDF ws, folderPath & "\" & "Filtered_Data_" & filterValue.Value &
".pdf"
End If
Next filterValue

' Consolidate PDFs


consolidatedPDFPath = folderPath & "\Consolidated_Report.pdf"
If MergePDFs(folderPath, consolidatedPDFPath, pdfApp) Then
MsgBox "Consolidated PDF saved successfully: " & consolidatedPDFPath
Else
MsgBox "Failed to save consolidated PDF."
End If

' Clean up
If Not pdfApp Is Nothing Then
pdfApp.Exit
End If
End Sub

Sub ExportPDF(ws As Worksheet, pdfFilePath As String)


' Export worksheet as PDF
ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfFilePath,
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False,
OpenAfterPublish:=False
End Sub

Function MergePDFs(folderPath As String, consolidatedPDFPath As String, pdfApp As


Object) As Boolean
' Merge individual PDFs into a single PDF
Dim pdfDoc As Object
Dim pdfFileName As String
Dim i As Integer

' Open the first PDF file


pdfFileName = Dir(folderPath & "\*.pdf")
If pdfFileName <> "" Then
Set pdfDoc = pdfApp.GetPDDoc(folderPath & "\" & pdfFileName)
If Not pdfDoc Is Nothing Then
' Open the rest of the PDF files and insert their pages into the first
PDF
Do While pdfFileName <> ""
If pdfFileName <> "Consolidated_Report.pdf" Then ' Skip the
consolidated PDF file
If pdfApp.Open(pdfFilePath) Then
pdfApp.InsertPages pdfFilePath,
pdfApp.GetNumPages(pdfFilePath), folderPath & "\" & pdfFileName, 0,
pdfDoc.GetNumPages
pdfDoc.Save PDSaveFull, consolidatedPDFPath
pdfApp.CloseDoc (pdfFileName)
End If
End If
pdfFileName = Dir
Loop

' Close the first PDF file


pdfDoc.Close
MergePDFs = True
End If
End If
End Function

Function GetFolderPath() As String


' Prompt user to select folder path
Dim folderDialog As FileDialog
Set folderDialog = Application.FileDialog(msoFileDialogFolderPicker)
folderDialog.Title = "Select Folder to Save PDFs"

If folderDialog.Show = -1 Then
GetFolderPath = folderDialog.SelectedItems(1)
Else
GetFolderPath = ""
End If
End Function

You might also like