How to set Fix Print area.

SamarthSalunkhe

Board Regular
Joined
Jun 14, 2021
Messages
103
Office Version
  1. 2016
Platform
  1. Windows
Hello Everyone,

I have set the print area in the sheet using VBA code, but I realise that the user can modify it using "Set Print Area" at the time of printing the page.

can someone help me to prevent this, as I don't want modification in the Print area to avid misuse of macro?

Currently, I'm using the below code for the print area.

VBA Code:
Private Sub Worksheet_Activate()

Sheet1.PageSetup.PrintArea = "$B$2:$D$39"

End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Attach your macro to the BeforePrint event instead of (or as well as) the Activate event. Then it will reset the print area at the time of print if your user has been fiddling with it.
 
Upvote 0
Attach your macro to the BeforePrint event instead of (or as well as) the Activate event. Then it will reset the print area at the time of print if your user has been fiddling with it.
sorry i didn't get you, can you please elaborate or provide code.

sorry but i'm beginner in VBA.
 
Upvote 0
In the VBA Editor, select ThisWorkbook in the Project pane, then at the top of the code window, where it says (General), click on the drop-down and select Workbook. Then to the right (where it says Open), instead choose BeforePrint. Then place your code into the subroutine:
VBA Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)

    Sheet1.PageSetup.PrintArea = "$B$2:$D$39"
    
End Sub
You are simply fixing the print area at the time the sheet is printed, rather than at the time of activating the worksheet. If you still want the print area set at the start, leave your current code in the sheet's Activate event (where it currently is).
 
Upvote 0
Solution
In the VBA Editor, select ThisWorkbook in the Project pane, then at the top of the code window, where it says (General), click on the drop-down and select Workbook. Then to the right (where it says Open), instead choose BeforePrint. Then place your code into the subroutine:
VBA Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)

    Sheet1.PageSetup.PrintArea = "$B$2:$D$39"
   
End Sub
You are simply fixing the print area at the time the sheet is printed, rather than at the time of activating the worksheet. If you still want the print area set at the start, leave your current code in the sheet's Activate event (where it currently is).
Thank you so much ChaireS, It works Perfectly. ?
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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