I've added print functionality to the code, and thrown in a slight twist. Given the problems we had getting the printer active, I've utitlised the existing code in Module 1 to search for and assign the printer with "Label2" somewhere in the name.
As a backup, if it can't find the correct printer, it will warn you of this and give the option to abort before printing in the wrong place.
Add this code after the existing code in module 1 and assign it to the print button. You don't need your userform now, everything is done by this code.
Sub Print_Labels()
Dim c As Range, Printer As Variant, Printers As Variant, cPrinter As String, AbortPrint
If MsgBox("Are you sure you want to print " & [Table_Query_from_Visual654[base]].Count & " Labels for " & [Table_Query_from_Visual654[base]].Cells(1, 1) & " ?", vbQuestion + vbYesNo) = vbYes Then
Application.ScreenUpdating = False
cPrinter = ActivePrinter
Printers = GetPrintersAndPorts
For Each Printer In Printers
If InStr(Printer, "MSP-Label2") Then
Application.ActivePrinter = Printer: Exit For
End If
Next Printer
If Not InStr(ActivePrinter, "MSP-Label2") Then
If MsgBox("Specified Label printer not found!" & vbCrLf & "Do you want to print to " & ActivePrinter & " ?", vbExclamation + vbYesNo) <> vbYes Then
GoTo AbortPrint
End If
End If
For Each c In [Table_Query_from_Visual654[base]]
With Sheets("Label")
.[J4] = c.Value
.[J22] = Intersect(c.EntireRow, [Table_Query_from_Visual654[rsc]])
.[AK4] = Intersect(c.EntireRow, [Table_Query_from_Visual654[subNo]])
If Intersect(c.EntireRow, [Table_Query_from_Visual654[subNo]]) = 0 Then
.[J9] = Application.VLookup(Intersect(c.EntireRow, [Table_Query_from_Visual654[subNo]]) & "", [Table_Query1[[Sub_ID]:[Leg_Drawing_No]]], 2, 0)
Else
.[J9] = Application.VLookup(Intersect(c.EntireRow, [Table_Query_from_Visual654[subNo]]) & "", [Table_Query1[[Sub_ID]:[Leg_Drawing_No]]], 4, 0)
End If
.[J25] = IIf(Intersect(c.Offset(1).EntireRow, [Table_Query_from_Visual654[subNo]].EntireColumn) = Intersect(c.EntireRow, [Table_Query_from_Visual654[subNo]]), Intersect(c.Offset(1).EntireRow, [Table_Query_from_Visual654[rsc]] _
.EntireColumn), "")
End With
ThisWorkbook.Worksheets("Label").PrintOut
Next
AbortPrint:
Application.ActivePrinter = cPrinter
Application.ScreenUpdating = True
End If
End Sub
Hope this helps.
Bookmarks