set print area for all sheets using vba

Kaps_mr2

Well-known Member
Joined
Jul 5, 2008
Messages
1,583
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to set the print range on each sheet on my workbook. However the following does not work:-

VBA Code:
For sh = 1 To output_workbook.Sheets.Count

    output_workbook.Sheets(sh).PageSetup.PrintArea = Range("a1:n28").Address
    output_workbook.Sheets(sh).PageSetup.Orientation = xlLandscape
    
    
     
Next sh

thanks
Kapa
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
What if you change this line:
VBA Code:
    output_workbook.Sheets(sh).PageSetup.PrintArea = Range("a1:n28").Address
to this:
Excel Formula:
    output_workbook.Sheets(sh).PageSetup.PrintArea = output_workbook.Sheets(sh).Range("a1:n28")
 
Upvote 0
thanks - that prints the range("a1:n28") on one page and the rest of the sheet on another page. The final output will be viewed as a pdf file. I am doing the pdf via excel rather than vba.
 
Upvote 0
Sorry, I forgot the ".Address" at the end of my line, i.e.
VBA Code:
    output_workbook.Sheets(sh).PageSetup.PrintArea = output_workbook.Sheets(sh).Range("a1:n28").Address
 
Upvote 0
hello - thanks.

So i now have as per below - but am still getting some sheets that ignore the defined print area.


VBA Code:
Public Sub SaveActiveWorkbookAsPDF()
Dim output_path As String
Dim strpathfile As String
Dim sh As Integer

For sh = 1 To output_workbook.Sheets.Count
 
    output_workbook.Sheets(sh).PageSetup.Orientation = xlLandscape
    output_workbook.Sheets(sh).PageSetup.PrintArea = output_workbook.Sheets(sh).Range("a1:n28").Address
      
Next sh

output_path = ThisWorkbook.path
strpathfile = output_path & "\output.pdf"


Application.DisplayAlerts = False

output_workbook.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=strpathfile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
  
  
End Sub
 
Upvote 0
Where exactly is "output_workbook" being defined/set?

Also, are you trying to print this to one PDF file, or multiple PDF files?
 
Upvote 0
hello,

Output_workbook is being defined at the top of the module.

I would like it as one file. thank you.

kind regards
Kaps
 
Upvote 0
It seems to work just fine for me.
Do you have other code overwriting your print ranges?
Are you sure that your data is not just spilling over to multiple pages?
 
Upvote 0
There is other data on the sheets but I don't want that printed. How can i check if there is other code overwriting my print ranges ?
 
Upvote 0
ss.png


is an example of the output. I want range "a1:n28" from each sheet.
 
Upvote 0

Forum statistics

Threads
1,215,212
Messages
6,123,655
Members
449,113
Latest member
Hochanz

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