在我调用之前在代码中定义的函数后,Excel无响应

时间:2013-02-23 18:57:40

标签: function excel-vba vba excel

我有一种情况,我想在我的vba代码中使用Erlang计算器。我知道Erlang代码可以工作,因为我之前已经使用过它,但是当我将erlang代码复制/粘贴到我的代码顶部的另一个模块中时,我似乎无法调用我需要的函数。当我调用它时,Excel无响应并占用大量内存。我知道这可能意味着某处存在无限循环,但我无法弄清楚它在哪里,或者这实际上是否是我不知道的另一个问题。

Erlang计算器是一个免费程序,我很高兴看到这个光滑的计算。

我正在调用的函数:agentno()

    'Version 1.0 Joanne Sparkes, Expedio Virtual Assistance Ltd for Call Centre Helper
    '18th November 2008
    '---------------------------------------------------------------------------------

    Public Function utilisation(intensity As Double, agents As Long) As Double
    'Copyright Expedio Virtual Assistance Ltd 2008
    'calculates utilisation or agent occupancy
     On Error GoTo utilisationerror
        utilisation = intensity / agents

    utilisationexit:
        If utilisation < 0 Then utilisation = 0
        If utilisation > 1 Then utilisation = 1
    Exit Function

    utilisationerror:
        utilisation = 0
        Resume utilisationexit
    End Function

    Public Function top(intensity As Double, agents As Long) As Double
    'Copyright Expedio Virtual Assistance Ltd 2008
    'top row of Erlang-C Formula
        top = (intensity ^ agents) / Application.WorksheetFunction.Fact(agents)
    End Function

    Public Function erlangBR(intensity As Double, agents As Long) As Double
    'Copyright Expedio Virtual Assistance Ltd 2008
    'calculates summed factorial element of Erlang-C formula
    Dim k As Long, max As Long, answer As Double

    k = 0
    max = agents - 1
    answer = 0

    For k = 0 To max
    answer = answer + ((intensity ^ k) / Application.WorksheetFunction.Fact(k))
    Next k

    erlangBR = answer

    End Function

    Public Function ErlangC(intensity As Double, agents As Long) As Double
    'Copyright Expedio Virtual Assistance Ltd 2008
    'Brings together elements of Erlang C formula Top, Utilisation and ErlangBR
    On Error GoTo ErlangCError
    ErlangC = (top(intensity, agents)) / ((top(intensity, agents)) + ((1 -utilisation  (intensity, agents)) * erlangBR(intensity, agents)))

    ErlangCExit:
        If ErlangC < 0 Then ErlangC = 0
        If ErlangC > 1 Then ErlangC = 1
    Exit Function

    ErlangCError:
        Resume ErlangCExit

    End Function

    Public Function Servicelevel(intensity As Double, agents As Long, target As Double, duration As Double) As Double
    'Copyright Expedio Virtual Assistance Ltd 2008
    'calculation of service level
    On Error GoTo servicelevelerror

    Servicelevel = 1 - (ErlangC(intensity, agents) * Exp(-(agents - intensity) * target / duration))

    Servicelevelexit:
    If Servicelevel > 1 Then Servicelevel = 1
    If Servicelevel < 0 Then Servicelevel = 0
    Exit Function

    servicelevelerror:
        Servicelevel = 0
        Resume Servicelevelexit

    End Function

    Public Function agentno(intensity As Double, target As Double, duration As Double, servreq As Double) As Long
    'Copyright Expedio Virtual Assistance Ltd 2008
    'calculates minimum agent numbers for required service level
    Dim agents As Long, minagents As Long

    minagents = Int(intensity)
    agents = minagents

    While Servicelevel(intensity, agents, target, duration) < servreq
    agents = agents + 1
    Wend

    agentno = agents

    End Function

对于长代码感到抱歉,但我觉得如果能够弄清楚发生了什么,就需要它。

无论如何,我尝试调用agentno()函数(传递所需的东西),我无法让它工作。

这是我尝试使用它的地方:

    For icount = 1 To 22
        For jcount = 1 To 6
            intensity = ((CallsForecasted(icount, jcount) / 1800) * duration) 'Calculates the intensity at each interval.
            AgentsNeeded(icount, jcount) = agentno(intensity, target, duration, servreq) '      <---         <---       <---          <---THIS IS THE ONE GIVING ME TROUBLE!!!!!!!!!!!!!!!!!!!!!!!!
        Next jcount
    Next icount

我已经完成了debug.print测试并发现强度已正确计算(我为了发布此处而将其删除),所以我知道一切都可以达到这一点,但是一旦调用了agentno() ,Excel停止响应并占用大量内存,如上所述。数组AgentsNeeded()似乎很好,就像在我所有代码的开头声明的那样。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我按照GSerg的建议进行了更多的调试,而且是导致问题的servreq。我不知道它实际上传递了一个小数到函数,所以当我想出这个时,它是一个快速解决方案,让它工作。

再次感谢GSerg沿着正确的道路推动像我一样的新手。 :d