0

指定された期間の分布の期待値を返す関数を作成したいと考えています。入力は次の形式のみになります ExpValue(Jan--01/June30;EXPO(2000),July--01/Dec--31;NORM(1000,2000)) --01/Dec--31 は期間です。また、EXPO(2000) と NORM(1000,2000) は、その特定の季節のデータの分布のタイプ (平均値、標準偏差などを括弧内に示します) です。ユーザーは、分布のタイプとして最初の 4 文字のみを入力する必要があります。たとえば、NORMAL 分布の場合は NORM、EXPONENTIAL 分布の場合は EXPO などです。"," で区切られたできるだけ多くのシーズンと、各シーズンの期間のタイプを入力する必要があります。 「;」で区切られたデュレーションからそれぞれ言及されます。

関数のコードを書きましたが、機能しません。必要な変更を提案してください。

Public Function ExpValue(str As String) As String

Dim Mylen As Integer
Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim D As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Double
Dim N As Integer
Dim ExpectValue As Double
Dim Arr1() As String
Dim Arr2() As String
Dim Arr3() As String
Dim Arr4() As String
Dim Arr5() As String
Dim Arr6() As String
Dim Arr7() As Double
Dim txt1 As String
Dim txt2 As String
Dim txt3 As String
Dim txt4 As String
Dim txt5 As String
Dim txt6 As String

Arr1() = Split(str, ",")
C = UBound(Arr1())
ReDim Arr1(C) As String

    For i = 0 To C

            Arr2() = Split(Arr1(i), ";")
            ReDim Arr2(1) As String
            Arr3(i) = Arr2(0)
            Arr4(i) = Arr2(1)

    Next i

    ReDim Arr3(C) As String
    ReDim Arr4(C) As String

    For i = 0 To C
        txt1 = Arr4(i)
        Mylen = Len(txt1)
        txt2 = Left(txt1, 4)        ' type of distribution
        A = Mylen - 5
        B = Mylen - 6
        txt3 = Right(txt1, A)       ' 1,2.3,4,... )
        txt4 = Left(txt3, B)        ' 1,2.3,4.,..
        Arr5 = Split(txt4, ",")
        D = UBound(Arr5())

        ReDim Arr7(D) As Double
            For j = 0 To D
                Arr7(i) = CDbl(Arr5(i))
            Next j

        Select Case txt2

            Case "EXPO", "POIS"             ' just one number EXPO(2.34)
            l = CDbl(txt4)                  ' txt4=2.34
            Arr6(i) = l

            Case "NORM"
            ExpectValue = Arr7(1)
            Arr6(i) = ExpectValue

            Case "BETA"
            ExpectValue = (Arr7(1) / (Arr7(0) + Arr7(1)))
            Arr6(i) = ExpectValue

            Case "GAMM"
            ExpectValue = Arr7(0) * Arr7(1)
            Arr6(i) = ExpectValue

            Case "TRIA"
            ExpectValue = ((Arr7(0) + Arr7(1) + Arr7(2)) / 3)
            Arr6(i) = ExpectValue

            Case "UNIF"
            ExpectValue = ((Arr7(0) + Arr7(1)) / 2)
            Arr6(i) = ExpectValue

            Case "LOGN"
            ExpectValue = Exp((Arr7(0) + ((Arr7(1)) ^ 2)) / 2)
            Arr6(i) = ExpectValue

            Case "ERLA"
            ExpectValue = Arr7(0) * Arr7(1)
            Arr6(i) = ExpectValue

        End Select

    'Next j
    Next i
    'i = i + 1

    If C = 0 Then
        txt6 = Arr3(0) & ";" & Arr6(0)
    Else
        txt6 = ""

        For i = 0 To C
            txt6 = txt6 & "," & Arr3(i) & "," & Arr6(i)
        Next i

    End If

    ExpValue = txt5

End Function
4

1 に答える 1

0
DateDiff("d",FirstDate,SecondDate) '+ 1

2 つの日付の間の日数の差、「m」は月などを示します。これは最初の日付から得られるので、それを除外します。合計日数が必要な場合は +1 だけです。

上記のコードに関しては、私はそれを機能させましたが、あなたが達成しようとしていることを理解するのは難しいです. 数式全体を文字列として渡すよりも、おそらく良い方法があります。

=ExpValue("1/1/2001-30/6/2001;EXPO(2000)|1/7/2001-31/12/2001;NORM(1000,2000)")

文字列の分割部分をより明確に識別できるように、数式をいくつか変更しました。"," は NORM(,) 関数に含まれているため使用できません。したがって、「-」は日付を分割し、「;」関数を分割し、「|」2 つの方程式を分割します。

これが私の試みです。コードは次のようになります。ケースの選択メソッドも正しく機能していないと思いますが、機能する機能を使用すると改善できます。

Public Function ExpValue(str As String) As String

Dim Mylen As Integer, A As Integer, B As Integer, C As Integer, D As Integer, i As Integer
Dim j As Integer, k As Integer, l As Double, N As Integer, ExpectValue As Double
Dim Arr1() As String, Arr2() As String, Arr3() As String, Arr4() As String, Arr5() As String
Dim Arr6() As String, Arr7() As Double, txt1 As String, txt2 As String, txt3 As String
Dim txt4 As String, txt5 As String, txt6 As String

Dim d1 As Date, d2 As Date, ArrDate() As String
Dim dPart() As Integer

Arr1() = Split(str, "|")
C = UBound(Arr1())

ReDim Arr3(C)
ReDim Arr4(C)
ReDim dPart(C)


For i = 0 To C
    Arr2() = Split(Arr1(i), ";")
    Arr3(i) = Arr2(0)
    Arr4(i) = Arr2(1)

    ArrDate() = Split(Arr2(0), "-")
    d1 = ArrDate(0)
    d2 = ArrDate(1)
    dPart(i) = DateDiff("d", d1, d2) + 1
Next i

ReDim Preserve Arr3(C) As String
ReDim Preserve Arr4(C) As String
ReDim Arr6(C)

For i = 0 To C
    If Arr4(i) <> "" Then
    txt1 = Arr4(i)
    Mylen = Len(txt1)
    txt2 = Left(txt1, 4)    ' type of distribution
    A = Mylen - 5
    B = Mylen - 6
    txt3 = Right(txt1, A)    ' 1,2.3,4,... )
    txt4 = Left(txt3, B)        ' 1,2.3,4.,..
    Arr5 = Split(txt4, ",")
    D = UBound(Arr5())

    ReDim Preserve Arr7(D) As Double

        For j = 0 To D
                Arr7(j) = CDbl(Arr5(0))
        Next j

        Select Case txt2

        Case "EXPO", "POIS"        ' just one number EXPO(2.34)
        l = CDbl(txt4)        ' txt4=2.34
        Arr6(i) = l

        Case "NORM"
        ExpectValue = Arr7(0)
        Arr6(i) = ExpectValue

        Case "BETA"
        ExpectValue = (Arr7(1) / (Arr7(0) + Arr7(1)))
        Arr6(i) = ExpectValue

        Case "GAMM"
        ExpectValue = Arr7(0) * Arr7(1)
        Arr6(i) = ExpectValue

        Case "TRIA"
        ExpectValue = ((Arr7(0) + Arr7(1) + Arr7(2)) / 3)
        Arr6(i) = ExpectValue

        Case "UNIF"
        ExpectValue = ((Arr7(0) + Arr7(1)) / 2)
        Arr6(i) = ExpectValue

        Case "LOGN"
        ExpectValue = Exp((Arr7(0) + ((Arr7(1)) ^ 2)) / 2)
             Arr6(i) = ExpectValue

        Case "ERLA"
        ExpectValue = Arr7(0) * Arr7(1)
        Arr6(i) = ExpectValue

        End Select
    End If
    Next i

    If C = 0 Then
        txt6 = dPart(0) & ";" & Arr3(0) & ";" & Arr6(0)
    Else
        txt6 = ""

        For i = 0 To C
            txt6 = txt6 & "," & dPart(i) & "," & Arr6(i)
        Next i
    End If

    ExpValue = txt6

End Function
于 2012-07-06T00:06:53.017 に答える