| View previous topic :: View next topic |
| Author |
Message |
Sudesh Guest
|
Posted: Thu Jan 06, 2005 3:14 pm Post subject: Excel Automation (count used rows for each used column) |
|
|
Hello:
How to count used rows of each column in a worksheet?
I am able to count total used columns and rows using "CurrentRegion".
Thanks,
Sudesh
|
|
| Back to top |
|
 |
BigStew Guest
|
Posted: Thu Jan 06, 2005 4:24 pm Post subject: Re: Excel Automation (count used rows for each used column) |
|
|
Use the Used_Range property of a WorkSheet;
This routine counts the number of non-blank cells in the used range.
Use this as an example.
Stewart
RangePtr Range = WS1->get_UsedRange();
RangePtr cols = Range->get_Columns();
RangePtr rows = Range->get_Rows();
int cellCount = 0;
for (int i = 1; i < rows->Count+1; ++i)
{
for (int j = 1; j < cols->Count+1; ++j)
{
AnsiString a = TVariant(Range->get_Item(TVariant(i),TVariant(j)));
if (!a.IsEmpty()) ++cellCount;
}
}
ShowMessage(cellCount);
"Sudesh" <sudeshc (AT) vfemail (DOT) net> wrote
| Quote: | Hello:
How to count used rows of each column in a worksheet?
I am able to count total used columns and rows using "CurrentRegion".
Thanks,
Sudesh
|
|
|
| Back to top |
|
 |
Sudesh Guest
|
Posted: Thu Jan 06, 2005 5:23 pm Post subject: Re: Excel Automation (count used rows for each used column) |
|
|
Hi:
So there is no such direct property to give individual row count of a
column?
Counting Rows like this will waste a lot of time.
Consider a case where the largest row count is say 64K but other columns are
with very few rows.
So for each column, iterating through 64K row cells will be too much time
consuming!! Automation speed itself is very slow.
Any other way? (I am using BCB3.0)
Thanks,
Sudesh
|
|
| Back to top |
|
 |
Mike King Guest
|
Posted: Thu Jan 06, 2005 6:33 pm Post subject: Re: Excel Automation (count used rows for each used column) |
|
|
"Sudesh" <sudeshc (AT) vfemail (DOT) net> wrote
| Quote: | Hi:
So there is no such direct property to give individual row count of a
column?
Counting Rows like this will waste a lot of time.
Consider a case where the largest row count is say 64K but other columns
are with very few rows.
So for each column, iterating through 64K row cells will be too much time
consuming!! Automation speed itself is very slow.
Any other way? (I am using BCB3.0)
Thanks,
Sudesh
|
Yes, automation can be slow so to compensate for that try getting Excel to
do more of the work for you. Excel is really good at number crunching so
let it do and you can just get the results. So just do something like the
following:
65536 - WorksheetFunction.CountBlank(column)
|
|
| Back to top |
|
 |
Sudesh Guest
|
Posted: Thu Jan 06, 2005 7:04 pm Post subject: Re: Excel Automation (count used rows for each used column) |
|
|
Hi Mike:
Whats the correct syntax fot this in BCB?
I get error "method 'CountBlank' not supported..." with the following.
int x = XLWorkSheet.OleFunction("CountBlank");
Thanks,
Sudesh
"Mike King" <emailMK (AT) excite (DOT) com> wrote
| Quote: | So just do something like the following:
65536 - WorksheetFunction.CountBlank(column)
|
|
|
| Back to top |
|
 |
Sudesh Guest
|
Posted: Thu Jan 06, 2005 7:10 pm Post subject: Re: Excel Automation (count used rows for each used column) |
|
|
Sorry i mean
int x = XLWorkSheet.OleFunction("CountBlank",col);
|
|
| Back to top |
|
 |
Mike King Guest
|
Posted: Thu Jan 06, 2005 7:19 pm Post subject: Re: Excel Automation (count used rows for each used column) |
|
|
"Sudesh" <sudeshc (AT) vfemail (DOT) net> wrote
| Quote: | Sorry i mean
int x = XLWorkSheet.OleFunction("CountBlank",col);
|
I use BCB almost everyday and I use Excel quite often but when I try to get
them to talk to one another I just start cry I cheat and write some VBA
code and let it do most of the work. So in other words, I have no clue how
to do it in BCB.
|
|
| Back to top |
|
 |
Sudesh Guest
|
Posted: Fri Jan 07, 2005 2:10 am Post subject: Re: Excel Automation (count used rows for each used column) |
|
|
No cry for this anymore:-)
int x =
XL.OlePropertyGet("WorksheetFunction").OleFunction("CountBlank",XLRange) ;
Thanks Mika for the clue.
Sudesh
| Quote: | I use BCB almost everyday and I use Excel quite often but when I try to
get them to talk to one another I just start cry I cheat and write
some VBA code and let it do most of the work. So in other words, I have
no clue how to do it in BCB.
|
|
|
| Back to top |
|
 |
Mike King Guest
|
Posted: Fri Jan 07, 2005 2:45 pm Post subject: Re: Excel Automation (count used rows for each used column) |
|
|
Do you know of a good article/tutorial of how to use IDispatch like you just
did?
"Sudesh" <sudeshc (AT) vfemail (DOT) net> wrote
| Quote: | No cry for this anymore:-)
int x =
XL.OlePropertyGet("WorksheetFunction").OleFunction("CountBlank",XLRange) ;
Thanks Mika for the clue.
Sudesh
I use BCB almost everyday and I use Excel quite often but when I try to
get them to talk to one another I just start cry I cheat and write
some VBA code and let it do most of the work. So in other words, I have
no clue how to do it in BCB.
|
|
|
| Back to top |
|
 |
Sudesh Guest
|
Posted: Fri Jan 07, 2005 5:11 pm Post subject: Re: Excel Automation (count used rows for each used column) |
|
|
Sorry, my knowledge of COM is also very limited. I hope some expert will
post to help.
Sudesh
"Mike King" <emailMK (AT) excite (DOT) com> wrote
| Quote: | Do you know of a good article/tutorial of how to use IDispatch like you
just did?
|
|
|
| Back to top |
|
 |
|