Search Contextures Sites
Add an "X" to items in a worksheet list, and the following code will print each marked item in an Excel order form
Thanks to Dave Peterson, who wrote this technique.
On a worksheet, create a list or orders, and leave the first column blank. When you are ready to print specific orders, put an X in column A, for those orders.
Each order that is marked will be printed, using the template on the Order Form sheet.
Values from the worksheet list, starting in Column B and moving right, are copied to the order form, into the addresses you've specified in this line:myAddr = Array("E5", "E6", "B10", _ "E25", "B16", "C16", "D16")
Change those references to match your order form.
Near the start of the code, change the worksheet names to match your worksheet names:Set FormWks = Sheets("Order Form")
Set DataWks = Sheets("Orders")
The "X" marks are cleared at the end of the code, to prepare for the next print session, and a message is displayed, indicating how many orders were printed.
After testing, you can change Preview to False in this line:FormWks.PrintOut Preview:=True
and the forms will print, instead of showing a preview.
Note: When you copy the code to your workbook, be sure to include the following line at the top of the code module:Option Base 0
If you're new to macros, you may want to read David McRitchie's intro at: http://www.mvps.org/dmcritchie/excel/getstarted.htmOption Base 0 Sub PrintUsingDatabase() Dim FormWks As Worksheet Dim DataWks As Worksheet Dim myRng As Range Dim myCell As Range Dim iCtr As Long Dim myAddr As Variant Dim lOrders As Long Set FormWks = Sheets("Order Form") Set DataWks = Sheets("Orders") myAddr = Array("E5", "E6", "B10", _ "E25", "B16", "C16", "D16") With DataWks 'first row of data to ' last row of data in column B Set myRng = .Range("B3", _ .Cells(.Rows.Count, "B").End(xlUp)) End With For Each myCell In myRng.Cells With myCell If IsEmpty(.Offset(0, -1)) Then 'if the row not marked, do nothing Else 'clear mark for the next time .Offset(0, -1).ClearContents For iCtr = LBound(myAddr) _ To UBound(myAddr) FormWks.Range(myAddr(iCtr)).Value _ = myCell.Offset(0, iCtr).Value Next iCtr Application.Calculate 'just in case 'after testing, change Preview ' to False to Print FormWks.PrintOut Preview:=True lOrders = lOrders + 1 End If End With Next myCell MsgBox lOrders & " orders were printed." End Sub
Click here to download the zipped sample Excel order forms workbook:
Contextures Inc., Copyright ©2016
All rights reserved.
Last updated: December 22, 2015 11:14 AM