Excel Automation: how to find the last row numbers in an Excel Sheet?

Hi Experts,

I want to open an Excel file in VC++ and append new data to an existing worksheet. I need to find the last row number in the used range. It seems to be an easy problem, but I keep gettng a count of 25384. What's wrong with this code?

LPDISPATCH lpDisp;
Range range;
lpDisp = sheet.GetUsedRange();
range.AttachDispatch(lpDisp);
range.Select();
range.GetRows();
long lTotalRows = range.GetCount();

Thanks,
Er
jper_hmAsked:
Who is Participating?
 
NawalKishore1976Connect With a Mentor Commented:
Go to excel and go to vba.
Select Object Browser and See the value.

Const xlLastCell = 11
0
 
NawalKishore1976Commented:
There is something Called The Special Cells:

Range("A1").Select
    ActiveCell.SpecialCells(xlLastCell).Select


This can be used to go to the last cell    
0
 
jper_hmAuthor Commented:
Thanks. How to do this in VC++?  What's the constant value for xlLastCell?
0
 
jper_hmAuthor Commented:
Thank you. I still cannot find the equivalent SpecialCells function in excel9.h. Is it this one:

LPDISPATCH Range::SpecialCells(long Type, const VARIANT& Value)




0
 
jper_hmAuthor Commented:
Yes, I got it. It's
range.SpecialCells((long)11,covOptional);

Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.