Locate last populated row/column in Excel with Win32::OLE

M

mrichardson

Hello,

I'm working on a Perl script that needs to locate the last populated
row/column in an Excel sheet. I have tried the following:

my $TransCharWSLastRow = $TransCharWS->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious, SearchOrder=>xlByRows})->{Row};
my $TransCharWSLastCol = $TransCharWS->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious, SearchOrder=>xlByColumns})->{Column};

However, both lines return "902" which doesn't seem especially useful.
I'd be willing to wade through some documentation if anyone can point
me in the right direction.

Thanks,
Matt
 
B

Brian Helterline

mrichardson said:
Hello,

I'm working on a Perl script that needs to locate the last populated
row/column in an Excel sheet. I have tried the following:

my $TransCharWSLastRow = $TransCharWS->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious, SearchOrder=>xlByRows})->{Row};
my $TransCharWSLastCol = $TransCharWS->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious, SearchOrder=>xlByColumns})->{Column};

It seems to work for me.

Other Ideas:

If you know that your UsedRange is completely populated, then you can
count the number of rows and columns to figure out the last cell.

# untested
my $used = $TransCharWS->UsedRange;
my $rows = $used->Rows->Count;
my $cols = $used->Columns->Count;
my $last = $used->Cells( $rows, $cols );

If this isn't a safe assumption (I would guess it is not), but you know
all the data is continuous, then you can select the beginning of the
UsedRange and scroll down and over to the last populated cell (like hitting
END+DOWN-ARROW, etc.

#again, untested
my $used = $TransCharWS->UsedRange->Cells( 1, 1 ); # first cell is active
$used = $used->End(xlDown); # last row
$used = $used->End(xlToRight); # last column of last row
However, both lines return "902" which doesn't seem especially useful.

Can't reproduce this
 

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

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top