Hi All,
I have gotton into a bizarre mess - it shouldnt have happened - but it has, Its not supposed to happen - but it has .............
This below is a custom function , which i have been calling from within a sheet , for ages - and never any problem ! However , calling from within VBA ........ I am doing something wrong
Anyone ??? please .................................. my code ( a trival , below )
- Code: Select all
Option Explicit
Const K_IN_BOUNDS = 1
Const K_WIN = "W"
Const K_LOSS = "L"
Const K_ERROR = "E"
Public Function AvgWinOdds(ByVal rngOutcome As Range, ByVal rngInBounds As Range, ByVal rngOdds As Range, ByVal nMinRequired As Integer, Optional ByVal nMaxRequired As Integer = 10, Optional ByVal nRowTop As Integer = 6) As Double
'
' Returns the current Strike Rate as a decimal number (eg 0.4 = 40% winners)
' -1 returned if strike rate could not be calculated.
'
' rngOutcome = cell containing most recent Outcome to be used.
' rngInBounds = cell containing most recent indicator for whether row in bounds or not (1 or 0).
' rngOdds = cell containing Winner's odds.
' nMinRequired = minimum number of qualifying races to use in calculation
' nMaxRequired = maximum number of qualifying races to use in calculation (optional, default = 10)
' nRowTop = top race row (optional, default = 6)
'
AvgWinOdds = -1
Dim iRowOutcome%: iRowOutcome = rngOutcome.Row
If nMinRequired > iRowOutcome - nRowTop + 1 Then Exit Function ' Insufficient race data available for calc.
Dim i%, j%, nW%, nL%, tW#
Const K_WIN = "W"
j = 0
For i = iRowOutcome To nRowTop Step -1
If rngInBounds.Offset(j).Value = K_IN_BOUNDS Then
If rngOutcome.Offset(j).Value = K_WIN Then
nW = nW + 1
tW = tW + rngOdds.Offset(j).Value
End If
End If
If nMaxRequired <= nW Then Exit For ' sufficient data collected already.
j = j - 1
Next
If nMinRequired > nW Then Exit Function ' Insufficient data available for calc.
AvgWinOdds = tW / (100# * CDbl(nW))
End Function
And a trivial code to test this is....
- Code: Select all
Private Sub CommandButton1_Click()
Dim outcome As String, InRange As Integer, Odds As Single, Nmin As Integer, Nmax As Integer
For i = 4 To 13 Step 1
outcome = Cells(i, 1)
InRange = Cells(i, 2)
Odds = Cells(i, 3)
Nmin = Cells(i, 5)
Nmax = Cells(i, 5)
Cells(i, 8) = AvgWinOdds(outcome, InRange, Odds, Nmin, Nmax, 4)
Next i
End Sub
On execute, it highlights the first variable inside the function , and a popup box says "Type Mismatch "