Dynamic Print Area (ActiveSheet)

J

Josh O.

I am currently using the following code to set the print area in Sheet1 on my
workbook.

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Sheets("Sheet1").PageSetup.PrintArea = Sheet1.Range("PrintArea").Address
End Sub


I would like to set this up to do the same thing for the Active Sheet a user
is on. I have tried a number of ways, but I need some help.

Any ideas?
 
J

Josh O.

Josh O. said:
I am currently using the following code to set the print area in Sheet1 on my
workbook.

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Sheets("Sheet1").PageSetup.PrintArea = Sheet1.Range("PrintArea").Address
End Sub


I would like to set this up to do the same thing for the Active Sheet a user
is on. I have tried a number of ways, but I need some help.

Any ideas?

Ok. Correction. I got it to work on the active sheet. However, I want the
print range to start at A3 instead of A1.

Here is the code I am using:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Sheets(ActiveSheet.Name).PageSetup.PrintArea =
Sheets(ActiveSheet.Name).Range("PrintArea").Address
End Sub

And the Named Ranges:
LastRow = LOOKUP(2,1/('Sheet1'!$B$1:$B$5000<>""),ROW('Sheet1'!$B$1:$B$5000))
PrintArea = OFFSET('Sheet1'!$A$3,-2,0,'Workbook1.xls'!LastRow,19)
 
J

Jim Thomlinson

give this a try...

Private Sub Workbook_BeforePrint(Cancel As Boolean)
With ActiveSheet
.PageSetup.PrintArea = .Range(.Range("A3"), _
.Range("PrintArea").Cells(.Range("PrintArea").Cells.Count))
End With
End Sub
 
J

Josh O.

It gives me a "Runtime Error: 1004, Unable to Set PrintArea Property of the
PageSetup Class".
 
J

Jim Thomlinson

OOPS...

Try this

Private Sub Workbook_BeforePrint(Cancel As Boolean)
With ActiveSheet
.PageSetup.PrintArea = .Range(.Range("A3"), _
.Range("PrintArea").Cells(.Range("PrintArea").Cells.Count)).address
End With
End Sub
 
D

Dick Kusleika

It gives me a "Runtime Error: 1004, Unable to Set PrintArea Property of the
PageSetup Class".

PrintArea takes a string, so just add .Address on to the end of the range.
Or you could use Excel's built in print area. When you set the print area
in the UI, it creates a name called Print_Area. Just manipulate that named
range and you won't have to access the PageSetup object at all.

With ActiveSheet
.Names.Add "Print_Area", .Range("A3:F10")
End With

or whatever range you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top