首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >If语句- msgbox

If语句- msgbox
EN

Stack Overflow用户
提问于 2018-07-02 22:55:04
回答 1查看 5.5K关注 0票数 2

我现在正试着写一个IF语句来表达这样的意思:如果文件存在超过5天,就不要运行宏。如果超过5天,则运行宏。

我希望这是一个是或否的对话框。这是我的代码。请帮帮忙。我还在努力学习这段vba代码。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-07-02 23:25:28

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

现在还不完全清楚你想要对你的Yes/No消息框做什么,这里是一个尝试:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51138916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档