MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 22nd, 2002, 04:16 AM   #1
Ian Mac
MrExcel MVP
 
Join Date: Feb 2002
Location: Newcastle, UK
Posts: 1,174
Default

I have the following formula is return the number of unique entries in a list:

=IF(LEN(Listing!I2:I845),SUMPRODUCT(1/COUNTIF(Listing!I2:I845,
)))

Which works fine:

the problem being the list varies in length, so I've added dynamic ranges to the formula:

=IF(LEN(Address),SUMPRODUCT(1/COUNTIF(Address,Address)))

Where 'Address' is =OFFSET(Listing!$I$2,0,0,COUNTA(Listing!$I:$I),1).

but now the formula doesn't work, BUT if I change the formula to:

=IF(LEN(Address),SUMPRODUCT(1/COUNTIF(Address,Listing!I2:I845)))

where the last arg in the COUNTA isn't the named range it works.

Any idea??

__________________
"Have a good time......all the time"
Ian Mac
Ian Mac is offline   Reply With Quote
Old Mar 22nd, 2002, 04:32 AM   #2
Aladin Akyurek
MrExcel MVP
 
Aladin Akyurek's Avatar
 
Join Date: Feb 2002
Location: The Hague
Posts: 50,317
Default

Quote:
On 2002-03-22 03:16, Ian Mac wrote:
I have the following formula is return the number of unique entries in a list:

=IF(LEN(Listing!I2:I845),SUMPRODUCT(1/COUNTIF(Listing!I2:I845,
)))

Which works fine:

the problem being the list varies in length, so I've added dynamic ranges to the formula:

=IF(LEN(Address),SUMPRODUCT(1/COUNTIF(Address,Address)))

Where 'Address' is =OFFSET(Listing!$I$2,0,0,COUNTA(Listing!$I:$I),1).

but now the formula doesn't work, BUT if I change the formula to:

=IF(LEN(Address),SUMPRODUCT(1/COUNTIF(Address,Listing!I2:I845)))

where the last arg in the COUNTA isn't the named range it works.

Any idea??
Hi Ian,

I just checked and as I expected, no probs. Although, like you I used 'address' as name, I think we should avoid doing that, that being a function name in Excel's name space. Again, I didn't encountered any trouble.

BTW, what formula did you use to define 'address' as dynamic range name?
Aladin Akyurek is offline   Reply With Quote
Old Mar 22nd, 2002, 07:51 AM   #3
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

I'd wouldn't recommend that you use a range as the 2nd argument (criteria) of the COUNTIF worksheet function...

COUNTIF(Address,Address)

The Help topic for COUNTIF specifies that the criteria should be "a number, expression, or text that defines which cells will be counted". And, provides no examples using a cell range or named range.

Upon further reflection... and a much needed 1st cup of coffee of retract my previously stated concerns!

[ This Message was edited by: Mark W. on 2002-03-22 07:22 ]
Mark W. is offline   Reply With Quote
Old Mar 22nd, 2002, 08:35 AM   #4
Ian Mac
MrExcel MVP
 
Join Date: Feb 2002
Location: Newcastle, UK
Posts: 1,174
Default

Aladin, I was a bit hasty with my question and didn't fully explain the problem, the OFFSET(.........) part of my orignal question is the dynamic range.

I agree with the NON use of Address, what I should of put is 'my named range is AddressOfCustomer' (me being hasty)

I have since looked at it at realised that I had forgotten to include -1 after the COUNTA():

=OFFSET(Listing!$I$2,0,0,COUNTA(Listing!$I:$I)-1,1)

Because I needed the range to start in row 2 as the first row is field headings (and of they're going to be unique ), and it was taking the range 1 row beyond my needs, Perhaps now I should mention that the error was a #DIV/0! (you doubtless would have figured it out had I told you (hasty hasty hasty)).

Also worth a mention is that I'm using the dreaded named range in a SUMPRODUCT(), something you've advised me against doing.

BUT! now this does confuse me! using my orignal formula:

=IF(LEN(Listing!I2:I845),SUMPRODUCT(1/COUNTIF(Listing!I2:I845,Listing!I2:I845)))

It works, I'd expect it to. It also works with the two named ranges and the manual range:

=IF(LEN(AddressOfCustomer),SUMPRODUCT(1/COUNTIF(AddressOfCustomer,Listing!I2:I845)))

I always thought that the ranges had to be indentical to work?? The above formula contains the error in the OFFSET() so the ranges in this case are :

=IF(LEN(Listing!I2:I846),SUMPRODUCT(1/COUNTIF(Listing!I2:I846,Listing!I2:I845)))

Any thoughts??

Mark, I agree with your comments regarding ranges but the formula to return unique number of values/entries requires the use of COUNTA(Range,Range) and I don't see that it makes a difference if I use a named range, the range or indeed just the OFFSET() function for both arguements.
__________________
"Have a good time......all the time"
Ian Mac
Ian Mac is offline   Reply With Quote
Old Mar 22nd, 2002, 08:43 AM   #5
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

Quote:
I have since looked at it at realised that I had forgotten to include -1 after the COUNTA():

=OFFSET(Listing!$I$2,0,0,COUNTA(Listing!$I:$I)-1,1)
Ian, I've found that it's quite useful to check the correctness of a named range by clicking in the "Refers to:" field after the defined name has been added. A marquee (sometimes referred to as "marching ants") should be shown around the desired range. This might have alerted you to the missing -1.

[ This Message was edited by: Mark W. on 2002-03-22 08:11 ]
Mark W. is offline   Reply With Quote
Old Mar 22nd, 2002, 11:44 AM   #6
Aladin Akyurek
MrExcel MVP
 
Aladin Akyurek's Avatar
 
Join Date: Feb 2002
Location: The Hague
Posts: 50,317
Default

On 2002-03-22 07:35, Ian Mac wrote:
Aladin, I was a bit hasty with my question and didn't fully explain the problem, the OFFSET(.........) part of my orignal question is the dynamic range.

I agree with the NON use of Address, what I should of put is 'my named range is AddressOfCustomer' (me being hasty)

I have since looked at it at realised that I had forgotten to include -1 after the COUNTA():

=OFFSET(Listing!$I$2,0,0,COUNTA(Listing!$I:$I)-1,1)

Because I needed the range to start in row 2 as the first row is field headings (and of they're going to be unique ), and it was taking the range 1 row beyond my needs, Perhaps now I should mention that the error was a #DIV/0! (you doubtless would have figured it out had I told you (hasty hasty hasty)).


Right. Hope you don't have in-between blanks in the dynamic range that you compute. COUNTA is quite useless in such situations. Since the range is supposed to be of alphanumeric type, use either

=OFFSET(Listing!$I$2,0,0,MATCH(REPT("z",255),Listing!$I:$I)-1,1)

if you don't expect any formula-generated blank cell as the last cell (Mark & Chris, are you taking notice?)

or

=OFFSET(Listing!$I$2,0,0,MATCH(CODE(""""),Listing!$I:$I,-1)-1,1)

if the last cell can house a formula generated blank.

Also worth a mention is that I'm using the dreaded named range in a SUMPRODUCT(), something you've advised me against doing.

I don't recall having committed such a sin. Nothing is wrong with what follows, where Range1 and Range2 are named ranges, even when dynamic:

=SUMPRODUCT((Range1=1)*(Range2="a"))

as long as Size(Range1)=Size(Range2).

BUT! now this does confuse me! using my orignal formula:

=IF(LEN(Listing!I2:I845),SUMPRODUCT(1/COUNTIF(Listing!I2:I845,Listing!I2:I845)))

It works, I'd expect it to. It also works with the two named ranges and the manual range:

=IF(LEN(AddressOfCustomer),SUMPRODUCT(1/COUNTIF(AddressOfCustomer,Listing!I2:I845)))

I always thought that the ranges had to be indentical to work?? The above formula contains the error in the OFFSET() so the ranges in this case are :

=IF(LEN(Listing!I2:I846),SUMPRODUCT(1/COUNTIF(Listing!I2:I846,Listing!I2:I845)))

Any thoughts??


Your observations are right. However, the unequal sized ranges issue does not involve here the SUMPRODUCT function itself, but the COUNTIF function. It's quite right for the COUNTIF to process unequal sized ranges:

Consider

{1;2;3} in E2:E4 and

{1;2;"w";"s"} in F2:F5.

=SUMPRODUCT(COUNTIF(E2:E4,F2:F5))

[ or {=SUM(COUNTIF(E2:E4,F2:F5))} ]

should simply produce 2. It's in the 'nature' of COUNTIF (like MATCH) that it can work with such ranges. SUMPRODUCT pushes COUNTIF here to accept a multicell range as condition and produce a constant array to feed to SUMPRODUCT.

A more important issue is that the formula

=IF(LEN(Range),SUMPRODUCT(1/COUNTIF(Range,Range)))

will not work as intended (something that surfaced up this evening). The idea behind is that you don't get a #DIV/0! when Range has (formula-generated) blanks. I feel guilty about it somehow. The formula must be an array-formula, as I posted it at the newsgroup worksheet.functions a while ago:

=SUM(IF(LEN(Range),1/COUNTIF(Range,Range)))

Please switch to this array-formula.

By the way, the inventor of the original array-formula

=SUM(1/COUNTIF(Range,Range))

is David Hager. The LEN bit is something that I started using at the old board, instead of ISBLANK, in order to circumvent the trouble with formula-generated blanks.

Aladin


[ This Message was edited by: Aladin Akyurek on 2002-03-22 10:49 ]
Aladin Akyurek is offline   Reply With Quote
Old Mar 23rd, 2002, 08:15 PM   #7
Chris Davison
MrExcel MVP
 
Join Date: Feb 2002
Location: Millbank, London, UK
Posts: 1,790
Default

Always
Chris Davison is offline   Reply With Quote
Old Mar 24th, 2002, 12:06 AM   #8
trillicomm
Board Regular
 
Join Date: Feb 2002
Posts: 95
Default

Quote:
On 2002-03-22 07:43, Mark W. wrote:
Quote:
I have since looked at it at realised that I had forgotten to include -1 after the COUNTA():

=OFFSET(Listing!$I$2,0,0,COUNTA(Listing!$I:$I)-1,1)
Ian, I've found that it's quite useful to check the correctness of a named range by clicking in the "Refers to:" field after the defined name has been added. A marquee (sometimes referred to as "marching ants") should be shown around the desired range. This might have alerted you to the missing -1.

[ This Message was edited by: Mark W. on 2002-03-22 08:11 ]
Hello Mark,

This is beside the Excel issue. Do you do private consulting in Austin, TX? I just have to ask you that because I live in Austin, too. Please let me know.

T. Le
trillicomm is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 08:35 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes