Excel宏生成几何分布而不为零

时间:2014-09-22 13:37:35

标签: excel excel-vba vba

我需要使用excel生成几何分布值,而不是零。我有一个代码,但该代码给出了几何分布值为0。

任何人都可以帮我这个代码吗?

Function GEOMRAND() 'ByVal Beta As Double
    Beta = Cells(2, 2)
    Dim K As Double
    Dim Upper As Double
    Dim Lower As Double
    Upper = 1 - Rnd
    Lower = 1 - Beta
    K = Application.WorksheetFunction.Ln(Upper / Lower) / Application.WorksheetFunction.Ln(Lower)
    If K < 0 Then
        K = 0
    End If
    GEOMRAND = Round(K, 0)
End Function

Function GEOMINV(ByVal R As Double, ByVal B As Double) 'ByVal Beta As Double
    Dim Upper As Double
    Dim Lower As Double
    Upper = 1 - R
    Lower = 1 - B
    K = Application.WorksheetFunction.Ln(Upper / Lower) / Application.WorksheetFunction.Ln(Lower)
    If K <= 0 Then
        K = 0
    End If
    GEOMINV = Round(K, 0)
End Function

Sub GEOMGEN(ByVal Col As Integer)
    For i = 1 To 30
        For j = 1 To Col
            Cells(j + 10, i) = GEOMRAND()
        Next
    Next
End Sub

Sub GEOMFILL()
    Worksheets("G_500").Select
    GEOMGEN (500)
    Worksheets("G_2000").Select
    GEOMGEN (2000)
    Worksheets("G_8000").Select
    GEOMGEN (8000)
    Worksheets("G_32000").Select
    GEOMGEN (32000)
End Sub

1 个答案:

答案 0 :(得分:0)

问题是,对于某些值,您明确告诉excel返回0。

当K <0或K <= 0时,您应该更改If行以将值设置为零以外的值。