print preview not working

ira daboy

New Member
Joined
Oct 10, 2013
Messages
20
Office Version
  1. 365
Platform
  1. Windows
I am trying to build a button to print two specific sheets, "PS" and "PL" are the name of the worksheets. But before the print I would like to preivew and if the preview is not acceptable then stop executing code. what is happening in the code i have build, if i Xout of the print preview it goes ahead and print anyway. does anyone know how i can stop it from printing if the preview in not acceptable? Below is the code i have so far. thanx

VBA Code:
Private Sub CommandButton4_Click()
  Dim PrntRng As Range
Set PrntRng = Application.InputBox("Select Range To Print", "Select Range", Type:=8)
ActiveSheet.PageSetup.PrintArea = PrntRng.Address
Debug.Print PrntRng.Columns.Count
Debug.Print PrntRng.Rows.Count

Sheets(Array("PS", "PL")).PrintPreview
    Sheets(Array("PS", "PL")).PrintOut
    Sheets("PL").Select
End Sub
 
Last edited by a moderator:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

When posting vba code in the forum, please use the available code tags. My signature block below has more details. I fixed it for you this time. :)

Would adding this line work for you?

Rich (BB code):
Sheets(Array("PS", "PL")).PrintPreview
If MsgBox("Confirm to print?", vbYesNo) = vbYes Then Sheets(Array("PS", "PL")).PrintOut
Sheets("PL").Select
 
Upvote 0
thank you Peter, that worked great. and i have taken your advise and updated my account info.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)

.. and thanks for updating your details. (y)
 
Upvote 0
Hi Peter, I would like to update this a little more to make the user pick a range. And if no range is selected it will say "You must pick a print range" then give them an opportunity to pick a range or cancel.
This code as it is asks me to debug or end. Is this possible?
 
Upvote 0
I don't have enough information to write any full code for you.

You apparently want to print two worksheets (PS and PL) but
  • You are only setting the print range on one sheet, and
  • That one sheet that you are setting the print range on may not even be one of the sheets to be printed. In your code, Print range is being applied to the ActiveSheet which, as far as I know, could be any worksheet in the workbook.
 
Upvote 0
Sorry, I have actually changed my code a bit. Below is what I have now. PL will always be the active sheet, this is where my command button is. So when execute my code if I don't select a range or if I click "Cancel" I get and error (see attached). Instead of an error I would like a msg box to come up and say "You must pick a print range" or if cancel is selected it will terminate the code without an error. thanks for your help with this.


Private Sub CommandButton4_Click()
Dim PrntRng As Range
Set PrntRng = Application.InputBox("Select Range To Print", "Select Range", Type:=8)

ActiveSheet.PageSetup.PrintArea = PrntRng.Address
Debug.Print PrntRng.Columns.Count
Debug.Print PrntRng.Rows.Count
Sheets(Array("PS", "PL")).Select
Application.Dialogs(xlDialogPrint).Show
Sheets("PL").Select
End Sub
 

Attachments

  • error.JPG
    error.JPG
    35.8 KB · Views: 14
Upvote 0
CommandButton4 is on Sheet4 (PL)
:oops: Oops, I missed that you had already stated that.


:oops: Oops for you too, you missed or forgot my earlier point
When posting vba code in the forum, please use the available code tags. My signature block below has more details.
I did not fix it for you this time (post #7)

See if this would suffice. It gives the user two chances (adjustable in the 'Const' line) to choose a print range on 'PL' otherwise the code does nothing.

VBA Code:
Private Sub CommandButton4_Click()
  Dim PrntRng As Range
  Dim Tries As Long
  
  Const AllowedTries As Long = 2
  
  Do
    On Error Resume Next
    Set PrntRng = Application.InputBox("Select Range To Print", "Select Range", Type:=8)
    On Error GoTo 0
    Tries = Tries + 1
  Loop Until Not PrntRng Is Nothing Or Tries = AllowedTries
  If Not PrntRng Is Nothing Then
    ActiveSheet.PageSetup.PrintArea = PrntRng.Address
    Sheets(Array("PS", "PL")).PrintPreview
    If MsgBox("Confirm to print?", vbYesNo) = vbYes Then Sheets(Array("PS", "PL")).PrintOut
    Sheets("PL").Select
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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