?
Solved

VBA Code to shift print area based on last row of data

Posted on 2009-04-06
2
Medium Priority
?
165 Views
Last Modified: 2013-11-25
I have a spreadsheet that currently has the print area set as c3:0133.  I would like to have the print area reset (I guess with a Before_Print Command)  based on the last row of data at the time of printing.  This spreadsheet is a case expense log where users log their expenses in rows...but the amount of entries/rows will vary every time.  For example, if a user enters rows of data down to row 50 then before they print the print area would change to c3:c50
0
Comment
Question by:RRUTLAND5
[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
  • 2
2 Comments
 
LVL 50

Accepted Solution

by:
Dave Brett earned 1000 total points
ID: 24084058
something like this in the ThisWorkbook module (to print c3:o50 in you example)
Cheers
Dave

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Dim ws As Worksheet, rng1
    Set ws = Sheets(1)
    If ws.Name <> ActiveSheet.Name Then Exit Sub
    Set rng1 = ws.Range(ws.[c3], ws.Cells(Rows.Count, "o").End(xlUp))
    ws.PageSetup.PrintArea = rng1.Address
End Sub

Open in new window

0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 24084061
And I should have noted that the code is set to run on the first sheet, ie
Set ws = Sheets(1)

Cheers
Dave
0

Featured Post

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!

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

764 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