• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User


  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Importing .TXT Files Based On The Date They Were Created?

Is there code that exists that could determine if the creation of a text file? I searched online but couldn't find anything so far.

I am importing a large text file. I have a reset button which basically clears the whole sheet and reimports this text file. To make things faster, I was hoping to incorporate some type of code that only imports this text file if it's new.

I was thinking something along the lines of...

Check ThisWorkbook.Path & "File Data 2 Import.txt"

If file creation date is not older than say like a minute


Skip Import

Then that way it would save time and only import the file if it was definitely new. If anyone has a cleaner way to do this I will definitely take notes.

Yep, you need to check the last modified date. Here's a quick macro and function as an example (note that this is copied from another post):

Sub test()

MsgBox FileLastModified("C:My Documentsabook.xls")

End Sub

Function FileLastModified(strFullFileName As String)

Dim fs As Object, f As Object, s As String

Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.GetFile(strFullFileName)

s = UCase(strFullFileName) & vbCrLf

s = s & "Last Modified: " & f.DateLastModified

FileLastModified = s

Set fs = Nothing: Set f = Nothing

End Function
Function FileLastModified(strFullFileName As String)

*all the function code you provided me above

End Function


Sub Test()

Dim LastSaved As Date

LastSaved = FileDateTime(ThisWorkbook.Path & "File Data 2 Import.txt")

If LastSaved <> Now - 3(Minutes Ago) Then

MsgBox "run code here"

End If

End Sub


I know the syntax for "now -3(minutes ago) is not right, but that's my question.

How do I write something along the lines of "if file is not older than a 3 minutes, then run code..."

If LastSaved > Now - TimeValue("00:03:00") Then

Thanks again Hui and Luke, as always.

Oops I meant just Luke, you both help me out a lot it was habit lol