VBA Code to select printer based on cell

TWill08

New Member
Joined
Aug 7, 2018
Messages
3
Hello,
I am having a very difficult time finding a way to have a macro select the printer to used based on a cell value. Basically I have 2 printers setup & installed on my computer. I am looking for the macro to select "printer A" if cell A9 =1. If cell A9=2 then I want it to select "printer B". I have the rest of the code written to select the print areas and print copies based on a cell value but selecting the printer is the last step I am having difficulty with. Below is my current code without the select correct printer:

Sub Macro1()
'
' Macro1 Macro
'

'
Sheets("Label").Select
If Sheets("Label").Range("D9") = 1 Then
ActiveSheet.PageSetup.PrintArea = "$B$19:$J$29"
End If
If Sheets("Label").Range("D9") = 2 Then
ActiveSheet.PageSetup.PrintArea = "$B$32:$J$42"
End If
ActiveWindow.SelectedSheets.PrintOut Copies:=Range("F17").Value, Collate:=True, _
IgnorePrintAreas:=False
Sheets("Home Screen").Select
Range("B4").Select
Selection.ClearContents
Range("B3").Select
Selection.ClearContents
Range("B2").Select
Selection.ClearContents

End Sub

Any help would be greatly appreciated.
Thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Call this procedure before the PrintOut line:

Code:
Sub SetPrinter()
  Dim strPrinterName As String
  
  Select Case ThisWorkbook.Sheets("Label").Range("A9").Text
    Case "1": strPrinterName = "Printer A"
    Case "2": strPrinterName = "Printer B"
  ' Case Else: strPrinterName = "Printer C"
  End Select
  
  Application.SendKeys strPrinterName & "~"
  Application.Dialogs(xlDialogPrinterSetup).Show
End Sub

Add the procedure above to the module, then add this line before PrintOut:
Code:
Call SetPrinter
 
Upvote 0
Hello,
Thank you very much for your help. I have added what you told me but am still having a little trouble. I am getting a "compile error: Expected End Sub" now on the second End If statement. I only got this after I added the above in, as it worked before that. Below is my current code.

Sub Macro1()
'
' Macro1 Macro
'

'
Sheets("Label").Select
If Sheets("Label").Range("D9") = 1 Then
ActiveSheet.PageSetup.PrintArea = "$B$19:$J$29"
End If
If Sheets("Label").Range("D9") = 2 Then
ActiveSheet.PageSetup.PrintArea = "$B$32:$J$42"
End If
Sub SetPrinter()
Dim strPrinterName As String

Select Case ThisWorkbook.Sheets("Label").Range("D9").Text
Case "1": strPrinterName = "DYMO LabelWriter 450"
Case "2": strPrinterName = "DYMO LabelWriter 450 Twin LEFT SIDE"
' Case Else: strPrinterName = "NPIE8C986 (HP Color LaserJet CP5520 Series)"
End Select

Application.SendKeys strPrinterName & "~"
Application.Dialogs(xlDialogPrinterSetup).Show
End Sub
ActiveWindow.SelectedSheets.CallSetPrinter.PrintOut Copies:=Range("F17").Value, Collate:=True, _
IgnorePrintAreas:=False
Sheets("Home Screen").Select
Range("B4").Select
Selection.ClearContents
Range("B3").Select
Selection.ClearContents
Range("B2").Select
Selection.ClearContents

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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