vendredi 28 octobre 2016

Writing a function in VBA to return a triangular random number

I am trying to write a series of VBA functions for excel that would return random number types to call on in subroutines. I am getting a similar error once I go into the more complicated formulas. For example: the asymmetric triangular random variable.

'the function

Function AsymetricTriangleInVBA (min As Double, mode As Double, max As Double) As Double
  Application.Volatile
  Randomize
  Dim Temp As Variant
  Temp = Rnd
  AsymetricTriangleInVBA = WorksheetFunction.if(Temp < ((mode - min) / (max - min)), min + (max - min) * WorksheetFunction.sqrt(((mode - min) / (max - min)) * Temp), min + (max - min) * (1 - WorksheetFunction.sqrt((1 - ((mode - min) / (max - min))) * (1 - Temp))))
End Function

'my test sub

Sub test()
    MsgBox AsymetricTriangleInVBA(5, 10, 15)
End Sub

The consistent error i am receiving is:

Run time error '438': Object does not support this property or method

Any thoughts?

Thanks




Aucun commentaire:

Enregistrer un commentaire