export as fixedformat pdf - orientation can't get last sheet to landscape

mdv1981

New Member
Joined
May 8, 2013
Messages
3
Hello all,

I'm new here but I hope you can help me out.

I've code to export multiple sheets in a workbook to pdf using 'ExportAsFixedFormat Type:=xlTypePDF'
All sheets are set to landscape. The export works fine however most of the time the last sheet is orientated as portrait instead of landscape.
I've tried a lot but can't get it right.

Hope you can help.

Thanks Maarten.

PS: When I export the last sheet alone the export is fine and in landscape!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You might improve your chance of getting help if you post your code.
 
Upvote 0
Here is my code:
For Each Sheet In ActiveWorkbook.Sheets
s2 = Sheet.Name
If s2 <> "vbpag" Then
Sheets(s2).PageSetup.Orientation = xlLandscape
Sheets(s2).Select
Range("A1:P28").Select
End If
Next
Sheets(Array("120", "123", "131", "142", "144", "146", "151", "152", "154", "156", "157", _
"159", "175", "194", "171", "172", "173", "174", "181", "191", "301")).Select

Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Mydir" & "filename", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=True

Like I said it works fine on all sheets except that the last sheet is published in portrait instead of landscape.
 
Upvote 0
Here is my code:
For Each Sheet In ActiveWorkbook.Sheets
s2 = Sheet.Name
If s2 <> "vbpag" Then
Sheets(s2).PageSetup.Orientation = xlLandscape
Sheets(s2).Select
Range("A1:P28").Select
End If
Next
Sheets(Array("120", "123", "131", "142", "144", "146", "151", "152", "154", "156", "157", _
"159", "175", "194", "171", "172", "173", "174", "181", "191", "301")).Select

Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Mydir" & "filename", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=True

Like I said it works fine on all sheets except that the last sheet is published in portrait instead of landscape.
Strange and I'm not sure what's happening since the code looks ok.
Try this. Before the sheets array line, add a new sheet and call it "Blank". Add Blank as the last array item. It will produce a blank sheet in the PDF file at the end of the document. After the export as pdf line you can delete the Blank sheet. Now does the last "real" sheet print in landscape?
 
Upvote 0
Strange and I'm not sure what's happening since the code looks ok.
Try this. Before the sheets array line, add a new sheet and call it "Blank". Add Blank as the last array item. It will produce a blank sheet in the PDF file at the end of the document. After the export as pdf line you can delete the Blank sheet. Now does the last "real" sheet print in landscape?

Hi Joe,

I've tried that one earlier. (only called the sheet 'last' :)). What happened was that it worked the first time I ran the code. After that both sheet '301' and the last/blank sheet were portrait instead of landscape.

Maarten.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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