[急][求救]关于vba 函数递归调用的问题
时间:2011-10-09
来源:互联网
是递归调用本身存在问题
还是当中涉及单元格数据的修改错误呢
代码如下:
Dim num(100, 5) As Integer
Dim k As Integer
Sub bb()
Dim p As Integer
k = 1
p = aa(120, 100, 1, 0, 0)
MsgBox p
End Sub
Function aa(m, n, x, y, z) As Integer
Dim i As Integer
Dim j As Integer
Dim r As Integer
Dim s(10) As Integer
Dim t As Double
If m - n <= 3 Then
k = k + 1
Cells(k, 1) = m - 2 * x
Cells(k, 2) = 130 - m - 2 * x
For i = 1 To x - 1
Cells(k, 2 * i + 1) = num(i, 1)
Cells(k, 2 * i + 2) = num(i, 2)
Next i
aa = k
ElseIf x = 6 Then
k = k + 1
Cells(k, 1) = -10
Cells(k, 2) = -10
For i = 1 To x - 1
Cells(k, 2 * i + 1) = num(i, 1)
Cells(k, 2 * i + 2) = num(i, 2)
Next i
aa = k
ElseIf x < 5 And m - n > 3 Then
num(x + 1, 1) = 1
num(x + 1, 2) = 1
s(1) = aa(m, n, x + 1, y + 1, z + 1)
num(x + 1, 1) = 1
num(x + 1, 2) = 0
s(2) = aa(m - 6, n, x + 1, y + 1, z)
num(x + 1, 1) = 0
num(x + 1, 2) = 1
s(3) = aa(m, n + 6, x + 1, y, z + 1)
num(x + 1, 1) = 0
num(x + 1, 2) = 0
s(4) = aa(m - 4, n + 4, x + 1, y, z)
k = k + 1
t = 0.4 * (z + 1) / (0.4 * (z + 1) + 0.6 * (x - z))
If t * Cells(s(1), 2) + (1 - t) * Cells(s(2), 2) >= t * Cells(s(3), 2) + (1 - t) * Cells(s(4), 2) Then
Cells(k, 2) = t * Cells(s(1), 2) + (1 - t) * Cells(s(2), 2)
Else
Cells(k, 2) = t * Cells(s(3), 2) + (1 - t) * Cells(s(4), 2)
End If
t = 0.6 * (y + 1) / (0.6 * (y + 1) + 0.4 * (x - y))
If t * Cells(s(1), 1) + (1 - t) * Cells(s(3), 1) >= t * Cells(s(2), 1) + (1 - t) * Cells(s(4), 1) Then
Cells(k, 1) = t * Cells(s(1), 1) + (1 - t) * Cells(s(3), 1)
Else
Cells(k, 1) = t * Cells(s(2), 1) + (1 - t) * Cells(s(4), 1)
End If
For i = 1 To x - 1
Cells(k, 2 * i + 1) = num(i, 1)
Cells(k, 2 * i + 2) = num(i, 2)
Next i
aa = k
End If
End Function
作者: santiago5365 发布时间: 2011-10-09
作者: Veron_04 发布时间: 2011-10-09
作者: santiago5365 发布时间: 2011-10-09
Dim num(100, 5) As Integer Dim k As Integer Sub bb() Dim p As Integer k = 1 p = aa(120, 100, 1, 0, 0) MsgBox p End Sub Function aa(m, n, x, y, z) As Integer Dim i As Integer Dim j As Integer Dim r As Integer Dim s(10) As Integer Dim t As Double On Error Resume Next If m - n <= 3 Then k = k + 1 Cells(k, 1) = m - 2 * x Cells(k, 2) = 130 - m - 2 * x For i = 1 To x - 1 Cells(k, 2 * i + 1) = num(i, 1) Cells(k, 2 * i + 2) = num(i, 2) Next i aa = k ElseIf x = 6 Then k = k + 1 Cells(k, 1) = -10 Cells(k, 2) = -10 For i = 1 To x - 1 Cells(k, 2 * i + 1) = num(i, 1) Cells(k, 2 * i + 2) = num(i, 2) Next i aa = k ElseIf x < 5 And m - n > 3 Then num(x + 1, 1) = 1 num(x + 1, 2) = 1 s(1) = aa(m, n, x + 1, y + 1, z + 1) num(x + 1, 1) = 1 num(x + 1, 2) = 0 s(2) = aa(m - 6, n, x + 1, y + 1, z) num(x + 1, 1) = 0 num(x + 1, 2) = 1 s(3) = aa(m, n + 6, x + 1, y, z + 1) num(x + 1, 1) = 0 num(x + 1, 2) = 0 s(4) = aa(m - 4, n + 4, x + 1, y, z) k = k + 1 t = 0.4 * (z + 1) / (0.4 * (z + 1) + 0.6 * (x - z)) If t * Cells(s(1), 2) + (1 - t) * Cells(s(2), 2) >= t * Cells(s(3), 2) + (1 - t) * Cells(s(4), 2) Then Cells(k, 2) = t * Cells(s(1), 2) + (1 - t) * Cells(s(2), 2) Else Cells(k, 2) = t * Cells(s(3), 2) + (1 - t) * Cells(s(4), 2) End If t = 0.6 * (y + 1) / (0.6 * (y + 1) + 0.4 * (x - y)) If t * Cells(s(1), 1) + (1 - t) * Cells(s(3), 1) >= t * Cells(s(2), 1) + (1 - t) * Cells(s(4), 1) Then Cells(k, 1) = t * Cells(s(1), 1) + (1 - t) * Cells(s(3), 1) Else Cells(k, 1) = t * Cells(s(2), 1) + (1 - t) * Cells(s(4), 1) End If For i = 1 To x - 1 Cells(k, 2 * i + 1) = num(i, 1) Cells(k, 2 * i + 2) = num(i, 2) Next i aa = k End If End Function
在aa中加入 On Error Resume Next语句可得到结果,你看看对否?
作者: Veron_04 发布时间: 2011-10-09
作者: santiago5365 发布时间: 2011-10-09
on error resume next 是把错误屏蔽了。如果不是这个错误,而是别的错误也会屏蔽掉的
作者: king06 发布时间: 2011-10-09
作者: z_wenqian 发布时间: 2011-10-09
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28