简体   繁体   English

在创建excel-vba函数时需要帮助

[英]need help in creating excel-vba function

My problem: It works fine when I call it in a SUb. 我的问题:当我在SUb中调用它时,它工作正常。 But it gives a high jump in the value of "pf" I want to give a condition that pf is always less than dpdlold...I tried different ways but nothing worked.. 但是它给“ pf”的值带来了很大的跳跃。我想给一个条件,就是pf总是小于dpdlold ...我尝试了不同的方法,但是没有任何效果。

code starts from here: 代码从这里开始:

Public Function pf(p, pwf, xf, PI, tau, n, k, wg, Hf, wf, Bg, dpdlold, mug, Bg_p, mug_p)

Dim i, j As Integer

Dim error As Double

Dim pj As Double

Dim pj1 As Double

Dim pj0 As Double

Dim e As Double

j = 100

i = 0

error = 1000

pj0 = dpdlold

pj = pj0

e = 0.001


    While (error > e) And (i < j)
        qgr = FrGas(tau, n, k, wg, Hf, wf, pj, mug_p)
        pj1 = (p - pwf - 2 * qgr / PI) / xf
        error = Abs(pj1 - pj)
        pj = pj1
        i = i + 1
    Wend
        pf = pj


End Function

How about changing your loop to something like this: 如何将循环更改为如下所示:

While (error > e) And (i < j)
    qgr = FrGas(tau, n, k, wg, Hf, wf, pj, mug_p)
    pj1 = (p - pwf - 2 * qgr / PI) / xf
    error = Abs(pj1 - pj)
    if pj1 > dpdlold Then pj1 = dpdlold
    pj = pj1
    i = i + 1
Wend

Now, if pj1 gets too big, it will be "clipped" to the value of dpdlold . 现在,如果pj1太大,它将被“剪切”为dpdlold的值。 If it hits the limit twice, the "error" in successive estimates will be zero and it will stop. 如果它两次达到极限,则连续估计中的“错误”将为零,并且将停止。

With the limited information you gave it's the best I can do… 您所提供的信息有限,这是我所能做到的最好的…

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM