Set Print Area using VBA

sjha

Active Member
Joined
Feb 15, 2007
Messages
355
This is what I have so far and this works fine. I have pre-defined the range but is there a way to print only up to the last row where there is data otherwise no need to print white spaces. Thanks for your time and effort. My code is below:

Sub Set_Print_Area()

Application.ScreenUpdating = False
ActiveWindow.SmallScroll Down:=-24
Range("A1:Q200").Select
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = "$A$1:$Q$200" With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.5)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = True
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLegal
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintErrors = xlPrintErrorsDisplayed
End With
ActiveWindow.SelectedSheets.PrintPreview
ActiveWindow.SmallScroll Down:=-15
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Untested

Code:
ActiveSheet.PageSetup.PrintArea = ActiveSheet.UsedRange.Address
 
Upvote 0
That did not change to what I had. Also, what do I need to add/change so that there is no Page Breaks. In my current situation, in first page, it has just 6 lines of data then next page has about half and then full pages of data....then next 9 pages with empty data. What should I do here? Many Thanks!!
 
Upvote 0
Try this on a copy of your sheet. Select the last cell with data then run this

Code:
Sub makelastcell()
Dim x As Integer
Dim str As String
Dim xlong As Long, clong As Long, rlong As Long
On Error GoTo 0
str = ActiveCell.Address
Range(ActiveCell.Row + 1 & ":" & Cells.Rows.Count).Delete
xlong = ActiveSheet.UsedRange.Rows.Count
xlong = ActiveSheet.UsedRange.Columns.Count
Range(Cells(1, ActiveCell.Column + 1), Cells(Cells.Rows.Count, Cells.Columns.Count)).Delete
Beep
xlong = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Columns.Count
rlong = Cells.SpecialCells(xlLastCell).Row
clong = Cells.SpecialCells(xlLastCell).Column
If rlong <= ActiveCell.Row And clong <= ActiveCell.Column Then Exit Sub
ActiveWorkbook.Save
xlong = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Columns.Count
rlong = Cells.SpecialCells(xlLastCell).Row
clong = Cells.SpecialCells(xlLastCell).Column
If rlong <= ActiveCell.Row And clong <= ActiveCell.Column Then Exit Sub
MsgBox "Sorry, Have failed to make " & str & " your last cell"
End Sub
 
Upvote 0
hi
is it possible to create a code that allows to copy the print area and the print title to a new workbook?i've one code that copies the print area to a new workbook, for each worksheet of the principal workbook but can't insert in there the print title.
i would appreciate the help

cheers
 
Upvote 0
Tutz, what are you really trying(Needing) to do!! Do you have formulas on your sheet that return "" so Excel is printing more than you need??

lenze
 
Upvote 0
Let's imagine that I've a workbook with 2 worksheets - sheet1 and sheet2. I want to create a new workbook for each worksheet(sheet1 and sheet2) but only with the print area and the print title included in each one. It's like copy the sheet to a new workbook but only including the print area and the print title. all the rest is out.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

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