Consulting

Results 1 to 10 of 10

Thread: SUM by colour

  1. #1

    Unhappy SUM by colour

    Hi all,

    I have a code that sums the values of cells with the same colour in a range of excel cells, and it works nicely... well, almost.

    It works nicely if there are only numbers in the range of cells. If I add text in any of them, I get #VALUE!

    How do I change the code so it ignores the text and only sums the numbers?

    Code below:


    Option Explicit
    
    
    
    Function ColorMath(InputRange As Range, ReferenceCell As Range, Optional Action As String = "S")
        
        Application.Volatile
        
        ' Action can be S to SUM, A to AVERAGE, or C to COUNT
        ' If not specified the default Action is SUM
        
        Dim ReferenceColor As Long
        Dim CellCount As Long
        Dim Result As Variant
        Dim Cell As Range
         
        Action = UCase(Action)
        Result = 0
        CellCount = 0
        ReferenceColor = ReferenceCell.Interior.Color
            
        If Action = "S" Or Action = "A" Then
                
            For Each Cell In InputRange
            
                If Cell.Interior.Color = ReferenceColor Then
                        
                    Result = Result + Cell.Value
                    CellCount = CellCount + 1
                            
                End If
                    
            Next Cell
                            
        End If
                
        If Action = "C" Then
                
            For Each Cell In InputRange
            
                If Cell.Interior.Color = ReferenceColor Then Result = Result + 1
                    
            Next Cell
                
        End If
                
        If Action = "A" Then
                
            Result = Result / CellCount
        
        End If
            
        
        ColorMath = Result
    
    
    End Function

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Add an If Then test
    If IsNumeric(Cell) Then
    Additions
    End If
    BTW, what is CellCount Used for?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Thanks SamT,

    I added 'if' as you suggested, but it comes up with an error, highlighting 'Additions'...

    CellCount is for counting the number of cells. I think I just grabbed the code from somewhere.

    I also have another version of the code that sums the values in the range of cells successfully, even if there is text in some of them. But that code doesn't seem to differentiate between cell colours that are similar. Is there a way for a code to look for the exact colour code? So that even two colours that are very similar are determined as different colours?

    The code below:

    Function SumByColor(CellColor As Range, rRange As Range)Dim cSum As Long
    Dim ColIndex As Integer
    ColIndex = CellColor.Interior.ColorIndex
    For Each cl In rRange
    If cl.Interior.ColorIndex = ColIndex Then
    cSum = WorksheetFunction.SUM(cl, cSum)
    End If
    Next cl
    SumByColor = cSum
    End Function
    Thank you!

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Function SumByColor(CellColor As Range, rRange As Range)
    Dim cell As Range
    Dim cSum As Long
    Dim ColIndex As Long
        
        ColIndex = CellColor.Interior.Color
        For Each cell In rRange
            If cell.Interior.Color = ColIndex Then
                cSum = WorksheetFunction.Sum(cell, cSum)
            End If
        Next cell
        SumByColor = cSum
    End Function
    ____________________________________________
    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

  5. #5
    Thank you xld. Much appreciated! That seems to differentiate the colours well! So, it works.

    Any way this could be applied to my original code? That one sums the cells and counts the cells, which is what I need. But it doesn't sum, if there is text in any of the cells. Any way you can amend that one?

    Many thanks!

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
     
    Function ColorMath(InputRange As Range, ReferenceCell As Range, Optional Action As String = "S")
         
        Application.Volatile
         
         ' Action can be S to SUM, A to AVERAGE, or C to COUNT
         ' If not specified the default Action is SUM
         
        Dim ReferenceColor As Long
        Dim CellCount As Long
        Dim Result As Variant
        Dim cell As Range
         
        Action = UCase(Action)
        ReferenceColor = ReferenceCell.Interior.Color
         
        If Action = "S" Or Action = "A" Then
             
            For Each cell In InputRange
                 
                If cell.Interior.Color = ReferenceColor Then
                     
                    Result = WorksheetFunction.Sum(cell, Result)
                    CellCount = CellCount + 1
                End If
            Next cell
        End If
         
        If Action = "C" Then
             
            For Each cell In InputRange
                 
                If cell.Interior.Color = ReferenceColor Then Result = Result + 1
            Next cell
        End If
         
        If Action = "A" Then
             
            Result = Result / CellCount
        End If
         
        ColorMath = Result
    End Function
    ____________________________________________
    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

  7. #7
    THANK YOU! This works perfectly!

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Did you find the Keyword "Additions" in any Excel or VBA Help file anywhere? No? Well that's because it isn't. That's where you are supposed to place your adding cell values code.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by SamT View Post
    BTW, what is CellCount Used for?
    To calculate the average.
    ____________________________________________
    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

  10. #10
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Thanks. Now I SEE!
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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