[急][求救]关于vba 函数递归调用的问题
在excel代码里面想实现函数的递归调用,但运行下来总是报错,想请问下各位大虾这是什么问题。谢谢啦是递归调用本身存在问题
还是当中涉及单元格数据的修改错误呢
代码如下:
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
--------------------编程问答-------------------- 是Excel的Cell试用出错,单元格为空,进行数值计算时转换出错。 --------------------编程问答-------------------- 谢谢。那我用val()加一个转换的过程是不是就能解决呢 --------------------编程问答--------------------
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语句可得到结果,你看看对否? --------------------编程问答-------------------- 恩。可以了。多谢啦。 --------------------编程问答-------------------- 如果val()有效,就用它吧。
on error resume next 是把错误屏蔽了。如果不是这个错误,而是别的错误也会屏蔽掉的 --------------------编程问答-------------------- 来晚了
补充:VB , VBA