SteveNash88
New Member
- Joined
- Jul 26, 2011
- Messages
- 2
Hi,
Started learning VBA a couple of days ago and I am trying to come up with practical examples where I can learn as much as possible. One thing I want to be able is to import a text file and also sort it, i.e.
"practice.txt" :
Peter - 20
David - 25
.
.
.
For this small example I have written
Option Explicit
Private Sub CommandButton1_Click()
Dim myFile As String, text As String, textline As String, posPeter As Integer, posAndreas As Integer
myFile = "C:\practice.txt"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
posPeter = InStr(text, "Peter")
posAndreas = InStr(text, "Andreas")
Range("A1").Value = Mid(text, posPeter, 5)
Range("A2").Value = Mid(text, posAndreas, 7)
Range("B1").Value = Mid(text, posPeter + 8, 2)
Range("B2").Value = Mid(text, posAndreas + 10, 2)
End Sub
______________________________________________
As you can see there are a few problems with this method, namely
a) I have to insert the names (Peter and Andreas) manually
b) I am forced to enter the number of letters in each name manually
c) I am forced to enter the position of each number (20 and 25) manually
The code above imports the names and ages and gives
...A ...B
1 Peter 20
2 Andreas 25
in excel which is exactly what I want. The method sucks if I have many names and ages in the text file because of the things I mentioned above.
I am thinking of a for loop but I do not know how to proceed. Think of a file with say 100 names and a corresponding number (age) to each name.
Any ideas?
Started learning VBA a couple of days ago and I am trying to come up with practical examples where I can learn as much as possible. One thing I want to be able is to import a text file and also sort it, i.e.
"practice.txt" :
Peter - 20
David - 25
.
.
.
For this small example I have written
Option Explicit
Private Sub CommandButton1_Click()
Dim myFile As String, text As String, textline As String, posPeter As Integer, posAndreas As Integer
myFile = "C:\practice.txt"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
posPeter = InStr(text, "Peter")
posAndreas = InStr(text, "Andreas")
Range("A1").Value = Mid(text, posPeter, 5)
Range("A2").Value = Mid(text, posAndreas, 7)
Range("B1").Value = Mid(text, posPeter + 8, 2)
Range("B2").Value = Mid(text, posAndreas + 10, 2)
End Sub
______________________________________________
As you can see there are a few problems with this method, namely
a) I have to insert the names (Peter and Andreas) manually
b) I am forced to enter the number of letters in each name manually
c) I am forced to enter the position of each number (20 and 25) manually
The code above imports the names and ages and gives
...A ...B
1 Peter 20
2 Andreas 25
in excel which is exactly what I want. The method sucks if I have many names and ages in the text file because of the things I mentioned above.
I am thinking of a for loop but I do not know how to proceed. Think of a file with say 100 names and a corresponding number (age) to each name.
Any ideas?