Before Print question

robfo0

Active Member
Joined
Feb 19, 2002
Messages
281
Hi everyone,

I have this code to prevent printing if cell A1 is empty:
Sub Workbook_BeforePrint(cancel As Boolean)

If Range("A1").Value = "" Then
cancel = True
Else
End If

End Sub


it works fine from "Thisworkbook" module, but I need this code to be on the sheet i want the code to apply to. There are many reasons for this, the main one is that i copy and paste this sheet with code, so i need this print code to "follow" it and work. the problem is, the code wont work from the sheet module. Is there anyway to make it work?

Thanks
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Here's some code I use to do a few tricks before print. Your need to test for the right sheet first.

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim sht As Worksheet
Dim intResponse As Integer
'Prompts the user for a date if printing the PROPOSAL
If ActiveSheet.Name = ("PROPOSAL") Then
Range("F28").Select
intResponse = MsgBox(prompt:="INSERT TODAY'S DATE?", _
Buttons:=vbQuestion + vbYesNoCancel, Title:="REMINDER")
Select Case intResponse
Case vbYes
ActiveCell.FormulaR1C1 = "=TODAY()"
Selection.NumberFormat = "mmmm d, yyyy"
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Case vbNo
Exit Sub
Case vbCancel
Cancel = True
End Select
End If
'Inserts the path in the footer of every sheet
For Each sht In ThisWorkbook.Sheets
sht.PageSetup.LeftFooter = "&8" & _
ThisWorkbook.FullName
Next sht
End Sub
 
Upvote 0
This is my first posting here, but your question intrigued me. The only thing I ever saw that might get you what you appear to want was a Mr. Excel tip about copying code into new workbooks.

You might pursue the following idea,
save your Workbook_BeforePrint() subroutine into a text (or bas?) file. Then when you go to copy the sheet figure out a way to execute the following in the target workbook:

Workbooks(x). ...

or

ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.AddFromFile "C:my beforeprint.txt"

Hope this was of some help.
 
Upvote 0

Forum statistics

Threads
1,222,682
Messages
6,167,621
Members
452,124
Latest member
lozdemr

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