Last Used Cell

R

ryguy7272

This snippet of code always fails on the ActiveSheet line

Dim LastR
LastR = Cells(Rows.Count, "P").End(xlUp).Select
ActiveSheet.PageSetup.PrintArea = "$A$1:LastR"


What is wrong? I am just trying to find the last used cell in Column P, and
then set the Print Area as A1 to the last row in Column P that is used.

Argh!!
 
J

Jim Thomlinson

Try this...

Dim LastR as long

LastR = Cells(Rows.Count, "P").End(xlUp).Select
ActiveSheet.PageSetup.PrintArea = "A1:p" & LastR
 
R

ryguy7272

Well, it certainly looks like it should work, but Excel doesn't like it. It
gtes hung up in the same place. What could it be?

Thanks,
Ryan--
 
J

Jim Thomlinson

you want the row so get rid of the select...

Dim LastR as long

LastR = Cells(Rows.Count, "P").End(xlUp).Row 'Row not Select
ActiveSheet.PageSetup.PrintArea = "A1:p" & LastR
 
G

Gord Dibben

Dim LastR As Long
LastR = Cells(Rows.Count, "P").End(xlUp).Row
ActiveSheet.PageSetup.PrintArea = "$A$1:p" & LastR


Gord Dibben MS Excel MVP
 
D

Dave Peterson

I don't think Jim saw that .Select lurking at the end of that second line:

Dim LastR as long
LastR = Cells(Rows.Count, "P").End(xlUp).Row '<--changed to row
ActiveSheet.PageSetup.PrintArea = "A1:p" & LastR
 
R

ryguy7272

I knew when Jim Thomlinson got involved this issue would be resolved!! Also,
thanks for the help Gord and Dave!! This must have been such a simple thing
for three gurus, but for me it was somewhat troublesome. Oh well, I learned
a new trick today.

Thanks again guys!
Ryan---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top