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 Apr 1st, 2002, 10:31 PM   #1
rvences
New Member
 
Join Date: Feb 2002
Location: rvences
Posts: 9
Default

I created a custom function (using VBA) that returns the Excel Color Index of a particular cell. The function works fine, however, when changing the color format of my "target cell" my custom function does not update to indicate the target cell's new color index. I only get the update once I manually recalculate (F9) or the spreadsheet recalculates by entering data into another cell. My auto calculate is enabled, and I did use the "Application.Volatile" feature in my code. Apparently a format change alone does not cause Excel to recalculate the spread sheet. I've also tried adding "calculate" to my code, which doesn't work. How can I get my custom function to "refresh" after a format change to a cell?
rvences is offline   Reply With Quote
Old Apr 1st, 2002, 11:54 PM   #2
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

Hi rvences,

Yes, calculation is not supposed to occur upon operations that do not change the contents of cells. However, you can capture such an event (it is not a real Excel event) using "trickery". Fortunately you asked at the right time (April 1st).

First of all, you have to know how the cell format has been changed (i.e., manually vs. via a macro). If it is changed via a macro then it is easy: just re-calculate right after the macro changes the color using the worksheet object's Calculate method. If it has changed because the cell format has been manually set, it requires the trickery I referred to. You can take advantage of the fact that the format cannot be manually edited without selecting the cell first. What you have to do is use the worksheet's SelectionChange event to determine if the last (previous) cell selected was the one you are interested in. In your SelectionChange event routine you must save a pointer to the Target range. If the Target cell is the cell you are interested in you must also save the current value of the format property you are interested in (e.g., ColorIndex). Then when the next cell is selected, simply check to see if the saved cell pointer is the cell of interest. If and only if this is so the format could have been edited. So then check the format to see if it has been changed, and if so, force the worksheet to re-calculate.

Here's an example that recalculates the sheet whenever the color fill of cell B4 is manually changed.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static LastRange As Range 'The last range selected
Static LastColorIndex As Integer 'The color index of the last range selected
If Not Intersect([B4], LastRange) Is Nothing Then
If LastRange.Cells(1).Interior.ColorIndex <> LastColorIndex Then
Calculate
End If
End If
Set LastRange = Target
LastColorIndex = Target.Interior.ColorIndex
End Sub

Incidentally, I should mention that the color fill property of a cell is not changed via Conditional Formatting, so none of this applies to Conditional Formatting, but this shouldn't matter anyway since Conditional Formatting occurs based on cell content changes that trigger worksheet re-calculation.
__________________
Keep Excelling.

Damon

VBAexpert Excel Consulting
(My other life: http://damonostrander.com )
Damon Ostrander is offline   Reply With Quote
Old Apr 2nd, 2002, 09:56 PM   #3
rvences
New Member
 
Join Date: Feb 2002
Location: rvences
Posts: 9
Default

Thanks for the tip! How would I incorporate this into a custom vba function? Would I call the macro from within the function?

Thanks,
Rvences
rvences is offline   Reply With Quote
Old Apr 4th, 2002, 12:57 AM   #4
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

Hi again rvences,

You would not need to incorporate this into your function. Just put the code into the worksheet's event code module (right-click on the worksheet's tab, select View Code, and insert the code into the code pane). This routine will cause the worksheet to recalculate whenever the cell (in this example B4) fill color changes, and therefore (because you have Volatile set) your function will calculate and return the new color index (or whatever).

Happy computing.

Damon
Damon Ostrander is offline   Reply With Quote
Old Apr 4th, 2002, 06:37 AM   #5
rvences
New Member
 
Join Date: Feb 2002
Location: rvences
Posts: 9
Default

Thanks once again. I hate to be a pest, but I have yet another question. I'm trying to use the custom function as an Add In so that I can pass it around to several users. You suggest adding your code (or a modified version of your code) to my workbook, would this work by adding the code to an XLA file, then passing out the Add IN? Or, will the code only work for one specific workbook (i.e. the custom function would not be "global")?

I appreciate all your help and patience.

Rvences
rvences is offline   Reply With Quote
Old Apr 4th, 2002, 01:21 PM   #6
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default

Hi again rvences,

Unfortunately, the code would not work as an add-in because it is based on a worksheet event and so has to reside in the worksheet's event code module. In addition, I cannot envision any way to make it more general such that it could be made into an add-in or even a workbook-level macro.

Damon
Damon Ostrander is offline   Reply With Quote
Old Apr 4th, 2002, 08:27 PM   #7
rvences
New Member
 
Join Date: Feb 2002
Location: rvences
Posts: 9
Default

I really apprecite the help. I'll just let the users know that they need to recalc the worksheet before the custom formula will work properly. Again, thanks for the tips!!

Rvences
rvences is offline   Reply With Quote
Old Jan 7th, 2010, 01:17 PM   #8
MikeHshm
New Member
 
Join Date: Jan 2010
Posts: 1
Default Re: Calculate on Format Change

Damon,

I found your old posting below which nearly does what I need -

I would like to force a full recalculation if someone changes the colour of any cell - could your sub be modified to do this ?



Quote:
Originally Posted by Damon Ostrander View Post
Hi rvences,

Yes, calculation is not supposed to occur upon operations that do not change the contents of cells. However, you can capture such an event (it is not a real Excel event) using "trickery". Fortunately you asked at the right time (April 1st).

First of all, you have to know how the cell format has been changed (i.e., manually vs. via a macro). If it is changed via a macro then it is easy: just re-calculate right after the macro changes the color using the worksheet object's Calculate method. If it has changed because the cell format has been manually set, it requires the trickery I referred to. You can take advantage of the fact that the format cannot be manually edited without selecting the cell first. What you have to do is use the worksheet's SelectionChange event to determine if the last (previous) cell selected was the one you are interested in. In your SelectionChange event routine you must save a pointer to the Target range. If the Target cell is the cell you are interested in you must also save the current value of the format property you are interested in (e.g., ColorIndex). Then when the next cell is selected, simply check to see if the saved cell pointer is the cell of interest. If and only if this is so the format could have been edited. So then check the format to see if it has been changed, and if so, force the worksheet to re-calculate.

Here's an example that recalculates the sheet whenever the color fill of cell B4 is manually changed.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static LastRange As Range 'The last range selected
Static LastColorIndex As Integer 'The color index of the last range selected
If Not Intersect([B4], LastRange) Is Nothing Then
If LastRange.Cells(1).Interior.ColorIndex <> LastColorIndex Then
Calculate
End If
End If
Set LastRange = Target
LastColorIndex = Target.Interior.ColorIndex
End Sub

Incidentally, I should mention that the color fill property of a cell is not changed via Conditional Formatting, so none of this applies to Conditional Formatting, but this shouldn't matter anyway since Conditional Formatting occurs based on cell content changes that trigger worksheet re-calculation.
MikeHshm is offline   Reply With Quote
Old Apr 24th, 2010, 11:58 PM   #9
Damon Ostrander
MrExcel MVP
 
Damon Ostrander's Avatar
 
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
Default Re: Calculate on Format Change

Hi again rvences,

I haven't check out this code, but I think it should do it:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   Static LastRange As Range 'The last range selected
   Static LastColorIndex As Integer 'The color index of the last range selected

   If LastRange.Cells(1).Interior.ColorIndex <> LastColorIndex Then
      Application.CalculateFull
   End If

   Set LastRange = Target
   LastColorIndex = Target.Interior.ColorIndex

End Sub
Damon
Damon Ostrander is offline   Reply With Quote
Old Jan 23rd, 2012, 02:32 PM   #10
Tahas
Board Regular
 
Join Date: Sep 2008
Location: Richmond Hill, Canada
Posts: 88
Default Re: Calculate on Format Change

Hi rvences,

Were you able to resolve your issue? I have a similar problem and not able to resolve it.

Damon, thanks for your valuable in put
Taha
Tahas 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 12:14 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