+ Reply to Thread
Results 1 to 7 of 7

Count coloured cells - But treat cells with colour and value as 0.5

Hybrid View

  1. #1
    Registered User
    Join Date
    10-19-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Count coloured cells - But treat cells with colour and value as 0.5

    Hi guys,

    I have a spreadsheet and a module that allows me to count coloured cells.

    If the cell with a colour in has ANY text in it, then I need it to count it as 0.5 instead of 1.

    Any ideas?

    Current Code:

    Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
    
        Dim rCell As Range
    
        Dim lCol As Long
    
        Dim vResult
    
    
    
    ''''''''''''''''''''''''''''''''''''''
    
    'Written by Ozgrid Business Applications
    
    'www.ozgrid.com
    
    
    
    'Sums or counts cells based on a specified fill color.
    
    '''''''''''''''''''''''''''''''''''''''
    
      
    
        lCol = rColor.Interior.ColorIndex
    
    
    
        If SUM = True Then
    
            For Each rCell In rRange
    
                If rCell.Interior.ColorIndex = lCol Then
    
                    vResult = WorksheetFunction.SUM(rCell, vResult)
    
                End If
    
            Next rCell
    
        Else
    
            For Each rCell In rRange
    
                If rCell.Interior.ColorIndex = lCol Then
    
                    vResult = 1 + vResult
    
                End If
    
            Next rCell
    
        End If
    
    
    
       ColorFunction = vResult
    
    End Function
    Cheers

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Count coloured cells - But treat cells with colour and value as 0.5

    Using your existing function change

    vResult = WorksheetFunction.SUM(rCell, vResult)
    to

    vResult = WorksheetFunction.SUM(IIf(IsNumeric(rCell), rCell, 0.5), vResult)
    If you range contains formulae nulls and you wish for them to be excluded then

    vResult = WorksheetFunction.SUM(IIf(IsNumeric(rCell), rCell, (rCell <> "") * -0.5), vResult)
    Last edited by DonkeyOte; 10-19-2009 at 11:36 AM. Reason: added point re: null

  3. #3
    Registered User
    Join Date
    10-19-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Count coloured cells - But treat cells with colour and value as 0.5

    If it has a value (text value) can I make it count is as 0.5 instead of 1?

    Thanks for the help so far

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Count coloured cells - But treat cells with colour and value as 0.5

    Let's just clarify what you want to do exactly.

    For counting cells of a given colour, non-blanks are worth 0.5 and blanks 0 ?

    For summing cells of a given colour, non-numerics worth 0

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Count coloured cells - But treat cells with colour and value as 0.5

    I'm heading off line, assuming prior post is correct then below is the original condensed and altered.

    Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
        Dim rCell As Range
        Dim lCol As Long
        Dim vResult
        lCol = rColor.Interior.ColorIndex
        For Each rCell In rRange
            If rCell.Interior.ColorIndex = lCol Then
                If SUM Then
                    vResult = WorksheetFunction.SUM(rCell.Value, vResult)
                Else
                    vResult = vResult + IIf(rCell.Value <> "", 0.5, 0)
                End If
            End If
        Next rCell
        ColorFunction = vResult
    End Function
    Though FWIW IMO it is ill advised to differentiate / calculate based on "feel / appearance".

  6. #6
    Registered User
    Join Date
    10-19-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Count coloured cells - But treat cells with colour and value as 0.5

    Quote Originally Posted by DonkeyOte View Post
    Let's just clarify what you want to do exactly.

    For counting cells of a given colour, non-blanks are worth 0.5 and blanks 0 ?

    For summing cells of a given colour, non-numerics worth 0
    Non blanks are worth 0.5 and blanks are worth 1.

    I am trying to count a spreadsheet that has been in use for several years - usings colours to display holiday days - And text within that colour to stipulate half a holiday day.

    Many thanks for your help so far

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Count coloured cells - But treat cells with colour and value as 0.5

    Non blanks are worth 0.5 and blanks are worth 1
    Change the 0 in the IIF to 1.

    usings colours to display holiday days - And text within that colour to stipulate half a holiday day
    Would be better to use H (or 1) in cell to denote Holiday and HH (or 0.5) for Half Holiday - use Conditional Formatting to apply colour based on the cell value if required. No VBA required from that point forth.
    Last edited by DonkeyOte; 10-19-2009 at 12:28 PM.

+ 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