Macro To Look through all tabs and Set Print Area

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hello All,


I am working with a workbook that has 12 tabs and a main tab that is the "Report" that gets populated into each tab.

I have macros that look at the "Report Tab" and say filter the report and look for the tab name, upon finding the tab name copy the data on the "Main Report Tab" and paste it into the appropriate Tab with a matching name.

I now need a macro that will go into each tab and set the print area from column B1:Q1 and then look under that until it see's the last bit of data and set the print area to that range, any ideas?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try this in the ThisWorkbook module

Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim ws As Worksheet, LR As Long
For Each ws In ActiveWindow.SelectedSheets
    LR = ws.Range("A" & Rows.Count).End(xlUp).Row
    ws.PageSetup.PrintArea = "A1:Q &LR"
Next ws
End Sub
 
Upvote 0
Vog,


Thank you for the quick reply. So I put this macro into This Workbook Module and it will automatically set the print range right before I select print every time?

If that's the case this macro is awesome! Thanks!
 
Upvote 0
Vog,


would I just change all the "A's" to cell or column reference B cause I need column A to not be on the print area?
 
Upvote 0
Vog,


I just tried it an I am getting a run-time error '1004' "The text you entered is not a valid reference or defined name?

it is highlighting, "

ws.PageSetup.PrintArea = "A1:Q &LR"
</pre>
 
Upvote 0
Oops!

Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim ws As Worksheet, LR As Long
For Each ws In ActiveWindow.SelectedSheets
    LR = ws.Range("B" & Rows.Count).End(xlUp).Row
    ws.PageSetup.PrintArea = "B1:Q" & LR
Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,962
Latest member
Fenes

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