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
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