Consulting

Results 1 to 4 of 4

Thread: Problem with UDF Function

  1. #1

    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..

    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

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    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]

    Mark

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •