Set Print Area for last cell with text

Charlie987

New Member
Joined
Jul 25, 2020
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi,
I am trying to set the print area of a range that is dynamically created from another workbook so the end of the data will be unknown.
I was using
Sub PrintArea()
On Error Resume Next

Cells(1, 1).Select
With ActiveSheet.PageSetup
.PrintArea = Range("A1").End(xlDown).Select.Address
End With
End Sub

But because I have some empty rows with borders set at the bottom of the data, it seems to declare the the last row as where the row where the borders end even though they are technically empty.
Is there a way I can select the end of the data where the last cell with an acutal value rather than a border.
Thanks
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
how about
VBA Code:
Sub PrintArea()
    On Error Resume Next
    With Range("A:A")
        Set Last = .Find(What:="*", After:=.Cells(1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        .Parent.PageSetup.PrintArea = .Parent.Range("A1", Last).Address
    End With
    On Error GoTo 0
End Sub
 
Upvote 0
how about
VBA Code:
Sub PrintArea()
    On Error Resume Next
    With Range("A:A")
        Set Last = .Find(What:="*", After:=.Cells(1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        .Parent.PageSetup.PrintArea = .Parent.Range("A1", Last).Address
    End With
    On Error GoTo 0
End Sub
Two things about your Find function...

1) When your search is xlPrevious and you want to start "after" the first cell, you do not have to specify that argument (put nothing between the commas) because the default is to start from the first cell when not specified.

2) [More of a general comment than one directed at you...] On the other hand, unless one knows for a fact that there are no formulas in the column or row (depending on your SearchOrder), one should probably specify the LookIn argument (xlValues or xlFormulas) because that argument is remembered from use to use whether that use is via VBA code or the dialog box, If you specify xlValues, the last displayed data cell will be found even if there are formulas after it displaying the empty text string ("") where as xlFormulas will find the last formula cell whether the empty text string is displaying or not.
 
Upvote 0

Forum statistics

Threads
1,214,570
Messages
6,120,296
Members
448,954
Latest member
EmmeEnne1979

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