Private Sub cmdCreate_Click() 'Open text files Open "c:\temp\GDP.txt" For Input As #1 Open "C:\temp\GDP2.txt" For Output As #2 'Declare variables Dim mypos As Integer Dim TextLine As String Dim year As Integer year = 1998 Dim s As String Dim flag As Boolean Dim counter As Integer Dim d As Double Dim Myline As String 'Write XML tags Print #2, "" Print #2, "" Print #2, "" Print #2, "" 'Loop to write year Do While year < 2009 s = "" & year & "" Print #2, s year = year + 1 Loop 'Write XML tags Print #2, "" Print #2, "" Print #2, "GDP" 'Initialize flag and counter flag = False counter = 0 'Loop through entire file Do While Not EOF(1) Line Input #1, TextLine ' current line 'As soon as there is a match, mypos is 1 and this block is no longer executed If flag = False Then mypos = InStr(1, TextLine, "1998", vbTextCompare) End If 'After 1998 has been found, get the value If mypos = 1 Then flag = True 'Take the right side of the line of text, removing first 11 characters Myline = Right(TextLine, Len(TextLine) - (11)) d = d + Val(Myline) counter = counter + 1 If counter = 4 Then d = d / 4 Print #2, "" & d & "" d = 0 counter = 0 End If End If Loop 'Write the XML tags Print #2, "" Print #2, "" Print #2, "" 'Close the files Close #1 Close #2 End Sub