Since UDF (user defined functions) do not allow changing other cell's values your attempt does not work.
As a workaround you can use a global/public variable Public Compteur As Long
to count in your UDF Bidcalcul
. And another volatile function Public Function GetCompteur() As Long
to output that variable.
Option Explicit
Public Compteur(1 To 1048576) As Long
Public Function Bidcalcul(ByVal Bid As Variant, ByVal IntervMin As Variant, ByVal IntervMax As Variant, ByVal Ticksize As Variant, ByVal Percentage As Variant) As Variant
Dim ValeurInitial As Variant
ValeurInitial = Application.ThisCell.Value
Dim RowCompteur As Long ' row counting has to be long there are more rows than integer can handle!
RowCompteur = Application.ThisCell.Row ' don't use ActiveCell!
If IsError(Bid) Then
Bidcalcul = Application.WorksheetFunction.Floor(Bid * Percentage, Ticksize)
End If
If IntervMin <= (Bid - ValeurInitial) And (Bid - ValeurInitial) <= IntervMax Then
Bidcalcul = ValeurInitial
Else
Bidcalcul = Application.WorksheetFunction.Floor(Bid * Percentage, Ticksize)
Compteur(RowCompteur) = Compteur(RowCompteur) + 1
End If
End Function
Public Function GetCompteur() As Long
Application.Volatile
GetCompteur = Compteur(Application.ThisCell.Row)
End Function
Note that you should not use ActiveCell
in a UDF because the active cell is not the cell the formula is written in. This is Application.ThisCell
but any other cell on the worksheet could be the ActiveCell
, so you might get wrong results!
Also your row counting variables need to be of type Long
because Excel has more rows than Integer
can handle. Actually I recommend always to use Long
instead of Integer
as there is no benefit in using Integer
in VBA.
But your entire approach has one big issue
With ValeurInitial = Application.ThisCell.Value
you reference to the same cell your formula is written in. So this genereates a circular reference and that is where you run into a problem. Because now if the formula calculates it's result changes and this changes thi initial value which would the formula need to calculate again and again.
Yes, this did not happen when you used ActiveCell.Value
but that definitely gave you a wrong result. So you might need to re-think your approach or how you deal with the circular reference.
My recommendation to get out of this issue
Don't use UDFs for calculating this. Use a command button to launch a calculation procedure Sub
and read your raw data into an array (for fast calculation) let your procedure do all the work and output the result in an output array that you can easily write into your sheet then.
This way you don't have to deal with circular references and you can easily count your iteration steps of your calulation. It gives you much more control over the entire process.