Jurijs Visockis
Jurijs Visockis

Reputation: 141

how to repeat function in excel

I made a function in Excel. But after cell update function value is not changed. How to update function cell value after cell changing

Public Function SumaByColor(ByVal ColorIndex As Range, ByVal TableRange As Range) As Double

Dim cell As Range
Dim colorIndexNumber As Integer
Dim colorSum As Double

colorIndexNumber = ColorIndex.Interior.ColorIndex

For Each cell In TableRange
If cell.Interior.ColorIndex = colorIndexNumber Then
colorSum = colorSum + 1
End If

Next cell

SumaByColor = colorSum
End Function

Upvotes: 1

Views: 495

Answers (1)

Harun24hr
Harun24hr

Reputation: 36840

First use Application.Volatile beginning of codes then write Me.Calculate in Worksheet_SelectionChange

Public Function SumaByColor(ByVal ColorIndex As Range, ByVal TableRange As Range) As Double
    Application.Volatile
    Dim cell As Range
    Dim colorIndexNumber As Integer
    Dim colorSum As Double

    colorIndexNumber = ColorIndex.Interior.ColorIndex

    For Each cell In TableRange
    If cell.Interior.ColorIndex = colorIndexNumber Then
    colorSum = colorSum + 1
    End If

    Next cell

    SumaByColor = colorSum
End Function

Worksheet_SelectionChange: enter image description here

Upvotes: 1

Related Questions