Consulting

Results 1 to 12 of 12

Thread: SUM/COUNT CELLS BY FILL COLOUR

  1. #1
    VBAX Regular
    Joined
    Jun 2008
    Posts
    41
    Location

    Exclamation SUM/COUNT CELLS BY FILL COLOUR

    i am trying to use a custom function in excel to count the number of cells in a given range that are fill with a specific colour.
    The range in question is one Sheet1 and the formula will be on Sheet2.
    i used a module to create the custom function via the Visual Basic Editor.

    i used the following code.

    [VBA]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[/VBA]

    On Sheet2 i placed the following formula but i got a #NAME error.

    [/B]
    =ColourFunction(Sheet1!$A$1,Sheet1!$B$3:Sheet1!$CS$3,TRUE)

    Cell A1 in Sheet1 is filled with the colour the formula is to count by
    the range is B3:CS3

    i have attached the excel file i am testing this on.


    Last edited by josephm; 06-11-2008 at 07:49 AM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You have called the function ColorFunction in the code, but tried to use ColourFunction in the spreadsheet. They have to be the same.
    ____________________________________________
    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
    VBAX Regular
    Joined
    Jun 2008
    Posts
    41
    Location
    thank you i am so foolish. will it work for font color or that requires change in the code? because it will be dates in the cells in the specified range that are green

    i corrected the function name and used the following formula on sheet2 but still got the #NAME error

    ColorFunction(Sheet1!$A$1,Sheet1!$B$3:Sheet1!$CS$3,TRUE)

    in cell A1 of Sheet1 i formatted the cell to font color green but the cell is empty initially do i have to put anything in cell A1.
    is it that this formula must be used on the same sheet as the range or am I doing the sheet reference wrong?
    Last edited by josephm; 06-11-2008 at 07:33 AM.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes if you change the line

    [vba]

    If rCell.Interior.ColorIndex = lCol Then
    [/vba]

    to

    [vba]

    If rCell.Font.ColorIndex = lCol Then
    [/vba]
    ____________________________________________
    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
    VBAX Regular
    Joined
    Jun 2008
    Posts
    41
    Location
    were u able to get this to work on the sheet i provided?
    i made the change in the code as follows...

    [VBA]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

    If SUM = True Then
    For Each rCell In rRange
    If rCell.Font.ColorIndex = lCol Then
    vResult = WorksheetFunction.SUM(rCell, vResult)
    End If
    Next rCell
    Else
    For Each rCell In rRange
    If rCell.Font.ColorIndex = lCol Then
    vResult = 1 + vResult
    End If
    Next rCell
    End If

    ColorFunction = vResult
    End Function[/VBA]


    then in cell B2 of Sheet2 i placed the following formula without absolute references

    =ColorFunction(Sheet1!A1,Sheet1!B3:Sheet1!CS3,TRUE)

    and it returened 0.

    on sheet1 in cell A1 i have nothing at all no characters of any kind but when u type in the cell whatever u type will turn green.

    now with in the range of cells to be checked they are green dates in the format of 4-Feb-08 for example.

    i have column A freezed will this affect things?
    Last edited by josephm; 06-11-2008 at 08:10 AM.

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Joseph, [uvba]use em please[/uvba]. I've done this for you on your posts.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes I did, I got 39636. I think you should be using

    =ColorFunction(Sheet1!A1,Sheet1!B3:Sheet1!CS3,FALSE)

    because you want to Coount not Sum.
    ____________________________________________
    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

  8. #8
    VBAX Regular
    Joined
    Jun 2008
    Posts
    41
    Location
    =ColorFunction(Sheet1!A1,Sheet1!B3:Sheet1!CS3,FALSE)

    i used the above formula and i got #name err

  9. #9
    VBAX Regular
    Joined
    Jun 2008
    Posts
    41
    Location
    [vba]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

    If SUM = True Then
    For Each rCell In rRange
    If rCell.Font.ColorIndex = lCol Then
    vResult = WorksheetFunction.SUM(rCell, vResult)
    End If
    Next rCell
    Else
    For Each rCell In rRange
    If rCell.Font.ColorIndex = lCol Then
    vResult = 1 + vResult
    End If
    Next rCell
    End If

    ColorFunction = vResult
    End Function[/vba]

    is the syntax bolded above correct or should it be

    [VBA] lCol rCell.Font.ColorIndex[/VBA]

  10. #10
    VBAX Regular
    Joined
    Jun 2008
    Posts
    41
    Location
    thank you zack.

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

  12. #12
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    If you are getting the #NAME error, are you sure you have the function in the correct module??

Posting Permissions

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