+ Reply to Thread
Results 1 to 6 of 6

Excel - Date comparision in VBA

  1. #1
    Silencer116
    Guest

    Excel - Date comparision in VBA

    Hey all,

    I got this problem.
    Every day i have a Macro creating a logfile, that contains the date of
    today.

    Like "Test" & Cstr(Date) & ".txt".

    This works okay and stuff. But now i would like to have a logfile that
    is 7 days or older; deleted.

    That made me creating the following:

    ExpiredDate =3D Date - 7
    Expired =3D Date >=3D ExpiredDate

    this would result in:

    Kill "H:/Test" & Cstr(Expired) & ".txt"

    But this doesn=B4t seem to work.. Could someone help me?


  2. #2
    Ivan Raiminius
    Guest

    Re: Excel - Date comparision in VBA

    Hi,

    Expired seems to me to be boolean false or true, so you get something
    like:
    Kill "H:/TestTrue.txt" (you attempt to delete "H:/TestTrue.txt").

    Regards,
    Ivan


  3. #3
    Silencer116
    Guest

    Re: Excel - Date comparision in VBA

    uh that=B4s not quite what i mean.

    i mean like, every day there is a logfile created with the today=B4s
    date.
    these files will remain as they are not automatically deleted.

    I want a macro to delete logfiles that are older than 7 days, resulting
    in that there are always only 7 existing logfiles, from today to 7 days
    back.

    so when a logfile doesn=B4t compare with one of these 7 logfiles, it
    should be deleted


  4. #4
    Bob Phillips
    Guest

    Re: Excel - Date comparision in VBA

    Are you sure that the CStr(Expired) is creating a date compatible with your
    file name. On my system, it returns 25/05/2006 as an example, but filenames
    cannot contain /.

    Probably best to use

    Kill "H:/Test" & Format(Date - 7,"yyyymmdd") & ".txt"

    just change the format to how you store them.


    --
    HTH

    Bob Phillips

    (remove xxx from email address if mailing direct)

    "Silencer116" <silencer116@hotmail.com> wrote in message
    news:1148460042.592428.36610@y43g2000cwc.googlegroups.com...
    uh that´s not quite what i mean.

    i mean like, every day there is a logfile created with the today´s
    date.
    these files will remain as they are not automatically deleted.

    I want a macro to delete logfiles that are older than 7 days, resulting
    in that there are always only 7 existing logfiles, from today to 7 days
    back.

    so when a logfile doesn´t compare with one of these 7 logfiles, it
    should be deleted



  5. #5
    Silencer116
    Guest

    Re: Excel - Date comparision in VBA

    Hey Bob,

    Thanks for your reply. The date=B4s format that is stored is " 24-5-2006
    "=2E This is allowed.

    But about your statement, does this also deletes the files that are
    older than 7 days?
    Like, not only the file that is exactly 7 days old, but also files that
    could be 9 days , 20 days or even a month old.


  6. #6
    Bob Phillips
    Guest

    Re: Excel - Date comparision in VBA

    No, you need a loop for that. Something like

    Sub DeleteOldFiles()
    Dim oFSO As Object
    Dim oFolder As Object
    Dim oFile As Object
    Dim dteFile As Date

    Set oFSO = CreateObject("Scripting.FileSystemobject")
    Set oFolder = oFSO.getfolder("C:\MyTest")
    For Each oFile In oFolder.Files
    If Left(oFile.Name, 4) = "Test" Then
    On Error Resume Next
    dteFile = DateValue(Replace(Replace(oFile.Name, ".xls", ""),
    "Test", ""))
    On Error GoTo 0
    If dteFile <> 0 And dteFile <= Date - 7 Then
    Kill oFile
    End If
    End If
    Next oFile

    Set oFile = Nothing
    Set oFolder = Nothing
    Set oFSO = Nothing

    End Sub


    --
    HTH

    Bob Phillips

    (remove xxx from email address if mailing direct)

    "Silencer116" <silencer116@hotmail.com> wrote in message
    news:1148462820.588127.263020@y43g2000cwc.googlegroups.com...
    Hey Bob,

    Thanks for your reply. The date´s format that is stored is " 24-5-2006
    ". This is allowed.

    But about your statement, does this also deletes the files that are
    older than 7 days?
    Like, not only the file that is exactly 7 days old, but also files that
    could be 9 days , 20 days or even a month old.



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1