Print range for unknown number of single variable

Deliverable7

New Member
Joined
Apr 9, 2016
Messages
33
I found this code which works very well for my use.

As my filtered list data set includes an unknown number of "Y" i need to include a variable to relplace the fixed +5 in the code below.

Any assistance appreciated.

Code:
Sub SetPrint()
    Set rowNo = Range("A:A").Find("Y", LookIn:=xlValues, lookat:=xlWhole)
    If Not rowNo Is Nothing Then
        ActiveSheet.PageSetup.PrintArea = Range("A" & rowNo.Row & ":H" & (rowNo.Row + 5)).Address
    End If
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi there,

Here's a different way (using a filter) of doing the task:

Code:
Option Explicit
Sub Macro1()

    Dim rngFiltered As Range
    Dim lngLastRow As Long
    
    ActiveSheet.AutoFilterMode = False 'Remove all filters
    
    'Ensure there's at least one row flagged with a 'Y'
    If Evaluate("COUNTIF('" & ActiveSheet.Name & "'!A:A,""Y"")") = 0 Then
        MsgBox "There are no rows in column A flagged with a Y.", vbExclamation
        Exit Sub
    End If
    
    Application.ScreenUpdating = False
    
    lngLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    Range("$A$1:$H$" & lngLastRow).AutoFilter Field:=1, Criteria1:="Y", Operator:=xlFilterValues
    Set rngFiltered = Range("$A$1:$H$" & lngLastRow).Offset(1, 0).SpecialCells(xlCellTypeVisible)
    ActiveSheet.PageSetup.PrintArea = rngFiltered.Address
    
    Application.ScreenUpdating = True

End Sub

The code assumes Row 1 contains headings and the data starts from Row 2.

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,227
Members
453,025
Latest member
Hannah_Pham93

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