macro that prints visible area after filter in excel

[亡魂溺海] 提交于 2019-12-25 09:05:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!