Setting the Print Area with VBA
When you set the Print Area (File>PageSetup>Sheet Tab), Excel creates a Name called Print_Area. It doesn’t appear to be any more complicated than that because you can create that Name manually and get the same effect. That leaves two ways to set the Print Area in VBA, by accessing the PageSetup object and by accessing the Names collection object.
Sub SPAPageSetup()
Sheet1.PageSetup.PrintArea = “A1:F15″
End Sub
Sub SPAName()
Sheet1.Names.Add “Print_Area”, Sheet1.Range(”A1:F15″)
End Sub
To remove the Print Area, you again have two options. Using the PageSetup object, set the PrintArea property to an empty string. For Names, use the Delete method. If the Name doesn’t exist, VBA will raise an error, so it’s best to use some error checking for that method.
Sub DPAPageSetup()
Sheet1.PageSetup.PrintArea = “” ‘empty string
End Sub
Sub DPAName()
On Error Resume Next
Sheet1.Names(”Print_Area”).Delete
On Error GoTo 0
End Sub
Stumpy:
What if the range changes depending on the amount of data transfered into the spreadsheet. I can count the # of columns and rows and set a variable represented by “Var = I9″ but I can’t find a way for the Range(”A1:Var”) to be accepted.
7 April 2005, 8:03 pmJohn:
Try Range(”A1:” & Var).
8 April 2005, 9:06 amJon Peltier:
I prefer Range(”A1″).Resize(, )
8 April 2005, 10:54 amJon Peltier:
Seems my response was edited unintentionally:
I prefer Range(”A1?).Resize(NumberOfRows, NumberOfColumns)
8 April 2005, 10:55 amRemy B:
make sure there is a printer installed when using the PrintArea procedure - otherwise it will return an error.
15 June 2005, 10:50 pmCrombes:
What If you would like to print 2 area’s
9 November 2005, 4:55 amon 2 sheets
somehow i get an error message using
Sheet1.PageSetup.PrintArea = “A1:F15,A20:F45″
first range on page1
second range on page 2
Nick:
Well the easiest way to do it is probably
sheet1.range(cells(1,1),cells(10,10)).printout
That will printout the range A1:J10
any of the numbers can be changed for variables.. so you can use that to print out multiple areas on the same sheet onto different pages
19 December 2006, 5:59 amMGray:
How do I set a specific print area using an assigned date? Example:
19 November 2007, 10:47 amRow 8 contains date headers and I want the user to be able to print a date range.
Tom Van Dam:
In order to build a range you can also do the following
Dim strRange as string
strRange = “A1:N” & Var
This way you can determine what the number of rows are and set Var to that value.
21 November 2007, 11:26 amBob:
I am trying to print one long line from a worksheet by breaking it up. A1:G1 on the first line with H1:N1 on the second line using Chr(10) or vbCrLf so that I can maintain the font size. Does anyone know what the syntax would be?
28 November 2007, 8:30 pmjohn:
what if i want to set the print area according to what I have selected
Below is my code, I have made a selection of the cells I want to set and i want to set the print area
Range(”A1:C1″).Select
16 June 2008, 9:19 pmRange(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.PageSetup.PrintArea =