open file if was created/saved within 30 minutes

dmtcrainey

Board Regular
Joined
Jul 27, 2003
Messages
78
I love this board.

I think I have a solution to my issue from a previous post, but wanted someone else's expertise.

I have a macro that opens a hard codeed file we will call "C:\stuff.txt".

4 times a day "C:\stuff.txt" is exported from another program and overwrote to this location, or possibly it is now being recreated. (did that make sense?).

I want to idiot proof my macro and only run my other code if the file was created or last saved withing so many minutes. Will this code work for that?

Sub stuff()

Dim Dte As Date

Const FileSpec As String = "C:\stuff.txt"

Set Fs = CreateObject("Scripting.FileSystemObject")

Set f = Fs.GetFile(FileSpec)

' Dte = f.DateCreated
' Dte = f.DateLastAccessed
Dte = f.DateLastModified
'for more attributes, see http://msdn2.microsoft.com/en-us/lib...af(VS.85).aspx

Set f = Nothing

Set Fs = Nothing

If ((Now - Dte) * 60 * 24) > 30 Then '((Now - Dte) * 60 * 24) is difference in Minutes
'msgbox with error message
Exit Sub
else
'Run my code
End If


End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,526
Hi,

Looks good to me :)

I just put a msgbox to test it:
Code:
MsgBox "Now  = " & Format(Now(), "dd/mm/yy hh:mm:ss") & vbCrLf & _
       "Then = " & Format(Dte, "dd/mm/yy hh:mm:ss") & vbCrLf & _
       "Mins = " & ((Now - Dte) * 60 * 24)
 
Upvote 0

Sumeluar

Active Member
Joined
Jun 21, 2006
Messages
250
Office Version
  1. 365
  2. 2016
  3. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
I've benn looking for something like that. Does anybody knows how to check several files (about 500) at the same time?

Regards!

Sumeluar
 
Upvote 0

Forum statistics

Threads
1,191,049
Messages
5,984,358
Members
439,882
Latest member
gerdc

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top