Solved

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

Posted on 2009-04-06
2
155 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
  • 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
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…

831 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