macro that prints visible area after filter in exc

2019-09-08 18:38发布

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?

2条回答
Fickle 薄情
2楼-- · 2019-09-08 19:12

Finally, this code worked! :) Thanks to all!

  ActiveSheet.PageSetup.PrintArea = Range("C3:I" & lastrow).Rows.SpecialCells(xlCellTypeVisible).Address
查看更多
我想做一个坏孩纸
3楼-- · 2019-09-08 19:13

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

查看更多
登录 后发表回答