I have a userform that contains a listbox. My workbook has 2 worksheets and my listbox grabs data from both sheets depending what I am searching for.
How do I export the contents of my listbox to pdf regardless of worksheet?
I found this generic macro that can be assigned to a command button in a userform but it will only export contents from active sheet and not content in the listbox.
Sub PDFActiveSheet()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler
Set ws = ActiveSheet
'enter name and select folder for file
' start in current workbook folder
strFile = Format(Now(), "yyyymmdd\_hhmm") _
& ".pdf"
strFile = ThisWorkbook.Path & "\" & strFile
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If myFile <> "False" Then
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
With ws.PageSetup
.CenterHeader = "Report"
.Orientation = xlLandscape
.Zoom = True
.FitToPagesTail = False
.FitToPagesWide = 1
End With
MsgBox "PDF file has been created."
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub