0

I want to count number of cells with two criteria : their color and their value.

I select the range, then the color, then the value of the cell.

I made this code in vba but it doesn't work.

Function CountCcolor(range_data As Range, criteria As Range, cellvalue As Range) As Long
    Dim datax As Range
    Dim xcolor As Long
xcolor = criteria.Interior.ColorIndex
For Each datax In range_data
    If datax.Interior.ColorIndex = xcolor And datax.value = cellvalue.value Then
        CountCcolor = CountCcolor + 1
    End If
Next datax
End Function

I think that the problem is in my cellvalue.value I don't know how I can access a selected cell value.

Thank you for your help

3
  • 3
    how are your cells colored; Manually through formatting, or Automatic through conditional formatting? Commented Apr 12, 2018 at 16:24
  • 1
    "doesn't work" is not very descriptive: what does it do? Commented Apr 12, 2018 at 16:56
  • it shows a value error at the begining. But now it works fine. Just it doesn't update automatically when I change the color of the cell Commented Apr 12, 2018 at 20:01

1 Answer 1

1

First time trying to help someone on here, so I hope I'm doing it right!!

Try adding

Application.Volatile

to your code just below your variables declaration. This makes Excel recalculate your function when it performs its standard recalc.

However, if you use your UDF a lot, things can slow down. Also becomes a monumental pain when stepping through code, if you haven't disabled autocalc.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.