+ Reply to Thread
Results 1 to 4 of 4

Speed issue

  1. #1
    Registered User
    Join Date
    10-06-2008
    Location
    Odense
    Posts
    27

    Speed issue

    Hi all

    I have written some code that works but its kind of slow-ish. My teacher solved the same problem and his works much faster. I am however not able to view his code.

    The task is to find flight routes from a list of 10000 flight numbers. Routes have to have origin in the cities typed into column E and destination at cities listed in Column F.

    I am using arrays To contain dest. origin and flight numbers.My Code takes about 10 seconds to run my teachers only a second or so....

    Sub FindFlights()

    Dim Origin() As String, Destination() As String, FligtNumber() As String
    Dim Arraylenght As Integer, NumRows As Integer, i As Integer, j As Integer, t As Integer, p As Integer

    With Range("A1")

    Arraylenght = Range(.Offset(1, 0), .End(xlDown)).Rows.Count

    End With

    ReDim Origin(Arraylenght)
    ReDim Destination(Arraylenght)
    ReDim FlightNumber(Arraylenght)

    For i = 1 To Arraylenght

    Origin(i) = Range("A" & i + 1).Value
    Destination(i) = Range("B" & i + 1).Value
    FlightNumber(i) = Range("C" & i + 1).Value

    Next

    With Range("E1")

    NumRows = Range(.Offset(0, 0), .End(xlDown)).Rows.Count

    End With

    t = 1

    For j = 2 To NumRows

    For i = 1 To Arraylenght

    For p = 2 To NumRows

    If Origin(i) = Range("E" & j).Value And Destination(i) = Range("F" & p).Value Then
    t = t + 1
    Range("H" & t).Value = Origin(i)
    Range("I" & t).Value = Destination(i)
    Range("J" & t).Value = FlightNumber(i)
    End If

    Next
    Next

    Next
    Attached Files Attached Files

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Speed issue

    mrosendal,

    For something like this, I highly recommend using the AutoFilter method:
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Speed issue

    or

    Please Login or Register  to view this content.



  4. #4
    Registered User
    Join Date
    10-06-2008
    Location
    Odense
    Posts
    27

    Re: Speed issue

    Thank you Tiger

    Your code is really fast. Even adding more cities does not reduce the speed drastically. Still slightly new at this so its nice to get some tips from the pros... How long have you been programming for ?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1