+ Reply to Thread
Results 1 to 7 of 7

Thread: Sum based on color

  1. #1
    Valued Forum Contributor johnjohns's Avatar
    Join Date
    11-19-2007
    Location
    Dubai, UAE
    MS-Off Ver
    2003 and 2007
    Posts
    504

    Sum based on color

    Hi all

    Is there a way to sum the cells based on the background color and font color? I need it such a way that summing should be according to the cell where the formula is given (based on its background/font color). I wrote a macro where I have to address a separate cell to take color. Can this be modified (considering the colors of conditional formatting also)?

    Function ColorSum(ColorTaken As Range, TheCells As Range, Optional TheFont As String)
      Dim ColorConsidered As Integer, ColorWiseTotal As Long, OneCell As Range
      Application.Volatile True
      ColorConsidered = IIf(UCase(TheFont) <> "T", ColorTaken.Interior.ColorIndex, ColorTaken.Font.ColorIndex)
      For Each OneCell In TheCells
         ColorWiseTotal = ColorWiseTotal + IIf(IIf(UCase(TheFont) <> "T", _
         OneCell.Interior.ColorIndex, OneCell.Font.ColorIndex) = ColorConsidered, OneCell.Value, 0)
      Next OneCell
      ColorSum = ColorWiseTotal
    End Function
    Any help is highly appreciated

    regards

    johnjohns
    Last edited by johnjohns; 02-10-2010 at 07:57 AM.

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Sum based on color

    The thing with conditional formatting is it doen't actually change the cell interior colorindex. You can usually get round this by using the condition that you are using in the formatting as the basis for a conditional sum (SUMIF/SUMPRODUCT/SUMIFS) and ignore the colours altogether.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Valued Forum Contributor johnjohns's Avatar
    Join Date
    11-19-2007
    Location
    Dubai, UAE
    MS-Off Ver
    2003 and 2007
    Posts
    504

    Re: Sum based on color

    In that case, is there a solution for the balance? ie, can the formula cell itself be addressed for color matching? Again, since in excel 2007 color wise filtering is possible for colors on conditional formatting also, I think there should be a solution there also.

    rgds
    johnjohns

  4. #4
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Sum based on color

    In that case, is there a solution for the balance? ie, can the formula cell itself be addressed for color matching?
    Sorry, don't quite follow what you mean.

    I understand that you can do filtering etc on colours in 2007 but imho it is really just pandering to something people do all too often that should really be avoided. Again I would suggest it would be simpler to do a conditional sum based on the same conditions that the formatting is based on unless you have a complex set up of a lot of different conditions. I'm not aware of an easy way to achieve what I think you are asking otherwise.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  5. #5
    Valued Forum Contributor johnjohns's Avatar
    Join Date
    11-19-2007
    Location
    Dubai, UAE
    MS-Off Ver
    2003 and 2007
    Posts
    504

    Re: Sum based on color

    Thank you Domski for the replies. My request is, if I am putting the formula in cell A2 and select a range for summing, can it sum the cells which are having the same color of A2 (with an option of background color or font color)?

    rgds
    johnjohns

  6. #6
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Sum based on color

    Here's an example:

    Public Function ColourSum(sumRange As Range, sumWhat As Byte, myColour As Byte) As Double
    
    Dim myRange As Range
    
    For Each myRange In sumRange
    
        If sumWhat = 1 Then
    
            If myRange.Interior.ColorIndex = myColour Then ColourSum = ColourSum + myRange.Value
            
        ElseIf sumWhat = 2 Then
        
            If myRange.Font.ColorIndex = myColour Then ColourSum = ColourSum + myRange.Value
    
        End If
        
    Next myRange
    
    End Function
    You would use it like:

    =ColourSum(A2:A10,1,3) to sum values with a red background colour

    =ColorSum(A2:A10,2,3) to sum values with a red font colour
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  7. #7
    Valued Forum Contributor johnjohns's Avatar
    Join Date
    11-19-2007
    Location
    Dubai, UAE
    MS-Off Ver
    2003 and 2007
    Posts
    504

    Re: Sum based on color

    Thanks Domski. But my requirement is, instead of mentioning the color code, the color to be taken from the formula cell's color.

    rgds
    johnjohns

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