How to speed-up my code

blelli

Board Regular
Joined
Jul 21, 2013
Messages
73
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
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top