Select/Print only sheets meeting conditions

FredM400

New Member
Joined
Jul 22, 2008
Messages
26
Hi

I have a workbookk with 6 to 40 worksheets in it.

I want to select all sheets that meet a certain condition and print them as a group. (Unknown number of sheets)
I can find examples where the sequance of sheets in the file is known, but not if they are random. The sheets can be in a random order. There could be from 1 to 20 sheets selected to the group.

My code snippet is below. It only is selecting the last worksheet that meets the condition, the grouping of the sheets is not working.

****************************************

For Each vPrintSht In ActiveWorkbook.Worksheets

If Right(vPrintSht.Name, 6) = Left(vGenAlias, 6) Then
Sheets(vPrintSht.Name).Select True
vPrintInclude = vPrintSht.Name
Else
Sheets(vPrintSht.Name).Select False
End If

Next

Sheets(vPrintInclude).Activate
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False


*********************************************

Any thoughts?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
A bit topsy turvy. Maybe

Code:
For Each vPrintSht In ActiveWorkbook.Worksheets
    If Right(vPrintSht.Name, 6) = Left(vGenAlias, 6) Then Sheets(vPrintSht.Name).Select Replace:=False
Next vprintsheet
ActiveWindow.SelectedSheets.PrintOut
 
Upvote 0
Much closer than I was able to get!

With the "Replace:=False" after the selection, the sheet is added to whatever the current set of selected sheets.

Sheets already selected at the start remain selected.

At the start of the selection test, what is a good way to have a condition of no sheets selected?
I want only the sheets selected by the condition test to be selected to print; not the sheets selected by the condition test plus whatever sheets happen to have been selected before the test was applied?

(I can think of a few ways, but am soliciting example of better ways)
 
Upvote 0
Try like this

Code:
Dim Sel As Boolean
For Each vPrintSht In ActiveWorkbook.Worksheets
    If Right(vPrintSht.Name, 6) = Left(vGenAlias, 6) Then
        If Sel Then
            Sheets(vPrintSht.Name).Select Replace:=False
        Else
            Sheets(vPrintSht.Name).Select
            Sel = True
        End If
    End If
Next vprintsheet
ActiveWindow.SelectedSheets.PrintOut
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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