我现在正试着写一个IF语句来表达这样的意思:如果文件存在超过5天,就不要运行宏。如果超过5天,则运行宏。
我希望这是一个是或否的对话框。这是我的代码。请帮帮忙。我还在努力学习这段vba代码。
Sub LastModifiedFile()
'Function FileLastModified(strFullFileName As String)
Dim fs As Object, f As Object, s As String, dtmodpath As String
dtmodpath = "\\jdshare\pdcmaterials\5_Tools\FTP\Cancelled_Report.txt"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(dtmodpath)
's = UCase(strFullFileName) & vbCrLf
s = f.DateLastModified
FileLastModified = s
Set fs = Nothing: Set f = Nothing
Range("E5").Value = FileLastModified
'If FileExists(strFullName) Then
'MsgBox FileLastModified(strFullName)
'Else
'MsgBox "File Older than 5 Years : " & vbNewLine & strFullName
'End If
'End Function
End Sub发布于 2018-07-02 23:25:28
使用DateDiff函数计算您的天数。
现在还不完全清楚你想要对你的Yes/No消息框做什么,这里是一个尝试:
Sub LastModifiedFile()
Dim fs As Object, f As Object, s As String, dtmodpath As String
Dim dtLastMod As Date
Dim intDays As Long
dtmodpath = "\\jdshare\pdcmaterials\5_Tools\FTP\Cancelled_Report.txt"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(dtmodpath)
dtLastMod = f.DateLastModified
' Here you compute the number of days between the file last mod date, and the current date
intDays = DateDiff("d", dtLastMod, Now)
Set fs = Nothing: Set f = Nothing
Range("E5").Value = dtLastMod
If intDays > 5 Then
If MsgBox("File is " & intDays & " days old, proceed with macro ?", vbYesNo, "Continue?") = vbYes Then
' RUN MACRO GOES HERE
End If
Else
MsgBox "File is " & intDays & " days old, cancelling"
End If
End Subhttps://stackoverflow.com/questions/51138916
复制相似问题