Allow volatile expression in VBA function

TLO2015

New Member
Joined
Feb 12, 2015
Messages
6
Hello,

I have a formula that checks the bgcolor of a reference cell (rColor), then selects all cells in the defined range (rMatrix) with corresponding color in order to calculate the sum or count the number of cells.

Problem is that I want to enter a specific formula that determines the needed range in rMatrix.
However I receive the message that the value for rMatrix is volatile.
example: rMatrix should be: cell("contents";A1) - where as the cell A1 contains the cell(address) values for the desired range.

my code for sum by color:
Code:
Function SomKleur(rColor As Range, rMatrix As Range, Optional SUM As Boolean)
    Application.Volatile
    Dim rCell As Range
    Dim lCol As Long
    Dim vResult

    lCol = rColor.Interior.ColorIndex

    If SUM = True Then
        For Each rCell In rMatrix
            If rCell.Interior.ColorIndex = lCol Then
                vResult = WorksheetFunction.SUM(rCell, vResult)
            End If
        Next rCell
    Else

        For Each rCell In rMatrix
            If rCell.Interior.ColorIndex = lCol Then
                vResult = 1 + vResult
            End If
        Next rCell
    End If

   SmColor = vResult

End Function


Thanks for your help/feedback!
T.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this to convert the address in the A1 cell into a range...

For Each rCell In Range(rMatrix.Value)
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,721
Members
449,465
Latest member
TAKLAM

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top