VBA Excel to PDF -Only parts

I have got an excel sheet that is used to make invoices. Now, I want to export this as PDF and send it on outlook. I managed to create a code that works. The only part which I am not sure about is that there are some parts of the excel sheet that I don’t want to be saved into the PDF. I was wondering if anyone knows a way to do that? I guess it would have to do with changing the part of the code which I am attaching, but I’m not sure what.

Also, I tried just entering Range() after ActiveSheet and then manually input what I DO want. This worked but when saved as PDF, each range is in its own page


    Create the PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, 
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=OpenPDFAfterCreating
Participant Asked on May 10, 2018 in VBA: Macros.
Add Comment
1 Answer(s)
Sub toPDF()
' This will copy a specified range('s) from ws1 & ws2 to ws3.
' Now you can print ws3 to PDF with only the desired data.
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim lRow1 As Integer
Dim lRow2 As Integer
Dim lRow3 As Integer
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
Set ws3 = Worksheets("Sheet3")
lRow1 = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
lRow2 = Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp).Row
lRow3 = Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row
ws1.Range("B3:H" & lRow1).Copy Destination:=ws3.Range("A" & lRow3) ' you can adjust your page1 range here
lRow3 = Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row
ws2.Range("B3:H" & lRow1).Copy Destination:=ws3.Range("A" & lRow3) ' you can adjust your page2 range here
'ActiveSheet.PrintOut ActivePrinter:="PDFCreator" ' (I use PDFCreator, its free or you can put your PDF code here)
ActiveSheet.DisplayPageBreaks = False
End Sub
Expert Answered on August 20, 2018.
Add Comment
  • Found this useful?

    Please share using the share button above.

    If you found the answer is best answer for your question, Please mark as 'best answer' by clicking on the right tick mark icon at the left side of the answer.

    Found the answer useful and wants to credit the user, then vote the answer (vote up).

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.