i am a beginner at VBA and need help with this code. I placed on my sheet command button and connected it with macro (the code is down there) "PrintSelectionToPDF" and it goes smoothly through code until i press button cancel (if changed my mind and don't want to save selected range as PDF) in inputbox it sends me error message -> run-time error '424' Oject required [enter image description here][1]
-> this part of code->
Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)
Thank you in advance if you can find me a solution.
`Sub PrintSelectionToPDF()
Dim ThisRng As Range
Dim strfile As String
Dim myfile As Variant
If Selection.Count = 1 Then
Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)
Else
Set ThisRng = Selection
End If
'Prompt for save location
strfile = "Selection" & "_" _
& Format(Now(), "yyyymmdd_hhmmss") _
& ".pdf"
strfile = ThisWorkbook.Path & "\" & strfile
myfile = Application.GetSaveAsFilename _
(InitialFileName:=strfile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and File Name to Save as PDF")
If myfile <> "False" Then 'save as PDF
ThisRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Else
MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File
Selected"
End If
End Sub`
Related
I need to export excel sheet as PDF named with String and Cell Value for Example XXXY - Cell Value
Note: i do not need codes that uses path location for the saving file
I tried the following code
Sub IVI_Formatting_Export_Two_PDF ()
Dim strFilename As String
Dim srn As String Dim SWPDF As Worksheet
Set SWPDF = ThisWorkbook.Sheets ("BSS CPES MainPage")
srn = "CPE Main-Page"
strFilename = SWPDF.Range ("F7").Value
Create File name with Warehouse Name
Export2PDF
SWPDF.ExportAsFixedFormat
Type:=xlTypePDF,
Filename:=strFilename & srn,
Quality:=xl QualityStandard,
IncludeDocProperties:=False,
IgnorePrintAreas:=False,
From:=1,
To:=1,
OpenAfterPublish:=True
End Sub
You must specify a path to export PDF. Without path where it will be saved? You can use variable to specify path. Currently I have used same as file path. Your problem was in filename parameter. Try below sub.
Sub IVI_Formatting_Export_Two_PDF()
Dim strFilename As String
Dim srn As String
Dim SWPDF As Worksheet
Set SWPDF = ThisWorkbook.Sheets("BSS CPES MainPage")
srn = "CPE Main-Page"
strFilename = SWPDF.Range("F7").Value
'Create File name with Warehouse Name
'Export2PDF
SWPDF.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "\" & strFilename & "_" & srn & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
From:=1, _
To:=1, _
OpenAfterPublish:=True
'Clear memory
Set SWPDF = Nothing
End Sub
Here Filename:=ThisWorkbook.Path & "\" & strFilename & "_" & srn & ".pdf" will export the PDF to same folder where your workbook is located. Also you must include .pdf extension to export excel sheet as pdf.
This is the code for a button I have on my userform to change the active sheet in my workbook to a pdf. I'm wondering why not everything in my excel sheet is showing. The first 6-7 pages of my excel sheet show perfectly fine, and it's one or two sections at the end of my sheet that show portions, or nothing at all
Private Sub CommandButton3_Click()
Dim strFile As String
Dim invoiceRng As Range
Dim pdfFile As String
'Setting range to be printed
Set invoiceRng = Range("A1:D850")
'setting file name with a time stamp.
strFile = "Checklist" & "_" & Format(Now(), "yyyymmdd_hhmmss") & ".pdf"
'setting the name. The resultent pdf will be saved where the main file exists.
pdfFile = ThisWorkbook.Path & Application.PathSeparator & strFile
invoiceRng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=pdfFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=True, _
OpenAfterPublish:=True
End Sub
I am currently using VBA to generate an automated letter for me. Using the below:
Sub CreatePDF()
Dim wSheet As Worksheet
Dim vFile As Variant
Dim sFile As String
Set wSheet = ActiveSheet
sFile = Replace(Replace(wSheet.Name, " ", ""), ".", "_") _
& "_" _
& Format(Now(), "yyyymmdd\_hhmm") _
& ".pdf"
sFile = ThisWorkbook.Path & "\" & sFile
vFile = Application.GetSaveAsFilename _
(InitialFileName:=sFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If vFile <> "False" Then
wSheet.Range("P1:Y336").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=vFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
MsgBox "PDF file has been created."
End If
End Sub
Wondering if there is a way to make the generated file name of the PDF to correspond to specific cell inputs (in the sheet, they are pulled in by vlookup). Ideally, have the file be: C7_C8.pdf
not sure if you mean the cell address, or what's inside the cell.
But i just hardcode the "sFile" to a specific cell in my worksheet :
sFile = Workbooks("blabla.xlsb").Sheets("Sheet1").Range("AE14")
into the below, notice the "" &
vFile = Application.GetSaveAsFilename _
(InitialFileName:="" & sFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
so whatever cell AE14 contains it will be passed as the PDF filename in the Save As prompt. hope it helps!
I am a newbie to Excel VBA, and I get this code from https://trumpexcel.com/convert-excel-to-pdf/ to print selection into PDF.
My only concern at the moment is, how to make all of my uncontinuous selections printed into 1 single PDF. If anybody can help, I would be very grateful for that.
For example, in the photo below, you can see that if I select from the first row to the last row, then when I print Selection into PDF, it will be converted into 1 single page. But for example, I only want to print the Product 1 and Product 3 (skipping product 2), then I will only be able to print them into 2 separated pages. How can I make this into 1 page is my question.
Code:
Sub PrintSelectionToPDF()
Dim ThisRng As Range
Dim strfile As String
Dim myfile As Variant
If Selection.Count = 1 Then
Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)
Else
Set ThisRng = Selection
End If
'Prompt for save location
strfile = "Selection" & "_" _
& Format(Now(), "yyyymmdd_hhmmss") _
& ".pdf"
strfile = ThisWorkbook.Path & "\" & strfile
myfile = Application.GetSaveAsFilename _
(InitialFileName:=strfile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and File Name to Save as PDF")
If myfile <> "False" Then 'save as PDF
ThisRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Else
MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File Selected"
End If
End Sub
I have a vba code that converts sheet "ConversiePDF" into PDF.
The problem is that the whole sheet is generated by formulas and half of the sheet is beeing populated with data and the other half remains blank (with formulas running but returning "")
My code converts the whole sheet that goes as far as row 1000 and generates 32 pages (of witch 30 are empty - just the table with no real data)
I need it to ignore the blank cells where formulas returned "" IF THE WHOLE ROW from A to Q is empty or returns "". (if only one-two or three cells from a row are empty this should NOT apply)
Please help..
'This is my code till now:
Dim ThisRng As Range
Dim strfile As String
Dim myfile As Variant
'Selectie Sheet pentru conversie
With ActiveWorkbook.Sheets("ConversiePDF")
Set ThisRng = .Range("A2", .Cells(.Rows.Count, "F").End(xlUp))
End With
Thank you..
Private Sub CommandButton1_Click()
'BUTON CONVERSIE PDF - ANGAJATORI
'DESCRIPTION: CONVERSIE A UNUI SHEET IN PDF
Dim ThisRng As Range
Dim strfile As String
Dim myfile As Variant
'Selectie Sheet pentru conversie
Application.ScreenUpdating = False
ActiveWorkbook.Sheets("PDF ANG").Visible = True
'Selectie filtru pentru ce sa converteasca
ActiveWorkbook.Sheets("PDF ANG").Select
Selection.AutoFilter Field:=1, Criteria1:="<>" 'column A
Selection.AutoFilter Field:=5, Criteria1:="<>" 'column E
'Selectie raza pentru conversie
With ActiveWorkbook.Sheets("PDF ANG")
Set ThisRng = .Range("A2", .Cells(.Rows.Count, "F").End(xlUp))
End With
'Prompt for save location
strfile = "Selection" & "_" _
& Format(Now(), "yyyymmdd_hhmmss") _
& ".pdf"
strfile = ThisWorkbook.Path & "\" & strfile
myfile = Application.GetSaveAsFilename _
(InitialFileName:=strfile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and File Name to Save as PDF")
If myfile <> "False" Then 'save as PDF
ThisRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Else
MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File Selected"
End If
ActiveWorkbook.Sheets("PDF ANG").ShowAllData
ActiveWorkbook.Sheets("PDF ANG").Visible = False
End Sub