-1

I am beyond confused, I have been looking into many different solutions and I keep getting errors.

Goal: I have a workbook with many separate sheets. I need two of the sheets to save in my system as two separate PDFs. These sheets are daily returns and will be updated daily. With that being said, I am okay if the PDFs overwrite the PDFs from the day before.

Problem: I've tried many different variations of code and can't seem to get it right.

Here are links to methodologies I have tried. I'm not going to put in any sample code because I have tried many different ones and don't feel like I am close on any of them.

Links to solutions I've Tried:

https://www.youtube.com/watch?v=zBFVm14biFI

http://www.contextures.com/excelvbapdf.html

VBA Print to PDF and Save with Automatic File Name

https://www.thespreadsheetguru.com/the-code-vault/vba-to-quickly-create-a-pdf-document-from-selected-excel-worksheets

https://exceloffthegrid.com/vba-code-save-excel-file-as-pdf/

Thank you so much.

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
Sean. D 1528
  • 29
  • 2
  • 6
  • Possible duplicate of [Excel VBA to Export Selected Sheets to PDF](https://stackoverflow.com/questions/20750854/excel-vba-to-export-selected-sheets-to-pdf) – Comintern Jul 24 '18 at 03:09
  • 1
    You don't post any code but feel that a list of reading assignments and you tubes videos is a fair substitute? Occam's razor would suggest that the only work you've done toward a solution is gather a collection of links you never bothered to read/watch yourself. –  Jul 24 '18 at 03:13
  • Not at all, I was simply asking a question in hopes that someone had code that worked for my goal. I could have posted 4 different sets of code that would have yielded a similar response for someone like you. The point is, I'm confused and asked the community for help. If you can't help don't comment. – Sean. D 1528 Jul 24 '18 at 03:21
  • 1
    You're forgetting that [your history](https://stackoverflow.com/users/10024825/sean-d-1528) of asking poor questions is readily available to anyone. –  Jul 24 '18 at 03:31

1 Answers1

2

Try this:

Sub ExportAsPDF()

Dim Worksheet1 As Worksheet
Dim Worksheet2 As Worksheet
Dim Worksheet1_Name As String
Dim Worksheet2_Name As String

Worksheet1_Name = "Sheet1" ' Replace this with the name of the first sheet you want to export
Worksheet2_Name = "Sheet2" ' Replace this with the name of the second sheet you want to export
Set Worksheet1 = ThisWorkbook.Worksheets(Worksheet1_Name)
Set Worksheet2 = ThisWorkbook.Worksheets(Worksheet2_Name)

Dim Write_Directory As String
Dim Worksheet1_Path As String
Dim Worksheet2_Path As String

Write_Directory = "C:\Users\YOUR_USERNAME\Documents" ' Replace this with the full path of the directory you want to save to
Worksheet1_Path = Write_Directory & "\" & Worksheet1_Name
Worksheet2_Path = Write_Directory & "\" & Worksheet2_Name

Worksheet1.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=Worksheet1_Path, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

Worksheet2.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=Worksheet2_Path, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

Application.StatusBar = "Exported " & Worksheet1_Name & " & " & Worksheet2_Name & " to PDF!"

End Sub
user5305519
  • 3,008
  • 4
  • 26
  • 44