Problem Reading Delimited File

M

Martin Swanston

Hi
As part of an application, I need to intereract with another (in-house)
application's data, so therefore I cannot change the layout of this
data. This data is stored in delimited text files on a network folder.
Basically, each time a new figure becomes available, a new figure
(month) is added to the appropriate text file which, for each line has
13 delimited columns (a year and 12 months) by as many rows as there is
data available.

The text file looks like this:

1963, 3.8, 3.8, 3.8, 3.9, 3.9, 3.9, 3.9, 3.9, 3.9, 3.9, 4.0,
4.2
1964, 4.1, 4.1, 4.0, 4.2, 4.2, 4.2, 4.3, 4.3, 4.3, 4.3, 4.3,
4.3
1965, 4.3, 4.4, 4.3, 4.4, 4.5, 4.5, 4.5, 4.5, 4.6, 4.6, 4.6,
4.7
1966, 4.7, 4.7, 4.7, 4.8, 4.8, 4.8, 4.8, 4.8, 4.9, 4.9, 4.9,
4.8
1967, 4.8, 4.8, 4.8, 4.8, 4.9, 4.9, 5.0, 5.0, 5.1, 5.1, 5.1,
5.1
1968, 5.1, 5.2, 5.2, 5.2, 5.3, 5.3, 5.4, 5.5, 5.5, 5.5, 5.5,
5.5
1969, 5.6, 5.5, 5.5, 5.7, 5.7, 5.8, 5.9, 5.8, 5.9, 5.9, 5.9,
6.0
1970, 6.0, 6.2, 6.2, 6.3, 6.3, 6.4, 6.5, 6.7, 6.7, 6.7, 6.8,
6.9
1971, 6.9, 7.0, 7.0, 7.0, 7.2, 7.2, 7.3, 7.3, 7.4, 7.4, 7.4,
7.5
1972, 7.6, 7.6, 7.7, 7.9, 7.9, 8.0, 8.0, 8.2, 8.4, 8.5, 8.7,
8.7
1973, 8.7, 8.8, 8.8, 9.0, 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.6,
9.7
1974, 9.4, 9.5, 10.0, 10.0, 10.6, 10.8, 11.0, 11.3, 11.5, 11.7, 12.1,
12.6
1975, 12.5, 12.8, 12.8, 13.2, 13.4, 13.6, 14.1, 14.1, 14.5, 14.6, 14.6,
14.9
1976, 15.2, 15.3, 15.3, 15.6, 15.7, 16.0, 16.1, 16.3, 16.4, 16.5, 16.7,
16.7
1977, 16.8, 16.9, 17.0, 17.0, 17.1, 17.3, 17.5, 17.5, 17.7, 17.9, 18.1,
18.3
1978, 18.4, 18.7, 18.6, 19.2, 19.3, 19.9, 20.0, 19.9, 20.3, 20.6, 20.6,
20.7
1979, 20.6, 21.5, 21.5, 21.8, 21.9, 22.6, 23.2, 23.2, 23.2, 24.0, 24.5,
24.7
1980, 24.8, 25.5, 25.8, 26.4, 26.6, 27.5, 27.6, 28.2, 29.3, 28.8, 29.1,
29.6
1981, 29.4, 29.6, 29.5, 30.0, 30.1, 30.7, 31.0, 31.9, 32.1, 32.2, 32.4,
32.5
1982, 32.5, 33.0, 32.8, 33.2, 33.2, 33.7, 34.5, 34.4, 34.2, 34.6, 35.1,
35.0
1983, 35.4, 36.1, 35.7, 36.2, 36.5, 36.6, 37.0, 36.9, 37.0, 37.4, 37.5,
37.7
1984, 37.9, 38.2, 37.6, 38.2, 38.3, 38.5, 39.0, 39.0, 39.4, 40.4, 39.9,
40.0
1985, 40.6, 40.8, 40.9, 41.8, 41.6, 41.9, 42.4, 42.5, 43.3, 42.9, 43.3,
43.5
1986, 43.9, 44.2, 44.4, 45.3, 44.8, 45.3, 45.9, 46.0, 46.0, 46.4, 46.9,
46.8
1987, 47.2, 47.6, 47.3, 48.2, 48.6, 48.8, 49.5, 49.4, 49.7, 50.2, 50.9,
50.9
1988, 51.2, 51.5, 51.4, 52.1, 52.5, 52.9, 53.7, 53.8, 54.3, 54.8, 55.3,
56.2
1989, 55.9, 56.4, 56.2, 57.2, 57.3, 57.8, 58.5, 58.4, 59.5, 60.2, 60.4,
60.3
1990, 61.0, 61.4, 61.4, 62.6, 63.2, 64.0, 64.4, 64.7, 65.2, 65.6, 66.3,
66.6
1991, 66.7, 67.0, 66.7, 67.8, 68.4, 68.7, 69.0, 69.7, 70.0, 70.3, 71.1,
70.9
1992, 71.5, 71.9, 72.3, 72.0, 72.6, 72.9, 73.1, 73.2, 73.7, 74.1, 74.0,
74.3
1993, 74.3, 74.4, 73.9, 74.7, 75.0, 74.9, 75.5, 75.6, 75.8, 76.0, 76.4,
76.4
1994, 76.6, 77.1, 77.0, 76.9, 77.6, 78.0, 78.1, 78.5, 78.5, 78.9, 79.0,
79.2
1995, 79.4, 79.6, 79.9, 79.9, 80.1, 80.2, 80.3, 80.5, 80.8, 81.2, 81.4,
81.5
1996, 81.7, 82.1, 82.2, 82.6, 82.6, 83.0, 83.3, 83.6, 84.3, 84.2, 84.5,
84.9
1997, 85.2, 85.1, 86.0, 85.7, 86.0, 86.2, 86.8, 87.3, 87.8, 88.1, 88.6,
89.1
1998, 89.2, 89.4, 90.1, 90.7, 91.2, 90.7, 91.7, 91.7, 92.3, 92.5, 92.8,
92.8
1999, 93.1, 93.7, 94.1, 94.4, 95.0, 95.5, 95.8, 96.2, 96.6, 97.3, 97.6,
98.6
2000, 98.8, 98.7, 98.9, 98.7, 98.8, 99.2,
99.5,100.3,100.7,101.3,101.9,103.3
2001,103.2,103.6,103.7,103.9,104.0,104.3,104.4,104.8,105.0,105.1,105.2,1
05.8
2002,106.3,106.9,106.7,108.0,107.9,108.2,108.4,108.6,108.8,109.0,109.6,1
09.5
2003,109.8,109.9,111.4,110.8,111.3,111.6,112.3,112.4,112.8,113.0,113.1,1
13.2

The idea is the user enters a date into a userform, that date is then
passed to my code below and this returns the figure for the year and
month, or if not yet available, the latest available figure. The problem
I'm having is where the month required is 01/04 but the last available
yield is 12/03. It's probably really straightforward to change, but I
just can't get my head around what needs to change. So with the above
data, if the date "31/01/2004" is passed to the procedure, it would
return 76.8 (the latest available figure) (the same would apply for any
date in the future), yet if "30/06/2003" was passed, then it would
return 77.6.
Here's my code:


code:
------------------------------------------------------------------------
--------

Sub ReadNAEIndices(DateToLookFor As String)
NAELogFile="C:\TEMP\AVEARN.DAT"
Dim FileNo As Integer
Dim LastAvailableValue As Double
FileNo = FreeFile()
Open NAELogFile For Input Access Read Shared As #FileNo
Do
On Error Resume Next
For IndCounter = 1 To 12 Step 1
IndValue(IndCounter) = 0
Next IndCounter
Input #FileNo, IndDate, IndValue(1), IndValue(2), _
IndValue(3), IndValue(4), IndValue(5),
IndValue(6), _
IndValue(7), IndValue(8), IndValue(9),
IndValue(10), _
IndValue(11), IndValue(12)
If EOF(FileNo) Then
For IndCounter = 1 To 12 Step 1
If CDbl(IndValue(IndCounter)) = 0
Then
LastAvailableValue =
IndValue(IndCounter - 1)
Exit For
End If
Next IndCounter
If CInt(IndDate) <=
Year(DateToLookFor) Then _
IndValue(Month(DateToLookFor)) =
LastAvailableValue
Exit Do
End If
On Error GoTo 0
Loop Until CInt(IndDate) = Year(DateToLookFor)
Close #FileNo
End Sub

------------------------------------------------------------------------
--------

If I have omitted any required info, please let me know.
Regards, and thanks in advance
Martin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
B

Bernie Deitrick

Martin,

You never store the last available value properly. Change:

Input #FileNo, IndDate, IndValue(1), IndValue(2), _
IndValue(3), IndValue(4), IndValue(5), IndValue(6), _
IndValue(7), IndValue(8), IndValue(9), IndValue(10), _
IndValue(11), IndValue(12)

If EOF(FileNo) Then
For IndCounter = 1 To 12 Step 1
If CDbl(IndValue(IndCounter)) = 0 Then
LastAvailableValue = IndValue(IndCounter - 1)
Exit For
End If
Next IndCounter

If CInt(IndDate) <= Year(DateToLookFor) Then _
IndValue(Month(DateToLookFor)) = LastAvailableValue
Exit Do
End If

To:

Input #FileNo, IndDate, IndValue(1), IndValue(2), _
IndValue(3), IndValue(4), IndValue(5), IndValue(6), _
IndValue(7), IndValue(8), IndValue(9), IndValue(10), _
IndValue(11), IndValue(12)

LastAvailableValue = CDbl(IndValue(12))
For IndCounter = 1 To 12 Step 1
If CDbl(IndValue(IndCounter)) = 0 Then
LastAvailableValue = IndValue(IndCounter - 1)
Exit For
End If
Next IndCounter

If EOF(FileNo) Then
If CInt(IndDate) <= Year(DateToLookFor) Then _
IndValue(Month(DateToLookFor)) = LastAvailableValue
Exit Do
End If

HTH,
Bernie
MS Excel MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top