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 May 15th, 2002, 07:33 AM   #1
GDawg
New Member
 
Join Date: Apr 2002
Posts: 20
Default

I have a column that contains values that have been directly keyed in and values that come from cells in other work sheets. Is there a COUNTIF (or other) formula that will count the number of cells that contain values not from other work sheets?

Thanks,

GDawg
GDawg is offline   Reply With Quote
Old May 15th, 2002, 01:55 PM   #2
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi Gdawg,

Not sure what you mean by this, but if you are talking about formulas in your data range vs. keyed in values, you would use VBA to do that.

If you are talking about comparing the data range with values on another worksheet, where you only want the number of cells that are *not* in other ranges, try soemthing like the following:

=COUNT(A1:A6)-SUMPRODUCT(COUNTIF(A1:A6,Sheet3!A1:A6))

This only searches the range on sheet3. If youneed multiple sheets, add addition SUMPRODUCT statements.

HTH,
Jay
Jay Petrulis is offline   Reply With Quote
Old May 15th, 2002, 01:59 PM   #3
Aladin Akyurek
MrExcel MVP
 
Aladin Akyurek's Avatar
 
Join Date: Feb 2002
Location: The Hague
Posts: 50,317
Default


Not sure what you mean by this, but if you are talking about formulas in your data range vs. keyed in values, you would use VBA to do that.


Exactly. Something like:

=COUNT(A1:A10)-COUNT(HasFormula(A1:A10))

where HasFormula returns an array constant of 1 and 0's.

Aladin Akyurek is offline   Reply With Quote
Old May 15th, 2002, 02:09 PM   #4
GDawg
New Member
 
Join Date: Apr 2002
Posts: 20
Default

Jay and Aladin,

Thanks for responding. I guess I wasn't very clear. What I meant was, in the column are values that have been keyed in i.e., 12345.66 and a formula bringing data from a separate worksheet, i.e., =Sheet1!N29. I want to be able to count either the cells that contain a formula or the keyed data cells.

I'm not yet up to VB so are there any other options?

Thanks,

GDawg
GDawg is offline   Reply With Quote
Old May 15th, 2002, 02:17 PM   #5
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi,

How large is your range? You could also choose
Edit>Goto>Special and select Formulas or Constants to highlight the cells

Following Aladin's suggestion, the following UDF will count the cells containing a formula in your selected range.

Code:
Function HAS_FORMULA_COUNT(DataRange As Range)
Dim UsedCell As Range, Counter As Long

For Each UsedCell In DataRange
    If UsedCell.HASFORMULA Then Counter = Counter + 1
Next UsedCell
HAS_FORMULA_COUNT = Counter

End Function
This is a Visual Basic function created for this purpose. To load it, do the following:

1. Copy the code
2. Go to Excel and type the Alt and F11 keys simultaneously (Alt-F11)
3. Type Ctrl-R
4. From the top menu Insert>Module
5. Paste the code (Ctrl-V)
6. Alt-Q to return to Excel.

You have loaded the function to your file and can call it like you would a regular Excel function.

=HAS_FORMULA_COUNT(A1:A10)

and this will return the number of cells in range A1:A10 that are formulas. Subtract this number from the total number of cells in the range to determine the constants.

HTH,
Jay

EDIT: This might not update as you would like, so add:

Application.Volatile

right after the DIM statement lines.

EDIT2: Ignore this suggestion, as I had the calculation set to manual.

[ This Message was edited by: Jay Petrulis on 2002-05-15 13:26 ]

[ This Message was edited by: Jay Petrulis on 2002-05-15 13:35 ]
Jay Petrulis is offline   Reply With Quote
Old May 15th, 2002, 02:27 PM   #6
GDawg
New Member
 
Join Date: Apr 2002
Posts: 20
Default

Thanks Jay but....

I get a message Compile Error:

Expected line number or label or statement or end of statement

Where did I go wrong?

Thanks

GDawg
GDawg is offline   Reply With Quote
Old May 15th, 2002, 02:31 PM   #7
Juan Pablo González
MrExcel MVP
 
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
Default

Try this formula:

Function CountFormula(Rng As Range) As Long
CountFormula = Rng.SpecialCells(xlCellTypeFormulas).Count
End Function

__________________
Regards,

Juan Pablo González
http://www.juanpg.com
Juan Pablo González is offline   Reply With Quote
Old May 15th, 2002, 02:41 PM   #8
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-05-15 13:31, Juan Pablo G. wrote:
Try this formula:

Function CountFormula(Rng As Range) As Long
CountFormula = Rng.SpecialCells(xlCellTypeFormulas).Count
End Function

Hi Juan,

I did something similar initially and I could not get it to work. Your function returns the total number of cells in the range, but I don't understand why. To me, it *should* work.

I messed up on Intersect(Rng, ...formulas), too.

To GDAWG: I cannot replicate your error. Please make sure that you copied the "End Function" into your code. It *should* work for you, too.

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Old May 15th, 2002, 02:42 PM   #9
Aladin Akyurek
MrExcel MVP
 
Aladin Akyurek's Avatar
 
Join Date: Feb 2002
Location: The Hague
Posts: 50,317
Default

Quote:
On 2002-05-15 13:31, Juan Pablo G. wrote:
Try this formula:

Function CountFormula(Rng As Range) As Long
CountFormula = Rng.SpecialCells(xlCellTypeFormulas).Count
End Function
Why not one that returns an array of 0's and 1's, when fed with a range? I could use such a function.

Aladin Akyurek is offline   Reply With Quote
Old May 15th, 2002, 02:51 PM   #10
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-05-15 13:42, Aladin Akyurek wrote:
Quote:
On 2002-05-15 13:31, Juan Pablo G. wrote:
Try this formula:

Function CountFormula(Rng As Range) As Long
CountFormula = Rng.SpecialCells(xlCellTypeFormulas).Count
End Function
Why not one that returns an array of 0's and 1's, when fed with a range? I could use such a function.

Hi Aladin,

Here is one that I got to work (at least for a 1-column range tested)

Code:
Function HAS_FORMULA_COUNT(DataRange As Range)
Dim UsedCell As Range, x, y
Dim MyArray

ReDim MyArray(1 To DataRange.Rows.Count, 1 To DataRange.Columns.Count)
    
With DataRange
For y = 1 To DataRange.Columns.Count
    For x = 1 To DataRange.Rows.Count
        If Cells(x + DataRange.Row - 1, y + DataRange.Column - 1).HASFORMULA Then
            MyArray(x, y) = 1
        Else
            MyArray(x, y) = 0
        End If
    Next x
Next y
End With
HAS_FORMULA_COUNT = MyArray

End Function
This requires that an equal sized worksheet range be selected and array-entered for this to work. (It has to be a worksheet array)

Let me know what needs to be done to improve/enhance/correct this.

Bye,
Jay
Jay Petrulis 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 04:12 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