Last row and column in print area

Pestomania

Active Member
Joined
May 30, 2018
Messages
332
Office Version
  1. 365
Platform
  1. Windows
Hi! I am trying to determine the "white space" between the last used row and the last row in print area. I currently have this to determine last used row and it works, but can't determine last row in print area.

Code:
Sub cover()
Dim lastrow as long
Dim top as string

Lastrow = range("A" & rows.count).End(xlUp).offset(1).row
Top =Range ("A" & lastrow).top

ActiveSheet.Shapes.AddShape(msoShapeRectangle, 0, top, x, y)

End sub

Can someone help me determine x and y to keep the shape from going outside the print area?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Below is a function to give you the bottom-right cell of the print area:
Code:
Function PrAreaBottomRight(sh1 As Worksheet) As Range
    With sh1
        Dim strPA As String: strPA = .PageSetup.PrintArea
        If strPA = "" Then 'No PrintArea Definition for this sheet - Excel will set PrintArea automatically based on the UsedRange
            Set PrAreaBottomRight = Nothing
            Exit Function
        Else
            Set PrAreaBottomRight = .Range(strPA)
        End If
    End With
    With PrAreaBottomRight
        Dim rr As Long: rr = .Rows.Count
        Dim cc As Long: cc = .Columns.Count
        PrAreaBottomRight = .Resize(1, 1).Offset(rr - 1, cc - 1)
    End With
End Function
The result of the function is a Range type object - you can then extract coordinates and size from it.
The function takes a worksheet object as argument.
If there is no PrintArea definition for this sheet - the function will return Nothing.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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