gpt4 book ai didi

vba - 使用数组时 VBA excel 中的运行时错误 5

转载 作者:行者123 更新时间:2023-12-03 09:08:41 35 4
gpt4 key购买 nike

我在excel 2007,OS:windows vista上使用vba,在有限差分方案中使用运动波动方程进行计算。但是,当它运行运行时 5(无效的过程调用或参数)消息时出现。我真的不知道出了什么问题。任何人都可以帮忙吗?

Sub kwave()

Dim u(500, 500), yy(500, 500), alpha, dt, dx, m, n, so, r, f, X, L, K As Single

Dim i, j As Integer

dx = 0.1
dt = 0.01
L = 10
m = 5 / 3
r = 1
f = 0.5
n = 0.025
so = 0.1 'this is slope
alpha = 1 / n * so ^ 0.5

X = 0
For i = 0 To 100
Cells(i + 1, 1) = X
u(i, 1) = L - so * X
X = X + dx

Cells(i + 1, 2) = u(i, 1)
Next i



For j = 0 To 100
For i = 1 To 100
'predictor step
u(i, j + 1) = u(i, j) - alpha * dt / dx * (u(i + 1, j) ^ m - u(i, j) ^ m) + (r - f) * dt

'corrector step

K = u(i, j + 1) ^ m - u(i - 1, j + 1) ^ m '<<<<----- RUNTIME ERROR 5 HAPPENS AT THIS LINE

yy(i, j + 1) = 0.5 * ((yy(i, j) + u(i, j + 1)) - alpha * dt / dx * K + (r - f) * dt)


Next i
Next j




End Sub

最佳答案

您声明变量错误 - 数组应该存储双/单,但它默认为一个变体。见这篇文章。

http://www.cpearson.com/excel/declaringvariables.aspx -

"Pay Attention To Variables Declared With One Dim Statement

VBA allows declaring more than one variable with a single Dim statement. I don't like this for stylistic reasons, but others do prefer it. However, it is important to remember how variables will be typed. Consider the following code:

Dim J, K, L As Long You may think that all three variables are declared as Long types. This is not the case. Only L is typed as a Long. The variables J and K are typed as Variant. This declaration is functionally equivalent to the following:

Dim J As Variant, K As Variant, L As Long You should use the As Type modifier for each variable declared with the Dim statement:

Dim J As Long, K As Long, L As Long "



此外,当 i = 99 且 j = 10 时,u(99,11),即 j+1,产生一个负数。请注意,这并不能完全导致问题,因为您可以将负数提高到指数。例如,-5^3 = -125

关于vba - 使用数组时 VBA excel 中的运行时错误 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28390833/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com