Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-04-06
2
Medium Priority
?
171 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

609 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