Set up print button to function only on 1 sheet

MartinBecks

New Member
Joined
Jul 7, 2014
Messages
12
I have 4 sheets in my workbook. Only 1 sheet named "Data Sheet" do people enter information and depending on the numbers of rows filled out it prints accordingly. I'm ok there. My issue is in printing the other sheets which has instructions and reference material. When I try printing another tab the message box appears. How do I modify the code so the print button only works for the "Data Sheet"?

I know all I need is a little fix but I didn't originate this code, a colleague did. In advance thank you.


I have the following code in ThisWorkbook
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)If PrtOK Then
Cancel = False
Else
MsgBox "Please use the embedded print button"
Cancel = True
End If
End Sub

and the following code in Module 1
Code:
Public PrtOK As BooleanSub Button41_Click()
PrtOK = True
Dim LastRow As Long
LastRow = Range("L" & Rows.Count).End(xlUp).Row
Set HeaderRange = Range("$A$1:$Q$16")
Set BodyRange = Range("$A$17:$Q$" & LastRow)
Set FooterRange = Range("$A$216:$Q$220")
Set UnionRange = Application.Union(HeaderRange, BodyRange)
With ActiveSheet.PageSetup
    .PrintArea = UnionRange.Address
End With
'show print dialog
Application.Dialogs(xlDialogPrint).Show
PrtOK = False
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Maybe this
Code:
Public PrtOK As BooleanSub Button41_Click()
PrtOK = True
Dim LastRow As Long, ws As Worksheet
LastRow = Range("L" & Rows.Count).End(xlUp).Row
Set ws = Sheets("Data Sheet")
Set HeaderRange = ws.Range("$A$1:$Q$16")
Set BodyRange = ws.Range("$A$17:$Q$" & LastRow)
Set FooterRange = ws.Range("$A$216:$Q$220")
Set UnionRange = Application.Union(HeaderRange, BodyRange)
With ws.PageSetup
    .PrintArea = UnionRange.Address
End With
'show print dialog
Application.Dialogs(xlDialogPrint).Show
PrtOK = False
End Sub
 
Upvote 0
The coded didn't work. I copied and pasted your code into Module 1 and I still got the message box prompting me to use the print button.
 
Upvote 0
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If ActiveSheet.Name <> "Data Sheet" Then Exit Sub
If PrtOK Then
Cancel = False
Else
MsgBox "Please use the embedded print button"
Cancel = True
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

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