How to Add elements to my Array?

blelli

Board Regular
Joined
Jul 21, 2013
Messages
73
Dears,
I've the following Macro which is responsible for finding the airport into a TXT. file. It's working perfectly, when the airport is found the macro returns the airport information, at the first line.

Sub FindAirportDep()
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\FMS ATR\DataBase\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 100)
For i = 0 To UBound(X)
If InStr(1, X(i), DepAirport, 1) > 0 Then
n = n + 1: Y = Split(X(i), delim)


DepNameAirport = Y(2)
DepLatitude = Y(3)
DepLongitude = Y(4)
DepElevation = Y(5)

Do While lin <> ""


Loop

End If
Next i

End Sub

But an airport may have more than 2 runways... sometimes 5, up to 10 runways... an in my database each line starting with an "R" represents a runway for that specific airport. The last line is an empty line, and after that we will have another airport...

So, how can add to my code the function to collect all runway information from that specifc airport?

Check my database format:

A|24AZ|PLEASANT VALLEY|34158097|-110935403|5688
R|06|60|3950|0|0|0|34157715|-110937309|5688|300|40
R|24|240|3950|0|0|0|34161064|-110924858|5688|300|40


A|24MO|MISTWOOD|38133364|-92833522|760
R|07|70|2800|0|0|0|38132309|-92838211|760|300|40
R|25|250|2800|0|0|0|38134422|-92828827|760|300|40



A|25NV|PARKER CARSON|39201578|-119683442|4939
R|06|44|815|0|0|0|39201025|-119684689|4939|300|40
R|18|180|2200|0|0|0|39201578|-119683442|4900|300|50
R|24|224|815|0|0|0|39202133|-119682192|4937|300|40
R|36|360|2200|0|0|0|39201578|-119683442|4900|300|50
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try this. Note that I've appended vbCrLf to the allData string to make the loop termination check easier.
Code:
Public Sub FindAirportDep()

    Dim fn As String, delimiter As String
    Dim allData As String
    Dim lines As Variant
    Dim i As Long
    Dim DepAirport As String
    Dim DepAirportName, DepLatitude, DepLongitude, DepElevation
    Dim fields As Variant, f As Long
    
    fn = "C:\path\to\Airports.txt" ' Change here (File Path)
    
    delimiter = "|"
    allData = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll & vbCrLf
    
    lines = Split(allData, vbCrLf)
    
    'DepAirport = "PLEASANT VALLEY"
    DepAirport = "PARKER CARSON"
    
    DepAirportName = ""
    
    i = 0
    While i < UBound(lines) And DepAirportName = ""
        
        If InStr(1, lines(i), DepAirport, vbTextCompare) > 0 Then

            fields = Split(lines(i), delimiter)
            DepAirportName = fields(2)
            DepLatitude = fields(3)
            DepLongitude = fields(4)
            DepElevation = fields(5)
            
            Do
                i = i + 1
                fields = Split(lines(i), delimiter)
                For f = 0 To UBound(fields)
                    Debug.Print fields(f); " ";
                Next
                Debug.Print
            Loop While i < UBound(lines) And lines(i + 1) <> ""
            
        End If
            
        i = i + 1
        
    Wend

End Sub
Please, always use CODE tags when posting VBA code - the # icon in the message editor.
 
Upvote 0
That's perfect. Thank you...

Another conceptual question...
Imagine that I have 3 arrays.

The first one called departure,
The second one callec route,
The third one called arrival.

How can I merge all three arrays together creating the fourth array called flight?

How can I add just another entry into an array between two points?
Imagine that my route has the points AAA, BBB, CCC and I would like to insert the point XXX between AAA and BBB... how can I do it?

Thank you so much!
Have a nice day.
 
Upvote 0
For the first question, declare a multi-dimensional array, probably a 2-dimensional array, maybe like this:
Code:
    Dim flight(1 To 3, 1 To 10)
    
    flight(1, 1) = "xxx" 'departure for flight 1
    flight(2, 1) = "yyy" 'route for flight 1
    flight(3, 1) = "zzz" 'arrival for flight 1
    
    ':
    ':
    
    flight(1, 10) = "xxx" 'departure for flight 10
    flight(2, 10) = "yyy" 'route for flight 10
    flight(3, 10) = "zzz" 'arrival for flight 10
For the second question, create an 'empty' slot in the array by moving the elements down one index from the point where you want to insert the new element. You might need to declare a dynamic array and use ReDim Preserve - see https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/redim-statement.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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