Set Print Area for last cell with text

Charlie987

New Member
Joined
Jul 25, 2020
Messages
22
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
 

Some videos you may like

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
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
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,853
Office Version
  1. 2010
Platform
  1. Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,126,998
Messages
5,622,097
Members
415,876
Latest member
csibonga2k17

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
Top