excel - 使用 VBA 的 Z 因子迭代
问题描述
我创建了一个带有两个子例程的表单按钮。1.压力深度计算 2.Z
因子计算
两者都是迭代。(1) 正常运行,而 (2) 不执行。
以下是代码:
Sub PressureDepthCalculation()
'Declaring Variables
Dim i, t, row As Integer
t = Range("B5").Value
row = 11
'For Loop
For i = t To 0 Step -100
'Depth caclculation
Range("A" & row).Value = i
'Pressure calculation
Range("B" & row).Value = Range("F5").Value + 0.052 * Range("F6") * i
row = row + 1
Next i
End Sub
Sub ZFactorCalculation()
'Z factor calculation
Dim r1, r2, r3, r4, r5, ppc, tpc, ppr, tpr, fr, dfr, ddfr, rhor As Double
Dim i, row,t As Integer
t = 1
row = 11
Range("D6").Value = 10.731
a1 = 0.3265
a2 = 1.07 * -1
a3 = 0.5339 * -1
a4 = 0.01569
a5 = 0.05165 * -1
a6 = 0.5475
a7 = 0.7361 * -1
a8 = 0.1844
a9 = 0.1056
a10 = 0.6134
a11 = 0.721
For i = t To 100
ppc = (4.6 + (0.1 * Range("H6").Value) - (0.258 * Range("H6").Value ^ 2) * 10.1325 * 14.7)
tpc = (99.3 + (180 * Range("H6").Value) - (6.94 * Range("H6").Value ^ 2) * 1.8)
ppr = 6760 / ppc
tpr = Range("B6").Value / tpc
rhor = 0.27 * ppr / tpr
r1 = (a1 + (a2 / tpr) + (a3 / tpr ^ 3) + (a4 / tpr ^ 4) + (a5 / tpr ^ 5))
r2 = ((0.27 * ppr) / tpr)
r3 = (a6 + (a7 / tpr) + (a8 / tpr ^ 2))
r4 = a9 * ((a7 / tpr) + (a8 / tpr ^ 2))
r5 = (a10 / tpr ^ 3)
fr = (r1 * rhor) - (r2 / rhor) + (r3 * rhor ^ 2) - (r4 * rhor ^ 5) + (r5 * (1 + (a11 * rhor ^ 2))) * (Exp(-a11 * rhor ^ 2)) + 1
dfr = (r1) + (r2 / rhor ^ 2) + (2 * r3 * rhor) - (5 * r4 * rhor ^ 4) + (2 * r5 * rhor * (Exp(-a11 * rhor ^ 2)) * ((1 + (2 * a11 * rhor ^ 3)) - (a11 * rhor ^ 2 * (1 + (a11 * rhor ^ 2)))))
ddfr = rhor - (fr / dfr)
If Abs(rhor - ddfr) <= 0.000000000001 Then
Range("I" & r).Value = (0.27 * ppr) / (rhor * tpr)
Else
rhor = ddfr
End If
另外,当在 (1) 中计算 Range("B"&row).value 时,我想用它来计算 ppc 来代替 (2) 中的 6760。
解决方案
您的第二个循环似乎没有继续循环的指令。
你可以这样做:
For i = t To 100 **Step someNumber** 'The -Step- argument was used in loop 1, but was omitted in loop 2
或这个:
For i = t To 100
'the rest of your code here
**Next i** 'will increment i by 1
欢迎来到 StackOverflow :)
推荐阅读
- codeigniter - 在 null 上调用成员函数 view()
- django - Django 模板加载器在另一个应用程序中搜索模板
- postgresql - 我们可以在结果集中添加 COUNT 作为列吗?
- php - 添加 session_id 作为参数会导致我在 Apache/PHP 中出现 403 错误?
- deep-learning - 如何在长句中使用bert?
- python - 如何为 discord.py bot 中的命令添加高级订阅?
- java - RSA 公钥到 Node-Forge 公钥
- postgresql - 多个选择查询后,来自 Express 服务器的 Postgres 数据库查询超时
- javascript - Angular 8 - 无法使用指令 - 无法绑定,因为它不是已知属性
- javascript - 有状态的类 Hook React