I am trying to make a modelisation of Monte-Carlo for 5000 trials. I seem to have a weird problem with
g = WorksheetFunction.Norm_S_Inv(Rnd())
During the functioning of the loop (of the 5000 trials), at first it is working but then an error message occurs:
Run-time error "1004":
Method 'Norm_S_Inv' of object 'WorksheetFunction' failed
I have checked on many VBA sites and I don't seem to find a solution. I also tested with
Application.WorksheetFunction.Norm_S_Inv(Rnd())
WorksheetFunction.Norm_S_Inv(Rnd())
WorksheetFunction.Norm.S.Inv
Application.WorksheetFunction.Norm.S.Inv(Rnd())
WorksheetFunction.NormSInv
Thank you in advance,
It happens when your Rnd()
returns a value of 0
or 1
, which is of course an acceptable value for the Rnd()
function but it is not as an input of the Norm_S_Inv()
.
I can reproduce that easily on my computer (my Excel is in Italian, actually I get the 1004
code but the text on my side rather says "Cannot find property Norm_S_Inv for the class WorksheetFunction):
g = WorksheetFunction.Norm_S_Inv(0.14)
Debug.Print g
>>> -1.08031934081496
g = WorksheetFunction.Norm_S_Inv(0) 'or g = WorksheetFunction.Norm_S_Inv(1)
>>>
In order to avoid that, make sure you don't get 0
or 1
as inputs of your function:
myRand = Rnd()
Do Until (myRand <> 0 And myRand <> 1)
myRand = Rnd()
Loop
g = WorksheetFunction.Norm_S_Inv(myRand)
Personal note: this seems a pretty bad error handling done for this function, the error message is not at all clear and I understand your frustration in finding the root cause.