VBA Code to Print Specific Non-Continuous Pages in Excel

JoHio2577

New Member
Joined
Jan 17, 2020
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I have a worksheet in a workbook that I have created that, with manually assigned page breaks, has 276 pages in total. I am wondering if there is a VBA code that will allow me to assign what pages from the worksheet I have print out of those 276. There are times the pages I want to print will be 1, 2, 3 (continuous) and there are times when I will need to print non-continuous (1, 2, 47, 50). I have my spreadsheet set up with a column that has 1 through 276 and a second column that populates with Y if I need that page to print and N if that page should not print. For example:

1579526015578.png


In this example, pages 1, 2, 5, 8, and 9 would need to print. The Y or N in the second column is determined by formulas - they are not manually entered.

Is this possible? If it is possible, is there a way to also make it print to PDF? If PDF is not possible, that's no big deal.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Here is a UDF that you can insert in a Module and then in any cell type"=ExtractYorN(A:B)"

VBA Code:
Option Explicit
Public Function ExtractYorN(pageRange As Range)
    Dim myCell  As Range
    Dim result  As Variant
    Dim L As Long
    L = 0
    For Each myCell In pageRange
        If L = 0 And myCell.Offset(0, 1).Value = "Y" Then
            result = myCell.Value
            L = 1
        ElseIf myCell.Offset(0, 1).Value = "Y" Then
            result = result & "," & myCell.Value
        ElseIf myCell.Value = "" Then
            ExtractYorN = result
            Exit Function
        Else
        End If
    Next myCell
    ExtractYorN = result
End Function

As for the printing, I will try to research some things and get back to you.
 
Upvote 0
Pardon my ignorance, I'm new to VBA codes. What does the above code do?
 
Upvote 0
My apologies, I see it extracts the pages with a "Y" and puts them in a series to be printed. I look forward to seeing if you can find a way to then print those pages.
 
Upvote 0
so if you go to Visual Basic Editor (Alt + F11), go to the left column, right click and go to insert > module and then paste the following code in this module. Then go to Excel, and go to any cell that isnt in Column A or B and type "=ExtractYorN(A:B)"
 
Upvote 0
This definitely gives me the pages that need to be printed as I make the updates to my worksheet. Any luck on coming up with a code that would then print the pages that have been extracted?
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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