Macro to set the number of print pages runs on BeforePrint, but then the whole sheet is printed

Anekcapa

New Member
Joined
Jan 9, 2019
Messages
2
Hi everyone,
I am trying to have Excel print a specific number of pages according to a cell value. I have it working perfectly for a form control button in my spreadsheet, but i want it to do the same when the user prints from the file menu. I do not know vba so have copied the working code from my button into the ThisWorkbook object using BeforePrint. This works perfectly as it does with the button, but after it runs the whole worksheet then prints as well. Can anyone please tell me how to stop the second print event from happening, or do I need to take a different approach?
Thanks

Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    For Each xWs In Application.ActiveWorkbook.Windows(1).SelectedSheets
    Dim WsName As String
    WsName = "Labeling"
        If xWs.Name = WsName Then
            Dim PageTo As String
            PageTo = Range("o11").Value
            If PageTo = "" Then
                MsgBox "There is no data to print." & vbCrLf & "" & vbCrLf & "Please paste a Reservation Manifest into cell A2"
                Exit Sub
            End If
            PageTo = Range("o11").Value
            
            ActiveWindow.SelectedSheets.PrintOut From:=1, To:=PageTo, Copies:=1, Collate _
            :=True, IgnorePrintAreas:=False
            
        End If
    Next
    
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Dim WsName As String
Cancel = True ' try adding this line
WsName = "Labeling"
 
Last edited:
Upvote 0
Dim WsName As String
Cancel = True ' try adding this line
WsName = "Labeling"

Hi Jim, thank you for replying
Unfortunately adding a cancel stops both the macro and the second print, so nothing happens. I have also tried adding it later in the sub, and right before 'End Sub'
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

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