VBA Formula Fails to Execute

P

Patrice Stewart

The procedure below increments a counter (StatusCount) based on the value
contained in range cells. The procedure executes properly when the first
range cell value is changed but will not automatically execute again.

Any assistance would be appreciated.


Function StatusCount(Status)
For Each cell In Worksheets("Sheet1").range("ILStatusRange").Cells
If cell.Value = Status Then
StatusCount = StatusCount + 1
End If
Next
End Function

Function is shown in cell as: =StatusCount("O") where "O" is one of the
six values that occur in the range.
 
D

Dave Peterson

Since you embedded the range to check inside the code, excel doesn't know when
to recalculate this function.

It would be better to pass that status code and the range to the function. Then
if either changes, the function will recalculate:

Function StatusCount(Status as string, rng as range) as long
dim cell as range
For Each cell In rng.Cells
If cell.Value = Status Then
StatusCount = StatusCount + 1
End If
Next cell
End Function

And you could call it like:

=statuscount("O",sheet1!Ilstatusrange)

=========
But excel has a function that looks a lot like your function.

Take a look at =countif()

=countif(sheet1!ilstatusrange,"0")
or
=countif(ilstatusrange,"0")
if you're on sheet1 or ilstatusrange is a workbook level name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top