运行此代码时,会出现语法错误。我无法理解原因
我粘贴了所有的代码。如果你需要,我可以添加数据
我想写递归代码,在时间限制下找到所有可能的死记硬背的航班
find_route函数应在自身中再次运行,直到满足停止条件。(飞行时间>480)
Function find_route(a As Integer, b As Integer) 'a start node, b start time
flight_time = 0
route(0) = a
l = 0
If flight_time <= 480 Then
If temp_flight_time(a, b) + flight_time <= 480 Then
l = l + 1
route(l) = next_destination(a, b)
flight_time = temp_flight_time(a, b) + flight_time
find_route(route(l),flight_time)'*******syntax error at this row******
End If
Else
Cells(7, 1).Select
For i = 0 To 30
ActiveCell.Value = route(i)
ActiveCell.Offset(0, 1).Select
Next
Cells(1, 1).Select
Exit Function
End If
End Function
Function temp_flight_time(a As Integer, b As Integer)
temp_flight_time = get_flight_time(a, next_destination(a, b))
End Function
Function get_flight_time(a As Integer, b As Integer) 'a from, b to
Cells(2, 1).Select
Dim ae As Integer
Dim be As Integer
For i = 1 To 50
ae = ActiveCell.Value 'From
be = ActiveCell.Offset(0, 1).Value 'To
If a = ae And b = be Then
get_flight_time = ActiveCell.Offset(0, 4).Value 'Flight Time
Exit For
Else
ActiveCell.Offset(1, 0).Select
End If
Next
End Function
Function next_destination(a As Integer, b As Integer)
Cells(2, 1).Select
Dim ae As Integer
Dim be As Integer
For i = 1 To 50
ae = ActiveCell.Value 'To
be = ActiveCell.Offset(0, 2).Value 'Departure
If a = ae And b <= be Then
next_destination = ActiveCell.Offset(0, 1).Value
Exit For
Else
ActiveCell.Offset(1, 0).Select
End If
Next
End Function
第一行:Function find_route(a As Integer, b As Integer)
find_route
是一个函数,因此它返回一个值。未指定返回类型,因此它返回一个Variant。
问题行:find_route(route(l),flight_time)
函数已被调用,但返回值无处可放。这会导致语法错误,因为必须对VBA中的返回值执行某些操作。
由于您从未设置函数的返回值(您可以通过编写find_route = ...
来实现),因此您可能希望将其设置为"Sub"。Subs不会返回任何东西,所以这条线现在可以了。
只需将第一行更改为Sub find_route...
,并将相应的"End Function"更改为"End Sub"