【问题标题】:Why am I getting Compile Error: Expected End Sub为什么我收到编译错误:预期的结束子
【发布时间】:2018-07-02 21:41:16
【问题描述】:

我有以下 vba 代码,当我尝试运行它时,它显示:编译错误:预期结束子。

有人知道我做错了什么吗?我对vba一无所知。我希望它检查最后修改的文件,然后告诉我它是否在 msgbox 中超过 5 年。

Sub LastModifiedFile()

Function FileLastModified(strFullFileName As String)
    Dim fs As Object, f As Object, s As String

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(strFullFileName)

    s = UCase(strFullFileName) & vbCrLf
    s = s & "Last Modified: " & f.DateLastModified
    FileLastModified = s

    Set fs = Nothing: Set f = Nothing

If FileExists(strFullName) Then
        MsgBox FileLastModified(strFullName)
    Else
        MsgBox "File Older than 5 Years : " & vbNewLine & strFullName
    End If

End Function

End Sub

【问题讨论】:

  • 您需要一个 IF 语句(或类似的语句)来检查 LResult 的值。您的代码 msgbox 现在是否显示任何内容?
  • 我的 Msgbox 目前没有任何内容。 IF 语句会是什么样子?

标签: excel vba


【解决方案1】:

该代码不会按原样工作。您有来自函数的代码和来自过程的代码相互交织在一起。

第一行定义你的程序:

  Sub LastModifiedFile()

然后你就有了与函数相关的代码。
这段代码应该找出传递给它的每个文件名的最后修改日期,并将该日期返回给主过程......你可以看到倒数第二行,上面写着FileLastModified = s。这是它将值传回调用过程的那一行。

Function FileLastModified(strFullFileName As String)
    Dim fs As Object, f As Object, s As String

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(strFullFileName)

    s = UCase(strFullFileName) & vbCrLf
    s = s & "Last Modified: " & f.DateLastModified
    FileLastModified = s

    Set fs = Nothing: Set f = Nothing

接下来,您将再次获得主过程中的一些代码。
您可以在第二行看到它要求 FileLastModified 函数查看您传递给它的任何文件的路径。
这里有一个问题——第一行调用了一个名为FileExists(strFullName) 的函数。这会将文件路径传递给您尚未发布的函数,该函数首先询问文件是否存在 - 它应该返回 TRUE 或 FALSE。
这里的另一个问题是,如果找不到文件,它会说它超过五年,而不是说它根本不存在。

If FileExists(strFullName) Then
        MsgBox FileLastModified(strFullName)
    Else
        MsgBox "File Older than 5 Years : " & vbNewLine & strFullName
    End If

在这段代码之后有两行——一行结束函数,另一行结束子。

End Function

End Sub  

您的代码应该看起来更像这样:

Sub LastModifiedFile()

    'Check if File Exists using `FileExists` function.
    If FileExists(strFullName) Then
        'If it does exist then pass the path to see when it was last modified.
        MsgBox FileLastModified(strFullName)
    Else
        'If it doesn't exist then say it's older than 5 years.
        MsgBox "File Older than 5 Years : " & vbNewLine & strFullName
    End If

End Sub

Function FileLastModified(strFullFileName As String)

    Dim fs As Object, f As Object, s As String

    Set fs = CreateObject("Scripting.FileSystemObject")

    'Returns a file object.
    Set f = fs.GetFile(strFullFileName)

    s = UCase(strFullFileName) & vbCrLf

    'f.DateLastModified returns the modified date of the file object.
    s = s & "Last Modified: " & f.DateLastModified

    'Pass the string variable back to whatever called it.
    FileLastModified = s

    Set fs = Nothing: Set f = Nothing

End Function  

您仍然需要 FileExists 函数的代码。

【讨论】:

  • 另一个问题:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多