问题
I have a macro that filters the table based on column A
values. Now I want to print only the visible rows after the filter, but sadly it prints all of the rows including the top and bottom rows that were hidden during the filter.
In my sheet, there are data from Column A:I
, but the print area should only be Columns C:I
- visible rows after filter.
Here are the codes that I've tried so far that didn't work:
Code 1:
ActiveSheet.PageSetup.printarea = Range("C3:I81000").Rows.SpecialCells(xlCellTypeVisible)
Code 2:
Dim lastrow As Long
lastrow = ActiveSheet.UsedRange.Rows.Count
Range(Cells(3, 3), Cells(lastrow, 9)).Select
ActiveSheet.PageSetup.printarea = Selection.Address
Any other suggestions?
回答1:
Finally, this code worked! :) Thanks to all!
ActiveSheet.PageSetup.PrintArea = Range("C3:I" & lastrow).Rows.SpecialCells(xlCellTypeVisible).Address
回答2:
Depending on what you need the code for, I just confirmed that the following snippet works:
Public Sub Test()
Range("C3").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.PageSetup.PrintArea = Selection.Address
'ActiveSheet.PrintOut
Range("C3").Select
End Sub
Please activate the "PrintOut" row only if you want the command to be send to the printer.
For some reason. the "Selection" for rows works better for hidden/filtered cells, than other options of selection rows.
Hope that helps Best seulberg1
来源:https://stackoverflow.com/questions/38862244/macro-that-prints-visible-area-after-filter-in-excel