在使用数组时,VBA中的运行时错误5 [英] run time error 5 in VBA excel when working with array

查看:749
本文介绍了在使用数组时,VBA中的运行时错误5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用vba在excel 2007,OS:windows vista上,使用有限差分方案中的运动波方程进行计算。但是,当运行运行时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 '这是斜率
alpha = 1 / n * so ^ 0.5

X = 0
对于i = 0到100
单元格(i + 1,1) = X
u(i,1)= L - so * X
X = X + dx

单元格(i + 1,2)= u(i,1)
Next i



对于j = 0至100
对于i = 1至100
'预测器步骤
u(i ,j + 1)= u(i,j) - α* dt / dx *(u(i + 1,j)^ m - u(i,j)^ m)+(r-f)
$ b'校正步骤

K = u(i,j + 1)^ m - u(i - 1,j + 1)^ m'< ; -----运行错误5在此线上的幸福

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


下一个i
下一个j




End Sub


解决方案

您正在声明变量错误 - 数组应该存储双/单,但默认为变体。请参阅这篇文章。



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


注意使用一个Dim语句声明的变量



VBA允许使用单个Dim
语句声明多个变量,我不喜欢这样的风格理由,但其他人则不喜欢
。但是,请记住变量如何输入
,请考虑以下代码:



Dim J,K,L As Long您可能会认为所有三个变量是
声明为Long类型,不是这样,只有L键入
Long,变量J和K键入Variant,这个声明是
在功能上相当于以下内容:



Dim J As Variant,K As Variant,L As Long您应该为使用Dim语句声明的每个变量使用As Type
修饰符:



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


此外,当我= 99和j = 10,u(99,11),其为j + 1,产生负数。请注意,这并不完全导致问题,因为您可以将负数提升为指数。例如,-5 ^ 3 = -125


I use vba on excel 2007, OS: windows vista, to make calculation using kinematic wave equation in finite difference scheme. But, when it runs the run-time 5 (invalid procedure call or arguments) message appears. I really don't what is going wrong. Anyone can help?

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

解决方案

You are declaring the variables wrong- the array should store a double/single but it is defaulting to a variant. See this article.

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 "

Additionally, when i = 99 and j = 10, u(99,11), which is j+1, produces a negative number. Note that this does not fully cause the problem though, because you can raise negative numbers to exponents. Ex, -5^3 = -125

这篇关于在使用数组时,VBA中的运行时错误5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆