Solved

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

Posted on 2009-04-06
2
157 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 250 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

696 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