Least used print area

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

Having issues with printing areas in worksheets, so have the following (partial) code:
Code:
With Activesheet.PageSetup
  .PaperSize = xlPaperA4
  .Orientation = xlPortrait
  x = (wks.HPageBreaks.Count + 1) * (wks.VPageBreaks.Count + 1)
  .Orientation = xlLandscape

  'Orientate the page for least number of page breaks
  .Orientation = IIf(x < (wks.HPageBreaks.Count + 1) * (wks.VPageBreaks.Count + 1), xlPortrait, xlLandscape)
            
  .Zoom = False
  .FitToPagesWide = 1
  .FitToPagesTall = False
End With
I would each sheet's print area to be the least number of A4 pages possible, optimised for landscape or portrait orientation.

I have one sheet where landscape orientation means 971 pages, but portrait cuts it to 455. I think I need to also adjust .FitToPagesWide and .FitToPagesTall, but have tried setting both to 1 (i.e. 1 page wide by 1 page tall), but this doesn't create the optimal print area.

Any suggestions?

TIA,
Jack
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You had activesheet and then a sheet variable so that needed changing but apart from that its changing the orientation correctly for me.

Code:
With ActiveSheet.PageSetup
  .PaperSize = xlPaperA4
  .Orientation = xlPortrait
  x = (ActiveSheet.HPageBreaks.Count + 1) * (ActiveSheet.VPageBreaks.Count + 1)
  .Orientation = xlLandscape
  'Orientate the page for least number of page breaks
  .Orientation = IIf(x < (ActiveSheet.HPageBreaks.Count + 1) * (ActiveSheet.VPageBreaks.Count + 1), xlPortrait, xlLandscape)
End With
 
Upvote 0
Thanks Steve, sheet variable was argument being passed in, that first With should have been With wks.PageSetup, must have copied some testing code was using.

I think it is selecting the least page orientation but just now, running on colleague's PC, it "seemed" to be ignoring columns to the right and only showing 2 on a portrait page (expecting at least 5 columns). Keep at it..
 
Upvote 0

Forum statistics

Threads
1,216,796
Messages
6,132,745
Members
449,756
Latest member
AdkinsP

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