VBA excel编译错误:预期的子,函数或属性

时间:2013-07-22 01:32:09

标签: vba

我试图在excel VBA中运行Rungenkutta差异问题  程序如下

Sub Rungenkutta()


Dim Noofitrations As Integer
Dim n As Integer
Dim ii As Integer
Noofitrations = Cells(2, 10)
n = Cells(3, 10)
h = Cells(3, 4)
col = 8
x0 = Cells(col, 9)
y0 = Cells(col, 10)

For i = 1 To Noofitrations

 Cells(7, 3) = x0
 Cells(7, 4) = y0
 xn = x0 + h
 yn = y0 + Cells(18, 3)

 ii i Mod n
 If ii = 0 Then
    col = col + 1
    Cells(col, 9) = xn
    Cells(col, 10) = yn
 End If
 x0 = xn
 y0 = yn

Next


End Sub

但在运行时我得到“VBA excel编译错误:预期的子,函数或属性”

我不明白我该怎么做才能运行程序

1 个答案:

答案 0 :(得分:2)

您的问题在于Mod运算符。 VBA无法识别您提供的语法。

以下是Mod运算符的一些文档 - http://msdn.microsoft.com/en-us/library/se0w9esz.aspx

Th Mod运算符是二元运算符,需要一个左右参数。

您需要更改

ii i Mod n   

ii = i Mod n

以下是您提供的修订示例。

Sub Rungenkutta()


Dim Noofitrations As Integer
Dim n As Integer
Dim ii As Integer
Noofitrations = Cells(2, 10)
n = Cells(3, 10)
h = Cells(3, 4)
col = 8
x0 = Cells(col, 9)
y0 = Cells(col, 10)

For i = 1 To Noofitrations

Cells(7, 3) = x0
Cells(7, 4) = y0
xn = x0 + h
yn = y0 + Cells(18, 3)

ii = i Mod n
If ii = 0 Then
   col = col + 1
   Cells(col, 9) = xn
   Cells(col, 10) = yn
End If
x0 = xn
y0 = yn

Next


End Sub