0

运行此代码时,出现语法错误。我无法理解原因。
我粘贴了我所有的代码。如果你需要,我可以添加数据。
我想编写递归代码以在时间限制下找到所有可能的飞行死记硬背。
find_route 函数应该自己再次运行,直到满足停止条件。(flight_time>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
4

1 回答 1

0

第一行: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...并将相应的“结束功能”更改为“结束子”

于 2015-03-26T23:57:23.137 回答