Macro to assign to print button with specific criteria

John Moe

New Member
Joined
Aug 16, 2024
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

I'm in need of VBA code that I can assign to a print button to print with certain criteria. I can create the button and assign a macro, no problem. I'm just having trouble writing the code.

What I need is a macro that will:

Ask the # of copies to print
Print a selection of $A$1:$E$29,$G$33:$L$61 from the active sheet # plus an i
To a specific printer on my network named: “Printer Name”


I have sheets in my workbook named 1-31 (corresponding to dates in a month). I also have sheets in the same workbook named 1i, 2i, 3i….through 31i. Ideally, the PRINT button would be repeated on all sheets, 1-31, but the command would print the selection on the corresponding sheet 1i, 2i, 3i…

Simply, the print button on sheet 1 would print the selection on sheet 1i, the print button on sheet 2 would print the selection on sheet 2i, etc.

I hope this is understandable!


Thanks!
 
You can try the below :

VBA Code:
Sub PrintRange()
Dim varInput As Variant
Dim result As Integer
Dim prntArea As String
Dim sht As Worksheet

prntArea = "$A$1:$E$29,$G$33:$L$61"

varInput = InputBox("How many copies to print?")

If IsNumeric(varInput) Then

    With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .DisplayAlerts = False
    End With
    
    Set sht = Nothing
    
    Set sht = Sheets(ActiveSheet.Name & "i")
    sht.Activate
    
    ActiveSheet.PageSetup.PrintArea = prntArea
    ActiveWindow.SelectedSheets.PrintOut Copies:=varInput, collate:=True, _
    IgnorePrintAreas:=False

End If

With Application
.EnableEvents = True
.ScreenUpdating = True
.DisplayAlerts = True
End With

End Sub
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You ended up with an orphaned End With, so I think that's why you got the error. I should have posted the whole thing, which is similar to the above but I prefer my style of indentation. Next time when posting code, please paste within code tags (use vba button on posting toolbar). It's so much easier to follow indented code.
VBA Code:
Sub PrintRange()
Dim varInput As Variant
Dim result As Integer
Dim prntArea As String
Dim sht As Worksheet

On Error GoTo errHandler
prntArea = "$A$1:$E$29,$G$33:$L$61" 'edit here if required
varInput = InputBox("How many copies to print?")
If IsNumeric(varInput) Then
'set your own number or just comment out or remove this section
'************
    If varInput > 20 Then
        result = MsgBox("Are you sure you want to print " & varInput & " copies?", vbOKCancel)
        If result = vbCancel Then Exit Sub
    End If
'***********
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        .DisplayAlerts = False 'this may not be wise
    End With
    ActiveSheet.PageSetup.PrintArea = prntArea
    Set sht = Sheets(ActiveSheet.Name & "i")
    sht.Activate
    ActiveWindow.SelectedSheets.PrintOut Copies:=varInput, collate:=True, _
        IgnorePrintAreas:=False
End If
   

exitHere:
With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .DisplayAlerts = True
End With
Set sht = Nothing
Exit Sub

errHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume exitHere

End Sub
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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