1

我正在尝试在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 编译错误:预期的子、函数或属性”

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

4

1 回答 1

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
于 2013-07-22T01:43:36.443 回答