简体   繁体   中英

Recursive Flight Paths Code VBA Syntax Error

When I run this code, I get syntax error. I cannot understand reason.
I paste all my code. if you need, i can add datas.
I want to write recurvise code to find all possible flight rote under time constraint.
find_route function should run again in itself until stop condition satisfied.(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

First line: Function find_route(a As Integer, b As Integer)

find_route is a function so it returns a value. The return type isn't specified so it returns a Variant.

Problem line: find_route(route(l),flight_time)

The function is called but there's nowhere to put the return value. This causes a syntax error because you have to do something with the return value in VBA.

As you never set the return value of the function (which you would do by writing find_route = ... ), you probably want to make this a 'Sub' instead. Subs don't return anything so that line would now be OK.

Just change the first line to Sub find_route... and the corresponding "End Function" to "End Sub"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM