I’m trying to find my last row in a PivotTable which can be filtered and hence changes in row size.
The below code does not find the last row.
However, the code works if I manually set the lastRow = 9.
ws.PageSetup.PrintArea = ws.Range("A1:AN9").Address
Hence I assume that this line is not working properly:
lastRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Sub SetPrintArea()
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Sheets("RELATION LEVEL")
' find the last row with formatting, to be included in print range
lastRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
ws.PageSetup.PrintArea = ws.Range("A1:AN" & lastRow).Address
End Sub
What am I doing wrong to set up the printing area?
FYI: I tried using the code as described here: Set printing area In Excel 2013 using macro
I expect to get the last row and set up the printing area from A1 to AN X.
The print area is not set up correctly however.
Malganas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.