Last row and column in print area

Pestomania

Active Member
Joined
May 30, 2018
Messages
292
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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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,214,859
Messages
6,121,963
Members
449,059
Latest member
oculus

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