vba 打开工作簿的最后修改日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16657170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:12:03  来源:igfitidea点击:

Last modification date of open workbook

vbaexcel-vbaexcel

提问by lajulajay

Vba newbie. Need a function to output the last modification date of an open workbook. Here is what I have so far but I am getting a message that my formula contains an error when I invoke the function:

vba新手。需要一个函数来输出打开的工作簿的最后修改日期。这是我到目前为止所拥有的,但是当我调用该函数时,我收到一条消息,指出我的公式包含错误:

Function LastWBModDate(wbname)

    ActivateWB (wbname)
    LastWBModDate = Format(FileDateTime(ActiveWorkbook.FullName), "m/d/yy h:n ampm")

End Function

Public Function ActivateWB(wbname As String)

    If IsWBOpen(wbname) Then
        Workbooks(wbname).Activate
    Else
        MsgBox "Workbook : " & wbname & " is not open " & vbNewLine
    End If

End Function

Public Function IsWBOpen(wbname As String) As Boolean

    On Error Resume Next
    If Workbooks(wbname) Is Nothing Then
        IsWBOpen = False
    Else
        IsWBOpen = True
    End If

End Function

Thanks!

谢谢!

回答by Santosh

Try below code :

试试下面的代码:

You may also refer this link

你也可以参考这个链接

Put below code on ThisWorkbook code section

将以下代码放在 ThisWorkbook 代码部分

Private Sub Workbook_Open()
    LastWBModDate
End Sub

Put this code in any Standard Module

将此代码放在任何标准模块中

Function LastWBModDate() As String

    Dim FSO As Object
    Dim File As Object

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set File = FSO.GetFile(ThisWorkbook.FullName)
    LastWBModDate = Format(File.DateLastModified, "m/d/yy h:n ampm")
    Msgbox LastWBModDate 
    Set FSO = Nothing
End Function

回答by Tim Williams

Function LastWBModDate(wbname As String)
Dim rv, wb As Workbook

    rv = "workbook?" 'default return value

    On Error Resume Next
    Set wb = Workbooks(wbname)
    On Error GoTo 0

    If Not wb Is Nothing Then
        rv = Format(FileDateTime(wb.FullName), "m/d/yy h:n ampm")
    End If

    LastWBModDate = rv

End Function