vba - Collecting an array of sheets based on conditions

SloppyJ

New Member
Joined
May 3, 2011
Messages
5
Hi ppl,

I need a code to select sheets with pages less than 15 to print out.
Also, a code to print out just the first 5 and last 5 pages if its more than 15 pages.

I've tried this so far but no success:

For X = 1 To Worksheets.Count

If Y Is Nothing Then
Set Y = Sheets(X)

Else
If ExecuteExcel4Macro("Get.Document(50)") < 10 Then
Set Y = Union(Y, Sheets(X))


End If
End If


Next X
Y.Select
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this.
Code:
Option Explicit

Sub SelectSheetsWithLessThan15Pages()

    Dim sh As Worksheet, arr() As Integer, i As Integer
    
    For Each sh In Worksheets
        If sh.PageSetup.Pages.Count <= 15 Then
            i = i + 1
            ReDim Preserve arr(1 To i)
            arr(i) = sh.Index
        End If
    Next

    Sheets(arr).Select

End Sub

Sub PrintSheetsWithMoreThan15Pages()

    Dim sh As Worksheet
    
    For Each sh In Worksheets
        With sh.PageSetup.Pages
            If .Count > 15 Then
                sh.PrintOut From:=1, To:=5
                sh.PrintOut From:=.Count - 5, To:=.Count
            End If
        End With
    Next

End Sub
 
Upvote 0
Hi people,

firstly I must say that I only begin writing macros.
I want to create a macro which will be able to check in all sheets of a same workbook if the cell A1 is <> 0. And if so then collect it and check the sheet after, etc... At the end I want to print "as an array of sheets" all "printable" sheets in a unique pdf.

I try several codes without any success.

Someone has an idea ?

Thanks a lot in advance !

Teuteu !
 
Upvote 0
Sorry I forgot, here is my code :
Dim mysht(), ib As Byte, sht As Worksheet

Sub Print1()

For Each sht In Worksheets
If sht.Range("A1").Value = "PRINT" Then
ReDim Preserve mysht(ib)
mysht(ib) = sht.Name
Debug.Print sht.Name
ib = ib + 1
End If
Next sht

Application.ActivePrinter = "FreePDF on Ne02:"

Sheets(Array(mysht)).PrintOut Copies:=1, Collate:=True

End Sub
 
Upvote 0
Hello I worked again on my macro and I got better results. I define the "printable worksheet" as variables and it works. The only problem appears at the end of the macro when it should print the worksheet array, even if the worksheet are recognized. Here is my new code (I put the blocking part in Bold). Can someone help me please?

Sub SelectSheet()

Dim NumberOfSheetName As Integer
Dim ArraySheetName(0 To 50) As String
Dim ArrayPrintSheetName(0 To 50) As String
Dim StringPrintSheetName As String

A = -1
Set SheetNameLocation = Sheets("Input").Range("F6")
NumberOfSheetName = SheetNameLocation.End(xlDown).Row - 5

For I = 0 To NumberOfSheetName - 1
ArraySheetName(I) = SheetNameLocation.Offset(I)
Sheets(ArraySheetName(I)).Select
If Sheets(ArraySheetName(I)).Range("A1") = "PRINT" Then
A = A + 1
If A = 0 Then
StringPrintSheetName = ArraySheetName(I)
Else: StringPrintSheetName = Left$(StringPrintSheetName, Len(StringPrintSheetName)) & Chr(34) & ", " & Chr(34) & ArraySheetName(I) & Mid$(StringPrintSheetName, Len(StringPrintSheetName) + 1)
End If
End If
Next I

Sheets(Array(StringPrintSheetName)).Select
Application.ActivePrinter = "FreePDF on Ne02:"
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _
"FreePDF on Ne02:", Collate:=True


End Sub

Thanks a lot in advance for any help !

Teuteu
 
Upvote 0

Forum statistics

Threads
1,224,608
Messages
6,179,872
Members
452,949
Latest member
Dupuhini

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