Solved

Setting print area using VBA

Posted on 2010-11-25
10
514 Views
Last Modified: 2012-05-10
Using VBA, how do I select my Print Area to be the first cell in use (normally A1) to the last cell with data in it ?
In addition I would like to be able to set up how many pages I want the print the result… for example 1 page wide by two pages long.

Thanks
0
Comment
Question by:GordonMasson
  • 6
  • 3
10 Comments
 
LVL 3

Expert Comment

by:RushB
ID: 34212986
Try this:

lastrow = Cells.SpecialCells(xlLastCell).Row
This selects the last used row in the worksheet

lastrow = Cells(Cells.Rows.Count, "G").End(xlUp).Row
This select the last used cell in Column G

This macro sets the print area, you must choose which 'lastrow' you
want and which column.

Sub stance()
lastrow = Cells.SpecialCells(xlLastCell).Row
lastrow = Cells(Cells.Rows.Count, "G").End(xlUp).Row
ActiveSheet.PageSetup.PrintArea = "$A$1:$G$" & lastrow
End Sub



Thanks,
RushB
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34213011
Something like:

With Activesheet.PageSetup
   .Zoom = False
   .FitToPagesWide = 1
   .FitToPagesTall = 2
   .Printarea = Activesheet.usedrange.address
End With

Open in new window

0
 

Author Comment

by:GordonMasson
ID: 34213055
Roya

That works very well but i have some cells which have a formula in them but the cell text is blank
Is there a way to make it work with the last cell with data rather than the last cell with a formula?

Thanks
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34213091
Add the function below, then use:

With Activesheet.PageSetup
   .Zoom = False
   .FitToPagesWide = 1
   .FitToPagesTall = 2
   .Printarea = Activesheet.range(activesheet.usedrange(1), lastcellinrange(activesheet.usedrange)).address
End With
Public Function LastCellInRange(rngInput As Range) As Range
   Set LastCellInRange = rngInput.Cells(1)
   On Error Resume Next
   With rngInput
      Set LastCellInRange = .Cells.Find(What:="*", After:=.Cells(1), lookat:=xlWhole, LookIn:=xlValues, _
              SearchOrder:=xlByRows, _
              SearchDirection:=xlPrevious)
   End With
End Function

Open in new window

0
 

Author Comment

by:GordonMasson
ID: 34213119
Hi Roya

Thanks for your help.
i am not sure where the Function should go?
Is it in a module or in teh Workbook.......

When i try to run the sub i get a fault on the .printarea line:
Wrong number of arguments or invalid property assignment.
Any ideas what i am doing wrong?

Thanks
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 34213126
The function goes into the same module as your current code.

I made a mistake - the printarea line should be:


   .PrintArea = ActiveSheet.Range(ActiveSheet.UsedRange.Cells(1), LastCellInRange(ActiveSheet.UsedRange)).Address
0
 

Author Comment

by:GordonMasson
ID: 34213156
Hi Roya

Thats got rid of the error but my print area is still wrong.

I do have borders round some of the empty cells. Is this the problem?
0
 

Author Comment

by:GordonMasson
ID: 34213182
Sorry Roya.... think i have that wrong... just checking what i have done
0
 

Author Comment

by:GordonMasson
ID: 34213224
Sorry Roya i got that wrong.
The problem was that i had two sheets open and the code was running on the wrong active sheet.
So.... to make sure i dont have this problem again is ther a mod you can do that ensures that the code runs in the current workbook?

Thanks
0
 

Author Closing Comment

by:GordonMasson
ID: 34215985
Thanks Roya

SOrted now, used ThisWorkbook.Activate to get me onto the right page.

Thanks for your help.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question