How to speed-up my code

blelli

Board Regular
Joined
Jul 21, 2013
Messages
63
Dears,

Is it possible to speed-up my code?

Sub test()


Dim fn As String, txt As String, delim As String, a() As String
Dim i As Long, ii As Long, iii As Long, n As Long, x, y
fn = "C:\Users\bruno\Desktop\Airports.txt" ' ?Change here (File Path)
delim = "|" ' ?Change here (delimiter)
temp = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
x = Split(temp, vbCrLf)

ReDim a(1 To UBound(x) + 1, 1 To 15)
For i = 0 To UBound(x)
If InStr(1, x(i), "SBKP", 1) > 0 Then

n = n + 1
y = Split(x(i), delim)

For m = 0 To UBound(y)
a(n, m + 1) = y(m)
Next

Do While InStr(1, x(i + 1), "R|", 1) > 0

n = n + 1:
y = Split(x(i + 1), delim)
For ii = 0 To UBound(y)
a(n, ii + 1) = y(ii)
Next
i = i + 1

Loop

End If
Next

End Sub
 

Some videos you may like

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

btorrean

Board Regular
Joined
Dec 2, 2011
Messages
71
What is the purpose of your code? What are you trying to accomplish? A little more info might help us go a long way in helping you. Thanks.

Brian
 

blelli

Board Regular
Joined
Jul 21, 2013
Messages
63
Hello Brian,

The purpose of this code if to access a TXT file with around 60.000 lines of information, collect the "range" of a specific airport, like "SBKP" and return all the info about this airport stored into an array or collection.

The information is presented like:
...
A|SBKG|PRESIDENTE JOAO SUASSUNA|-7269167|-35895000|1646
R|15|147|5249|1|109100|147|-7265528|-35901833|1608|300|50
R|33|327|5249|0|0|0|-7273833|-35889972|1646|300|50


A|SBKP|VIRACOPOS INTL|-23006944|-47134444|2170
R|15|149|10630|1|110300|148|-22998467|-47147003|2139|300|51
R|33|329|10630|0|0|0|-23016372|-47121997|2170|300|50


A|SBLE|HORACIO DE MATTOS|-12482222|-41276944|1660
R|14|140|6831|0|0|0|-12482222|-41276944|1660|300|50
R|32|320|6831|0|0|0|-12482222|-41276944|1660|300|50
...

And the result, in this example, will be:
A|SBKP|VIRACOPOS INTL|-23006944|-47134444|2170
R|15|149|10630|1|110300|148|-22998467|-47147003|2139|300|51
R|33|329|10630|0|0|0|-23016372|-47121997|2170|300|50

Sometimes the airport have more than 2 runways (look at the begining of each line... every line starting witl "R|" represents an airport runway.

Actually the provided code is taking 0.22 seconds to return all necessary info.


Thank you so much for your assistance,
Best regards

Bruno Lelli
 

Watch MrExcel Video

Forum statistics

Threads
1,109,336
Messages
5,528,105
Members
409,802
Latest member
joeino

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top