打开工作簿的上次修改日期

时间:2013-05-20 19:46:00

标签: vba excel-vba excel

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

谢谢!

2 个答案:

答案 0 :(得分:4)

尝试以下代码:

您也可以参考此link

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

Private Sub Workbook_Open()
    LastWBModDate
End Sub

将此代码放入任何标准模块

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

答案 1 :(得分:3)

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