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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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
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,214,793
Messages
6,121,614
Members
449,039
Latest member
Mbone Mathonsi

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
Back
Top