Print only visible Filtered Rows from Excel Table

JohnZ1156

Board Regular
Joined
Apr 10, 2021
Messages
160
Office Version
  1. 2021
Platform
  1. Windows
Runing MS Excel 2021.

I have an Excel table. I have a macro that will print out the last 45 rows of the table.
I would like to be able to print out the last 45 visible rows of a "filtered" table.
Now, it only prints out the visible rows within the last 45 rows.

VBA Code:
Sub RegisterPrint()

'    Call ShowAllPrinters
    Application.ScreenUpdating = False
    Application.ActivePrinter = "HP Officejet Pro 8600 (Network) on NE02:"
    Application.Dialogs(xlDialogPrinterSetup).Show

    ActiveSheet.PageSetup.PrintArea = ""

    Dim MyLastRow As String

    MyLastRow = Range("A65536").End(xlUp).Row
    Range("A" & (MyLastRow - 44) & ":K" & MyLastRow).Select
'Put paste code down here

    ActiveSheet.PageSetup.PrintArea = Selection.Address
'    Application.Dialogs(xlDialogPrinterSetup).Show

'Worksheets("Sheet1").Activate
'ActiveSheet.PageSetup.PrintArea = _
'ActiveCell.CurrentRegion.Address

,    Call RegisterHeader
    
   ActiveWindow.SelectedSheets.PrintOut Copies:=1, Preview:=True, Collate:=True, _
        IgnorePrintAreas:=False
'    ActiveSheet.PageSetup.PrintArea = ""

    Range("B1048576").End(xlUp).Select
    ActiveCell.Offset(1, 0).Select

    Call Change_Font
'-------------------------------------
    Range("A1048576").End(xlUp).Select
    ActiveCell.Offset(-16, 0).Select
    Application.Goto ActiveCell, Scroll:=True
    ActiveCell.End(xlDown).Select
    ActiveCell.Offset(1, 1).Select
    
    Application.ActivePrinter = "HP Officejet Pro 8600 (Network) on NE02:"

    
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this:

VBA Code:
Sub RegisterPrint()
    Dim MyLastRow As Long, i As Long, n As Long, StartRow As Long

'    Call ShowAllPrinters
    Application.ScreenUpdating = False
    Application.ActivePrinter = "HP Officejet Pro 8600 (Network) on NE02:"
    Application.Dialogs(xlDialogPrinterSetup).Show

    ActiveSheet.PageSetup.PrintArea = ""
    MyLastRow = Range("A" & Rows.Count).End(xlUp).Row
    n = 0
    StartRow = 1
    For i = MyLastRow To 1 Step -1
      If Range("A" & i).EntireRow.Hidden = False Then
        n = n + 1
        If n = 45 Then
          StartRow = i
          Exit For
        End If
      End If
    Next
    
    ActiveSheet.PageSetup.PrintArea = Range("A" & StartRow & ":K" & MyLastRow).Address
'    Application.Dialogs(xlDialogPrinterSetup).Show

'Worksheets("Sheet1").Activate
'ActiveSheet.PageSetup.PrintArea = _
'ActiveCell.CurrentRegion.Address

    Call RegisterHeader
    
   ActiveWindow.SelectedSheets.PrintOut Copies:=1, Preview:=True, Collate:=True, _
        IgnorePrintAreas:=False
'    ActiveSheet.PageSetup.PrintArea = ""

    Range("B1048576").End(xlUp).Select
    ActiveCell.Offset(1, 0).Select

    Call Change_Font
'-------------------------------------
    Range("A1048576").End(xlUp).Select
    ActiveCell.Offset(-16, 0).Select
    Application.Goto ActiveCell, Scroll:=True
    ActiveCell.End(xlDown).Select
    ActiveCell.Offset(1, 1).Select
    
    Application.ActivePrinter = "HP Officejet Pro 8600 (Network) on NE02:"

    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hi DanteAmor,
Thank you for your quick response.
Your suggestion works great with one minor detail, which was absolutely NOT your fault.
I neglected to mention that my table headers start in row 17.
If my filter returns less than 45 rows of data, it includes rows above the table, (ex. rows 1-16)
Changes: I made the Start Row = 17

Last: I want to exclude column A from the printout report.
Changes: ActiveSheet.PageSetup.PrintArea = Range("B" & StartRow & ":K" & MyLastRow).Address

Works great!
Thank you.
 
Upvote 1

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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