Error with Saving PDF.

SkyeS

New Member
Joined
May 21, 2015
Messages
40
For some reason I keep getting an error when I am trying to save two sheets as a PDF. Still new to VBA so i know there is a better way to do this all but this is how i have it set up right now. I always want the Letter sheet to be the first sheet in the PDF, but then I want either CAt, Dog or Bird sheet to be saved afterwards depending on which one the user picks. Right now I get an error and its not saving. I'm not sure what the reason is. I know it has to be a simple syntax error. If anyone could help me that would be wonderful.

Sub GeneratePDF()


MsgBox "start"


If Sheets("Main").Range("G17") = 1 Then
Sheets(Array("Letter", "Cat")).Select
ActiveSheet.ExportAsFixedFormat_ Type:=xlTypePDF, Filename:="D:\" & Sheets("Main").Range("B5").Value & ".pdf"
MsgBox "Saved Cat"
End If


If G18 = 2 Then
Sheets(Array("Letter", "Dog")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="D:\" & Sheets("Main").Range("B5").Value & ".pdf"
MsgBox "Saved Dog"
End If


If G19 = 3 Then
Sheets(Array("Letter", "Bird")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="D:\" & Sheets("Main").Range("B5").Value & ".pdf"
MsgBox "Saved Bird"
End If




End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try this:

Code:
Sub GeneratePDF()

Dim sht_name As String


Application.ScreenUpdating = False


Select Case Sheets("Main").Range("G17")


    Case 1: sht_name = "Cat"
    Case 2: sht_name = "Dog"
    Case 3: sht_name = "Bird"


End Select


Sheets(Array("Letter", sht_name)).Select
ActiveSheet.ExportAsFixedFormat_ Type:=xlTypePDF, Filename:="D:\" & Sheets("Main").Range("B5").Value & ".pdf"


MsgBox "Saved " & sht_name


Application.ScreenUpdating = True


End Sub
 
Upvote 0
Thank you for responding!

I get an error on this line saying "Subscript out of range"

Sheets(Array("Letter", sht_name)).Select
 
Upvote 0
Sorry. Figured out that problem. (All my fault.) But now I am getting an error in this line

ActiveSheet.ExportAsFixedFormat_ Type:=xlTypePDF, Filename:="D:\" & Sheets("Main").Range("B5").Value & ".pdf"

Saying "Object doesnt support this property or method."
 
Upvote 0
The code works perfectly for me. Are you sure the sheet names you've entered in the code (Cat/Dog/Bird) are identical to the actual sheet names? Any leading or trailing spaces would cause the code to fall over.
 
Upvote 0
Yeah. I already checked that. I played around with it more and I keep getting that it doesn't support that Property or method.
 
Last edited:
Upvote 0
I tried a different way with the code and it ended up finally working. It doesn't make sense why it didn't work with your code. Logically it should've. But thank you so much for your help!
 
Upvote 0

Forum statistics

Threads
1,215,379
Messages
6,124,610
Members
449,174
Latest member
ExcelfromGermany

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top