SUGGEST to get min max date from txt string

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

SUGGEST to get min max date from txt string

Post by sal21 »

I use the tipical free file eof .... to read line by line a txt file.

During the loop a get in a mid postion of string a date form each lines and store the date value it in mYDate var.

How to get the min and max date when the loop is finished?

Peraphs i can store the date value in a collection, in a dictionary...

note:
In txt are approx 350.xxx lines

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: SUGGEST to get min max date from txt string

Post by Rudi »

I'll take a chance to reply...
You can try Dim myDate() as Variant
Then in your loop have a counter like: i=i+1 and populate your variable myDate(i)
Lastly use the LBound(myDate) to extract the Min Date and UBound(myDate) to extract the Max date.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78440
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SUGGEST to get min max date from txt string

Post by HansV »

Try code like this:

Code: Select all

    Dim myDate As Date
    Dim minDate As Date
    Dim maxDate As Date
    Dim strLine As String
    minDate = #1/1/2100#
    maxDate = #1/1/1900#
    Dim f As Integer
    f = FreeFile
    Open "C:\MyFile.txt" For Input As #f
    Do While Not EOF(f)
        Line Input #f, strLine
        myDate = CDate(Mid(strLine, 24, 10))
        If myDate > maxDate Then
            maxDate = myDate
        End If
        If myDate < minDate Then
            minDate = myDate
        End If
    Loop
    Close #f
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: SUGGEST to get min max date from txt string

Post by sal21 »

HansV wrote:Try code like this:

Code: Select all

    Dim myDate As Date
    Dim minDate As Date
    Dim maxDate As Date
    Dim strLine As String
    minDate = #1/1/2100#
    maxDate = #1/1/1900#
    Dim f As Integer
    f = FreeFile
    Open "C:\MyFile.txt" For Input As #f
    Do While Not EOF(f)
        Line Input #f, strLine
        myDate = CDate(Mid(strLine, 24, 10))
        If myDate > maxDate Then
            maxDate = myDate
        End If
        If myDate < minDate Then
            minDate = myDate
        End If
    Loop
    Close #f
Sorry me Hans,
but i'm inattentive...

i have two var

mYDate and mYDate1

based mYDate i need only the min date an based mYDate1 only the max date

User avatar
HansV
Administrator
Posts: 78440
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SUGGEST to get min max date from txt string

Post by HansV »

Sorry, I don't understand.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: SUGGEST to get min max date from txt string

Post by sal21 »

HansV wrote:Sorry, I don't understand.
i get also other value from the each string with myDate1 = CDate(Mid(strLine, 68, 10))

now i need to get, during the loop only the min date with...

myDate = CDate(Mid(strLine, 24, 10))

and i need to get, during the loop only the max date...

myDate1 = CDate(Mid(strLine, 68, 10))

User avatar
HansV
Administrator
Posts: 78440
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SUGGEST to get min max date from txt string

Post by HansV »

OK, try this version:

Code: Select all

    Dim myDate As Date
    Dim myDate1 As Date
    Dim minDate As Date
    Dim maxDate As Date
    Dim strLine As String
    minDate = #1/1/2100#
    maxDate = #1/1/1900#
    Dim f As Integer
    f = FreeFile
    Open "C:\MyFile.txt" For Input As #f
    Do While Not EOF(f)
        Line Input #f, strLine
        myDate = CDate(Mid(strLine, 24, 10))
        If myDate < minDate Then
            minDate = myDate
        End If
        myDate1 = CDate(Mid(strLine, 68, 10))
        If myDate1 > maxDate Then
            maxDate = myDate1
        End If
    Loop
    Close #f
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4353
Joined: 26 Apr 2010, 17:36

Re: SUGGEST to get min max date from txt string

Post by sal21 »

HansV wrote:OK, try this version:

Code: Select all

    Dim myDate As Date
    Dim myDate1 As Date
    Dim minDate As Date
    Dim maxDate As Date
    Dim strLine As String
    minDate = #1/1/2100#
    maxDate = #1/1/1900#
    Dim f As Integer
    f = FreeFile
    Open "C:\MyFile.txt" For Input As #f
    Do While Not EOF(f)
        Line Input #f, strLine
        myDate = CDate(Mid(strLine, 24, 10))
        If myDate < minDate Then
            minDate = myDate
        End If
        myDate1 = CDate(Mid(strLine, 68, 10))
        If myDate1 > maxDate Then
            maxDate = myDate1
        End If
    Loop
    Close #f

Genius!

... to understand me :grin:
... and for the brilliant code :thankyou: