UDF gets called 3 times

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,531
Office Version
  1. 365
Platform
  1. Windows
I have a UDF that is called from 2 sheets (Undo and Undo (20)) in one workbook. Here's the code:
VBA Code:
Function Odds2Match(pWins2Go As Double, pLoss2Go As Long, pProbWin As Double) As Double

Debug.Print "Book:  " & Application.ActiveWorkbook.Name
Debug.Print "Sheet: " & Application.ActiveSheet.Name
Debug.Print "Cell:  " & Application.ActiveCell.Address

'Check the  number of wins & losses and the winning probability
If pWins2Go < 1 Then
  Odds2Match = CVErr(xlErrValue): Exit Function: End If
If pLoss2Go < 0 Then
  Odds2Match = CVErr(xlErrValue): Exit Function: End If
If pProbWin < 0 Or pProbWin > 1 Then
  Odds2Match = CVErr(xlErrValue): Exit Function: End If

Dim W As Long   'Number of wins to pass to binom.dist
W = pWins2Go - 1

'Add up the probability of winning with each of the remaining numbers of losses
Dim G As Long   'Number of games to pass to binom.dist
Dim odds As Double
Dim L As Double
Odds2Match = 0
'Error: Wins & losses for this game not included
For L = 0 To pLoss2Go - 1
  G = W + L
  odds = WorksheetFunction.Binom_Dist(W, G, pProbWin, False) * pProbWin
  Odds2Match = Odds2Math + odds
Next L

End Function

I was getting a wrong answer, so I set a breakpoint on the first line. Then in the Undo sheet, I ran the UDF by editing the cell that calls it and then tabbing out. The UDF runs 3 times. The first time the parameters are as from the other sheet, the second time pProbWin = 0, and the third time it has the correct values from the first sheet.

I then added the 3 debug.print statements. This is the contents of the Immediate window:

Code:
Book:  Tally with Undo.xlsm
Sheet: Undo
Cell:  $K$5
Book:  Tally with Undo.xlsm
Sheet: Undo
Cell:  $K$5
Book:  Tally with Undo.xlsm
Sheet: Undo
Cell:  $K$5

According to this, it is called 3 times from the same cell in the same sheet.

Can someone please tell me what the heck is going on?

Thanks
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Looks like you are using Selection Change event to trigger a macro, right? You probably have Change event that re-trigger the macro. This perhaps the cause.
 
Upvote 0
The only way that I could reproduce the what you described was to have the function in the spreadsheet being used in the spreadsheet 3 times and have $K$5 being one of the parameters and used by all 3 functions.
ie K5 was changed and hence the active cell, triggering 3 uses / occurences of the function to update.
 
Upvote 0
Looks like you are using Selection Change event to trigger a macro, right? You probably have Change event that re-trigger the macro. This perhaps the cause.
Sorry, I should have mentioned how it is called. It's called from cell K5 in a sheet that keep track of wins and losses in a game. The sheet has a button control with buttons for Win and Lose that each run a macro to tally the score. But here I am testing it by just opening K5 and tabbing out, which executes the Odds2Match function.
 
Upvote 0
First, it's not necessarily being called from the same cell each time, it's just that your debug statements refer to the active workbook, sheet and cell, which doesn't change.
When stepping thru the function, the parameters passed to it on the first call are exactly what they would be if called from the backup sheet. On the 2nd call, they don't match any sheet. On the third call, they match the parameters on the "active" sheet. So I thought the debug statements would show if it is being called from a different sheet.

What debug statements would I use to see where the function is being called from?
 
Upvote 0
Second, have a read of Charles WIlliams' page here: Excel User-Defined Functions - Decision Models
I'm not sure I followed all of that, but it seems like the problem may be that Excel is working its way through various dependencies. The expression in cell that calls the function is kinda complicated:

VBA Code:
=IF(Wins2Go>0, Odds2Match(Wins2Go,LossesMax-SetGmsLstTop,LatestWinPC), BINOM.DIST(SetGmsWonTop,SetGmsTop,LatestWinPC,FALSE))

But I still don't understand why it would get called 3 times with completely different parameters.
 
Upvote 0
Code:
 Odds2Match = Odds2Math + odds

What is "Odds2Math"?
It's a typo. I guess that after I pasted the code, I accidentally deleted the "c". It should be:
Code:
 Odds2Match = Odds2Match + odds
That's how it is in the actual code.
 
Upvote 0
Application.ThisCell is the cell calling the function.
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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