VBA Printing specific worksheets

stevet1478

New Member
Joined
Sep 3, 2021
Messages
1
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,

Does anyone know a VBA code to allow you to print specific worksheets/tabs?

Also, i'm looking for a code that will allow you to only print a certain number of pages in a certain worksheet. For Example, one worksheet contains 50 pages (As sometimes due to production demand, may require up to 50 pages to be printed to be placed onto 50 pallets, whereas other production orders may only require 3 pages to be printed.

I have a front sheet where it will workout how many pages are needed, so was hoping that i could use that information to generate how many pages are required to print?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Welcome to the Board!

OK, let's say that you have a Summary sheet with two columns (A and B) that indicate what sheets you want to print, and how many pages of each you want to print.
So that data may look something like this:

Sheet NamePages
Sheet11
Sheet33
Sheet515
Sheet650

Then, we can run some VBA code to loop through those rows and print each sheet.
The code will look something like this:
VBA Code:
Sub MyPrintMacro1()

    Dim lr As Long
    Dim r As Long
    Dim sh As String
    Dim pg As Long
    
'   Activate sheet that lists pages to print
    Sheets("Summary").Activate
    
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows, start in row 2
    For r = 2 To lr
'       Get sheet name and pages to print
        sh = Cells(r, "A")
        pg = Cells(r, "B")
'       Activate sheet to print
        Sheets(sh).Activate
'       Print
        ActiveWindow.SelectedSheets.PrintOut From:=1, to:=pg, Copies:=1, Collate _
            :=True, IgnorePrintAreas:=False
    Next r

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,944
Members
449,095
Latest member
nmaske

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