+ Reply to Thread
Results 1 to 5 of 5

CodeFunction Won't Refresh Dynamically

Hybrid View

  1. #1
    Registered User
    Join Date
    04-17-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    38

    CodeFunction Won't Refresh Dynamically

    Apologies in advance if this has been posted in the wrong thread!

    I am a newcomer to vba and have utilised a vba function for summing by colour. I've got it to work but it does not update dynamically i.e. if I uncolour the cells they still remain in the sum. Is there anything I can do to enable it to refresh every time cells are coloured / uncoloured?


    Function ColorFunction(rColor As Range, rRange As Range, Optional SUM_MIN As Integer = 0, Optional iSign As Integer = 0)
    Dim rCell As Range
    Dim lCol As Long
    Dim vResult
    Dim min_val As Variant
    min_val = 1E+16 ' An arbitrary high-value
    
    
    ''''''''''''''''''''''''''''''''''''''
    'Written by Ozgrid Business Applications
    'www.ozgrid.com
    'Modified by Hui
    'SUM_Min
    '0 - Sum cells
    '1 - Count
    '2 - Minimum
    '3 - Maximum
    '''''''''''''''''''''''''''''''''''''''
    lCol = rColor.Interior.ColorIndex
    If SUM_MIN = 0 Then ' Sum cells. = Default Value
    For Each rCell In rRange
    If rCell.Interior.ColorIndex = lCol Then
    If iSign = -1 And rCell.Value < 0 Then
    vResult = vResult + rCell.Value
    ElseIf iSign = 1 And rCell.Value > 0 Then
    vResult = vResult + rCell.Value
    ElseIf iSign = 0 Then
    vResult = vResult + rCell.Value
    End If
    End If
    Next rCell
    ElseIf SUM_MIN = 1 Then ' Count cells.
    vResult = 0
    For Each rCell In rRange
    If rCell.Interior.ColorIndex = lCol Then
    If iSign = -1 And rCell.Value < 0 Then
    vResult = vResult + 1
    ElseIf iSign = 1 And rCell.Value > 0 Then
    vResult = vResult + 1
    ElseIf iSign = 0 Then
    vResult = vResult + 1
    End If
    End If
    Next rCell
    ElseIf SUM_MIN = 2 Then ' extract Minimum
    For Each rCell In rRange
    If rCell.Interior.ColorIndex = lCol Then
    If iSign = -1 And rCell.Value < 0 And rCell < min_val Then
    min_val = rCell
    ElseIf iSign = 1 And rCell.Value >= 0 And rCell < min_val Then
    min_val = rCell
    ElseIf iSign = 0 And rCell < min_val Then
    min_val = rCell
    End If
    End If
    Next rCell
    vResult = min_val
    If vResult = 1E+16 Then vResult = "Error"
    
    
    ElseIf SUM_MIN = 3 Then ' extract Maximum
    min_val = -min_val
    For Each rCell In rRange
    If rCell.Interior.ColorIndex = lCol Then
    If iSign = -1 And rCell.Value < 0 And rCell.Value > min_val Then
    min_val = rCell
    ElseIf iSign = 1 And rCell.Value >= 0 And rCell > min_val Then
    min_val = rCell
    ElseIf iSign = 0 And rCell > min_val Then
    min_val = rCell
    End If
    End If
    Next rCell
    vResult = min_val
    If vResult = 1E+16 Then vResult = "Error"
    
    
    End If
    ColorFunction = vResult
    End Function

  2. #2
    Forum Contributor
    Join Date
    10-18-2012
    Location
    Telford, England
    MS-Off Ver
    Excel 2010 (2003)
    Posts
    294

    Re: CodeFunction Won't Refresh Dynamically

    User defined functions will only run when the values change, not when their colours change.
    You can override this by making the function volatile - it will be run at every calculation (including ones in other workbooks that are open at the same time.
    Function My_Func()
        Application.Volatile
        ' 
        '    Remainder of the function
        ">
    End Function
    from http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: CodeFunction Won't Refresh Dynamically

    please also read the forum rules about cross-posting. this has already been answered in at least one other forum
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Registered User
    Join Date
    04-17-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    38

    Re: CodeFunction Won't Refresh Dynamically

    Thanks brynbaker

    Point taken Joseph.

    How do I mark this thread solved?

  5. #5
    Registered User
    Join Date
    04-17-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    38

    Re: CodeFunction Won't Refresh Dynamically

    Ignore my last comment; thread now marked as solved...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1