Set print area for multiple sheets using VBA

Snail123

New Member
Joined
Sep 24, 2015
Messages
6
Hello,
I am very new to VBA and I am looking to create a macro to define the print area for each worksheet in my workbook.

Currently this is the code I am using and it is assigned to a button; the trouble I have is that instead of setting the print area to the last cell of each worksheet (the number of columns and rows on each are different), it only sets the print area to row 17, i.e. the last cell in Sheet 1 where the button is placed.

Is there a way where the button can stay on Sheet 1 but the print area is set to the last cell used (row and column with data) for Sheets 1 to 8?

Code:

Sub Print_area()
'Get values
Dim wks As Worksheet
Dim lastCell As Long
lastCell=Range("B" & Rows.Count).End(xlUp).Row
For Each wks in Worksheets
wks.PageSetup.PrintArea = "B1:J" & lastCell
Next wks

'Formatting
For Each wks In ThisWorkbook.Worksheets
With wks.PageSetup
.LeftHeader = "Test"
.CenterHeader = "Test"
.CenterHorizontally = True
.Orientation = xlLandscape
.PaperSize = xlPaperA2
.FitToPagesWide = 1
End With
Next wks
End Sub

Thanks for your help (in anticipation!)

PS. I'm using MS Excel 2013
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this way
Code:
Sub Print_area()
    'Get values
    Dim wks As Worksheet
    Dim lastCell As Long
    
    For Each wks In ActiveWorkbook.Worksheets
        lastCell = wks.Range("B" & Rows.Count).End(xlUp).Row
        wks.PageSetup.PrintArea = "B1:J" & lastCell
        With wks.PageSetup
            .LeftHeader = "Test"
            .CenterHeader = "Test"
            .CenterHorizontally = True
            .Orientation = xlLandscape
            .PaperSize = xlPaperA2
            .FitToPagesWide = 1
        End With
    Next wks
End Sub
 
Upvote 0
Hello,
I am very new to VBA and I am looking to create a macro to define the print area for each worksheet in my workbook.

Currently this is the code I am using and it is assigned to a button; the trouble I have is that instead of setting the print area to the last cell of each worksheet (the number of columns and rows on each are different), it only sets the print area to row 17, i.e. the last cell in Sheet 1 where the button is placed.

Is there a way where the button can stay on Sheet 1 but the print area is set to the last cell used (row and column with data) for Sheets 1 to 8?

Code:

Sub Print_area()
'Get values
Dim wks As Worksheet
Dim lastCell As Long
lastCell=Range("B" & Rows.Count).End(xlUp).Row
For Each wks in Worksheets
wks.PageSetup.PrintArea = "B1:J" & lastCell
Next wks

'Formatting
For Each wks In ThisWorkbook.Worksheets
With wks.PageSetup
.LeftHeader = "Test"
.CenterHeader = "Test"
.CenterHorizontally = True
.Orientation = xlLandscape
.PaperSize = xlPaperA2
.FitToPagesWide = 1
End With
Next wks
End Sub

Thanks for your help (in anticipation!)

PS. I'm using MS Excel 2013


Hi Momentum,

Thanks for this wonderful help, but could you please help if I want to select print ranges in selected work sheets (For Eg: Sheet 2,3,4 only), not all work sheets and in addition till the value in column B in the mention 3 sheets greater than 0 not last cell.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,603
Members
449,089
Latest member
Motoracer88

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