Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setup print area using vba

Status
Not open for further replies.

vba317

Programmer
Mar 5, 2009
708
US
I am trying to setup a printarea using vba with no luck. I would appreciate so help if possible. I have tried a couple of different combinations with no success. I keep on getting a compile error on the red line.

Code:
Sub PrintAreaSetup()

Dim PR1 As String
Dim PR2 As String
Dim LR As String

LR = ActiveSheet.UsedRange.Rows.Count

PR1 = "$A$1"
PR2 = "$F" & "$" & LR
                                    
Range(PR1, PR2).Select

[red] ActiveSheet.PageSetup.PrintArea = ("PR1:PR2")[/red]

With ActiveSheet.PageSetup
.LeftFooter = "&F"
.CenterFooter = "&A"
.RightFooter = "&"""

End With
'
End Sub
 


Hi,

your code will produce unexpected results IF the UsedRange does not include the first row...
Code:
with ActiveSheet.UsedRange
  LR = .Rows.Count + .Row - 1
End with

PR1 = "$A$1"
PR2 = "$F" & "$" & LR
                                    
ActiveSheet.PageSetup.PrintArea = (PR1 & ":" & PR2)

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
what about this ?
Code:
Sub PrintAreaSetup()
With ActiveSheet.PageSetup
  .PrintArea = Intersect(Range("$A:$F"),ActiveSheet.UsedRange).Address
  .LeftFooter = "&F"
  .CenterFooter = "&A"
  .RightFooter = "&"""
End With
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top