如果语句-msgbox

时间:2018-07-02 14:55:04

标签: vba if-statement msgbox

我现在正在尝试编写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

2 个答案:

答案 0 :(得分:4)

祝贺您正确使用.DateLastModified属性!

代替MsgBox-es调用函数。 DateAdd()返回日期,该日期是当前日期之前的5天,因此很容易比较。这显示了MsgBox(),它通知文件距上一次修改是否有少于或少于5天的时间:

Option Explicit

Sub LastModifiedFile()

    Dim fileObject As Object
    Dim file As Object
    Dim modPath As String

    modPath = "\\jdshare\pdcmaterials\5_Tools\FTP\Cancelled_Report.txt"
    Set fileObject = CreateObject("Scripting.FileSystemObject")
    Set file = fileObject.GetFile(modPath)

    If DateAdd("d", -5, Now) < file.DateLastModified Then
        MsgBox "Less than 5 days."
    Else
        MsgBox "More than 5 days."
    End If

End Sub

如果要在整个故事中使用放置一个MsgBox,则应该可以:

Sub LastModifiedFile()

    Dim fileObject As Object
    Dim file As Object
    Dim modPath As String

    modPath = "\\jdshare\pdcmaterials\5_Tools\FTP\Cancelled_Report.txt"
    Set fileObject = CreateObject("Scripting.FileSystemObject")
    Set file = fileObject.GetFile(modPath)

    Dim msgBoxStatement As String

    If DateAdd("d", -5, Now) < file.DateLastModified Then
        msgBoxStatement = "This file is NOT older than 5 days!" & vbCrLf & _
            "Should it be deleted?"
    Else
        msgBoxStatement = "This file is older than 5 days!" & vbCrLf & _
            "Should it be deleted?"
    End If        

    Select Case MsgBox(msgBoxStatement, vbYesNo Or vbQuestion, "Delete?")        
        Case vbYes
            'run the for deletion
        Case vbNo
            'do not run the code for deletion
    End Select

End Sub

答案 1 :(得分:1)

使用DateDiff函数计算您的天数。

使用“是/否”消息框尚不清楚您要做什么,这是一个尝试:

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 Sub