VBA Express Forum  
Google
 




Go Back   VBA Express Forum > VBA Code & Other Help > Excel Help
     Feedback     
Register FAQ Members Arcade KBase Articles

Reply
 
Thread Tools Display Modes
Old 07-30-2010, 06:34 AM   #1
YasserKhalil

 
Joined: May 2010
Posts: 143
Kb Entries: 0
Articles: 0
Problem with UDF Function

Hi guys
I got a UDF Function which counts the colored cells
It works fine
But when I try to color a cell within the range, Excel doesn't recalculate the colored cells until I clicked twice in the cell that has the function
I want a solution to get the result as soon as I color another cell within the range..


Code:
Function CountColors(myrng As Range) Dim Cell As Range For Each Cell In myrng If Cell.Interior.ColorIndex <> xlNone Then CountColors = CountColors + 1 End If Next End Function

Local Time: 10:08 PM
Local Date: 09-08-2010

 
Reply With Quote Top
Old 07-30-2010, 07:22 AM   #2
xld
 
xld's Avatar
Distinguished Lord of VBAX

 
Joined: Apr 2005
Posts: 18,282
Kb Entries: 3
Articles: 0
Not a hope dear boy. Colouring a cell does not trigger the Excel calculation, so your UDF does not fire.

You could add Application.Volatile which will cause your UDF to fire whenever any change that causes a sheet recalculation happens, but thgis will stilnot happen if a cell colour is changed.



Nihil simul inventum est et perfectum

Abusus non tollit usum

Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber

Local Time: 04:08 AM
Local Date: 09-09-2010
Location:

 
Reply With Quote Top
Old 07-30-2010, 07:23 AM   #3
GTO
 
GTO's Avatar

 
Joined: Sep 2008
Posts: 1,817
Kb Entries: 0
Articles: 0
Changing colors is not recognized as a change, such as a change in value, nor will it cause a re-calculation of the sheet.

You could try using the selection change event to force a calc.


VBA:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Application.Intersect(Target, Me.Range("A1:C15")) Is Nothing Then Me.Calculate End If End Sub
VBA tags courtesy of www.thecodenet.com

Mark

Local Time: 08:08 PM
Local Date: 09-08-2010
Location:

 
Reply With Quote Top
Old 07-30-2010, 07:51 AM   #4
GTO
 
GTO's Avatar

 
Joined: Sep 2008
Posts: 1,817
Kb Entries: 0
Articles: 0
Here's a sharper example of what I was thinking might be a semi-okay workaround.

http://www.cpearson.com/excel/Colors.aspx

About 3/8'ths down the page. I am not saying I'd want this in anything but a small (calcualtion-wise) wb.

Hope that helps,

Mark

Local Time: 08:08 PM
Local Date: 09-08-2010
Location:

 
Reply With Quote Top
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Free Animated Greetings | Breast Enlargement | Debt Help | Breast Enlargement | Debt Help


All times are GMT -4. The time now is 11:08 PM.


Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright @2004 - 2009 VBA Express