Peskarik
Programmer
- Aug 30, 2006
- 7
Hello, guys
This is my first post.
Moreover, I am novice at VBA.
I got a couple of questions. I have this code for expected value calculation:
Function ExpVal(vvec, pvec)
' returns expected value for 2 arrays
If Application.Sum(pvec) <> 1 Or _
Application.Count(vvec) <> Application.Count(pvec) Then
ExpVal = -1
Exit Function
ElseIf pvec.Rows.Count <> vvec.Rows.Count Then vvec = Application.Transpose(vvec)
End If
ExpVal = Application.SumProduct(vvec, pvec)
End Function
[/color blue]
Question no.1:
The function works only if the first If...Then statement is written the way it is written above. If I change it to an in-line code (everything starting with If and finishing with -1 is written on one line): function does not work.
In fact, it works only if ExpVal = -1[/color blue] is written on a separate line. Otherwise I get error message "Compile Error: Else without If".[/color red]
Can someone explain why it is like that?
Question no.2:
This is not a very flexible function, since one has to choose the pvec and vvec correctly (since there is a count function for pvec, it must sum to one, because sum of probabilities must be one.)
What would be the way to change the function so that one can choose vvec and pvec without specific order and still incorporate the probability count function?
I hope someone finds time to answer these questions. Thanks in advance!
This is my first post.
Moreover, I am novice at VBA.
I got a couple of questions. I have this code for expected value calculation:
Function ExpVal(vvec, pvec)
' returns expected value for 2 arrays
If Application.Sum(pvec) <> 1 Or _
Application.Count(vvec) <> Application.Count(pvec) Then
ExpVal = -1
Exit Function
ElseIf pvec.Rows.Count <> vvec.Rows.Count Then vvec = Application.Transpose(vvec)
End If
ExpVal = Application.SumProduct(vvec, pvec)
End Function
[/color blue]
Question no.1:
The function works only if the first If...Then statement is written the way it is written above. If I change it to an in-line code (everything starting with If and finishing with -1 is written on one line): function does not work.
In fact, it works only if ExpVal = -1[/color blue] is written on a separate line. Otherwise I get error message "Compile Error: Else without If".[/color red]
Can someone explain why it is like that?
Question no.2:
This is not a very flexible function, since one has to choose the pvec and vvec correctly (since there is a count function for pvec, it must sum to one, because sum of probabilities must be one.)
What would be the way to change the function so that one can choose vvec and pvec without specific order and still incorporate the probability count function?
I hope someone finds time to answer these questions. Thanks in advance!