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 1st, 2002, 05:04 PM   #1
nsmythe
New Member
 
Join Date: Apr 2002
Location: Amgen
Posts: 3
Default

I am very new to creating functions in excel, and macros for that matter, and was very intersted in creating a formula or function that counted the number of cells in a range that were of a certain color. I was able to copy and paste one of the samples I found on this website, but the problem is that the value that is returned does not get updated automatically when a cell within the range is changed to the color that is counted. How can I make the function remain active all the time, and not only when I am editing the formula and hit the enter key?

I also am wondering if there would be a way for me to have every cell I color in the future dark blue to have the font color change to yellow automatically.

Being this is my first post I have to say that this board is amazing!!

The function I am using is:

Public Function CountBlue(Inrange As Range)
CountBlue = 0
For Each cell In Inrange
If cell.Interior.ColorIndex = 11 Then
CountBlue = CountBlue + 1
End If
Next
End Function

Thanks
nsmythe is offline   Reply With Quote
Old May 1st, 2002, 05:08 PM   #2
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

How are you passing the range Inrange?
In what way?
Tom
Tom Schreiner is offline   Reply With Quote
Old May 1st, 2002, 05:10 PM   #3
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

This is a volatile (technically speaking) function. Change your code to:

Code:
Public Function CountBlue(Inrange As Range)
application.volatile
CountBlue = 0 
For Each cell In Inrange 
If cell.Interior.ColorIndex = 11 Then 
CountBlue = CountBlue + 1 
End If 
Next 
End Function
This will allow you to recalc. "CountBlue", however, changing the color of a cell does not inspire Excel to recalculate. You'll either need to manually recalc. (Shift-f9), change a cell's value or use a worksheet_selectionchange event. Right-click on the sheet in question, click view code and paste the following:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Calculate
End Sub
This should inspire Excel. Hope this helps.

_________________
Cheers, NateO

[ This Message was edited by: NateO on 2002-05-01 16:17 ]
NateO is offline   Reply With Quote
Old May 1st, 2002, 05:16 PM   #4
nsmythe
New Member
 
Join Date: Apr 2002
Location: Amgen
Posts: 3
Default

That code worked great. Thanks a lot. What exactly would a worksheet_selectionchange event do and where would I find out. Also do you have an idea for how I can have the text change color when I change the cell's color?
nsmythe is offline   Reply With Quote
Old May 1st, 2002, 05:17 PM   #5
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

I just edited my post, look at the 2nd piece of code and the comment above the code.
__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog
NateO is offline   Reply With Quote
Old May 1st, 2002, 05:27 PM   #6
nsmythe
New Member
 
Join Date: Apr 2002
Location: Amgen
Posts: 3
Default

TsTom

I tried your function, and it doesnt seem to change the text yellow. Do I need to do something in addition? You guys are soooooo fast.
nsmythe is offline   Reply With Quote
Old May 1st, 2002, 06:10 PM   #7
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

Stick with the function I gave you earlier and modify your worksheet_selectionchange() event procedure to look similar to the following:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Calculate
For Each cell In UsedRange
If cell <> "" And cell.Interior.ColorIndex = 11 _
Then cell.Font.ColorIndex = 36
Next
End Sub
This will hurt the performance of your workbook to some extent, to enhance performance, you may want to limit the tested range (instead of 'usedrange').

_________________
Cheers, NateO

[ This Message was edited by: NateO on 2002-05-01 17:37 ]
NateO 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:40 PM.


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