BeforePrint Event VBA Issues

jtraylor

New Member
Joined
Feb 5, 2018
Messages
2
I have a template that should only be used if it is for a certain ship to that ends up in a specific cell in t he workbook. I want to keep them from printing the file if the ship to does not match what I need it to match.

I have the below code in the ThisWorkbook module.

Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)


Dim Data_Book As String
Dim ShipTo As String


Data_Book = ThisWorkbook.Name
ShipTo = Workbooks(Data_Book).Sheets("Sheet1").Range("A12")


If ShipTo <> "Canada" Then
    Cancel = True
    MsgBox ("This template is ONLY for Canada orders. Please close this workbook and open the Non-Canada template."), vbOKOnly
End If


End Sub

I am using Excel 2016 but most of the users of this template will be running 2013. I have viewed many posts on how to do the Before Print event but I can't seem to get it to work for me. I do File --> Print and the code never runs. I do quick print and the code never runs.

Is it just because of the version of Excel I'm using? If yes is there anyway to keep them from printing it?

I'd even take something that would pop up a message box if the value in that cell isn't Canada. THe problem is that the template is tied to another file and a vlookup is dependent on them putting a valid sales order number in another cell so the VLOOKUP returns the actual Ship to. So if the value was #N/A I wouldn't want the message box to pop up.

Any help is much appreciated!!!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try this...

Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] Workbook_BeforePrint(Cancel [color=darkblue]As[/color] [color=darkblue]Boolean[/color])
    
    [color=darkblue]With[/color] ThisWorkbook.Sheets("Sheet1").Range("A12")
        [color=darkblue]If[/color] [color=darkblue]Not[/color] IsError(.Value) [color=darkblue]Then[/color]
            [color=darkblue]If[/color] .Value <> "Canada" [color=darkblue]Then[/color]
                Cancel = [color=darkblue]True[/color]
                MsgBox ("This template is ONLY for Canada orders. Please close this workbook and open the Non-Canada template."), vbOKOnly
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]Else[/color]
            MsgBox "Invalid ship to:", , ""
            Cancel = [color=darkblue]True[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Last edited:
Upvote 0
The code runs for me on 2010. Note that before print does not run until you hit the print button on the print preview screen.
 
Upvote 0
THANK YOU Alpha Frog! Just for my education why didn't my code work? I like your option better but in case I want to do a before print event again.
 
Upvote 0
THANK YOU Alpha Frog! Just for my education why didn't my code work? I like your option better but in case I want to do a before print event again.

You're welcome.

Don't know why your code didn't work. It looks fine to me. Perhaps the Application.EnableEvents was set to false. Maybe canada was not capitalized; the comparison in the code is case sensitive. I don't know.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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