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

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,166
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:

Scott T

Well-known Member
Joined
Dec 14, 2016
Messages
2,593
Office Version
365, 2016
Platform
Windows
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.
 

jtraylor

New Member
Joined
Feb 5, 2018
Messages
2
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.
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,166
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.
 

Forum statistics

Threads
1,082,323
Messages
5,364,590
Members
400,808
Latest member
formulasataglance

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top