Solved

Setting print area using VBA

Posted on 2010-11-25
10
512 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 85

Expert Comment

by:Rory Archibald
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry Roya.... think i have that wrong... just checking what i have done
0
 

Author Comment

by:GordonMasson
Comment Utility
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
Comment Utility
Thanks Roya

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

Thanks for your help.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now