Quick VBA cancel print question

micfly

Well-known Member
Joined
Sep 8, 2008
Messages
543
VBA Code:
Sub Button561_Click()
' Enter_Date_and_Print()
    Dim MyDate As Date
    MyDate = Application.InputBox("Please Enter a Date", "Date")
    Range("C3") = MyDate
    
    ' Change Copies:=2 (below) to 1 or however many copies you want
    ActiveWindow.SelectedSheets.PrintOut Copies:=2

End Sub

If the user clicks Cancel instead of entering a date it still prints. What can I add to this statement to cancel the print job if they click Cancel instead of OK?
Also, is there a way to get a prompt for which Printer to use?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Not perfect but seems to work. HTH. Dave
Code:
Sub Button561_Click()
' Enter_Date_and_Print()
Dim MyDate As Date
MyDate = Application.InputBox("Please Enter a Date", "Date")
If MyDate <> "12:00:00 AM" Then
Range("C3") = MyDate
' Change Copies:=2 (below) to 1 or however many copies you want
ActiveWindow.SelectedSheets.PrintOut Copies:=2
Else
MsgBox "No date entered!"
End If
End Sub
 
Upvote 0
You should also consider if the user just clicks OK without entering a date or presses ESC.
Below code takes care of everything.
Try it out and change the error messages as you wish or simply remove them.

VBA Code:
Sub Button561_Click()
' Enter_Date_and_Print()
    Dim MyDate As Date
    Dim Str As String
  
    Str = Application.InputBox("Please Enter a Date", "Date")
  
        Select Case Str
            Case False
                MsgBox "Error: input aborted!"
                Exit Sub
            Case ""
                MsgBox "Error: empty input"
                Exit Sub
            Case Is <> ""
                If IsDate(Str) = True Then
                    MyDate = Str
                    Range("C3") = MyDate
                    'Change Copies:=2 (below) to 1 or however many copies you want
                    ActiveWindow.SelectedSheets.PrintOut Copies:=2
                Else
                    MsgBox "invalid date"
                End If
            Case Else
                MsgBox "Error: unknown error"
                Exit Sub
        End Select
End Sub
 
Last edited:
Upvote 0
Solution
Not perfect but seems to work. HTH. Dave
Code:
Sub Button561_Click()
' Enter_Date_and_Print()
Dim MyDate As Date
MyDate = Application.InputBox("Please Enter a Date", "Date")
If MyDate <> "12:00:00 AM" Then
Range("C3") = MyDate
' Change Copies:=2 (below) to 1 or however many copies you want
ActiveWindow.SelectedSheets.PrintOut Copies:=2
Else
MsgBox "No date entered!"
End If
End Sub
Your code works with some limitations.
When the user clicks OK without entering a date or types in an invalid date, you'll get a TYPE MISMATCH ERROR in VBA
 
Upvote 0
Pete your approach is much better. Having a string input instead of a date input (ie. no error) from the inputbox was the part I was missing. I'm guessing micfly will appreciate it as well. Be safe. Dave
 
Upvote 0
Awesome, thanks guys, both work - that really helped! Any hope of getting a "Select Printer" option?
 
Upvote 0
Okay, thanks for the suggestion. I am having trouble understanding the printer part and would not know how to incorporate that in this code?
I just want it to ask me which printer (Select?) before it prints.
 
Upvote 0
This might be a bit much but U can trial it...
Code:
Application.CommandBars.ExecuteMso "PrintPreviewAndPrint"
HTH. Dave
 
Upvote 0

Forum statistics

Threads
1,215,641
Messages
6,125,982
Members
449,276
Latest member
surendra75

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