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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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