?
Solved

Setting print area using VBA

Posted on 2010-11-25
10
Medium Priority
?
518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

765 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