VBA 错误:“编译错误:预期结束子”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4472095/
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-08 11:00:46  来源:igfitidea点击:

VBA Error: "Compile error: Expected End Sub"

excelvba

提问by blunders

Trying to pass "GetFullNamePDF()" to the Filename attribute, but getting the following error: "Compile error: Expected End Sub"

试图将“GetFullNamePDF()”传递给文件名属性,但出现以下错误:“编译错误:预期结束子”

Sub PrintPDF()

    Function GetFullNamePDF() As String
        GetFullNameCSV = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
    End Function

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "GetFullNamePDF()", Quality:=xlQualityStandard, IncludeDocProperties _
        :=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

End Sub

I know nothing about VBA, and got the above code from a question I asked yesterday, but was unable to test at the time. Guessing the error has to do with the function, since the code works without the function added and the filepath/name hard coded.

我对 VBA 一无所知,从我昨天问的一个问题中得到了上面的代码,但当时无法测试。猜测错误与函数有关,因为代码在没有添加函数和硬编码文件路径/名称的情况下工作。

Idea of the code is to dynamically use the filename of itself to name the path and file for the PDF. If you have any questions, just comment -- thanks!

代码的想法是动态使用自身的文件名来命名PDF的路径和文件。如果您有任何问题,请发表评论-谢谢!

回答by kgiannakakis

You can't nest a function inside a procedure. You need to move it above:

您不能在过程中嵌套函数。你需要把它移到上面:

Function GetFullNamePDF() As String
    GetFullNameCSV = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
    'This should be
    GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function

Sub PrintPDF()

     'Remove the quotes from GetFullNamePDF
     ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        GetFullNamePDF(), Quality:=xlQualityStandard, IncludeDocProperties _
        :=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

End Sub

回答by Martin Broadhurst

It is illegal to declare a function within a sub. It should look like this:

在 sub 中声明函数是非法的。它应该是这样的:

Function GetFullNamePDF() As String 
    GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf") 
End Function 


Sub PrintPDF() 
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ 
        "GetFullNamePDF()", Quality:=xlQualityStandard, IncludeDocProperties _ 
        :=True, IgnorePrintAreas:=False, OpenAfterPublish:=False 
End Sub 

回答by Fionnuala

Like this:

像这样:

Function GetFullNamePDF() As String
    GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function

Sub PrintPDF()
    Dim sFileName As Variable

    sFileName=GetFullNamePDF()

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        sFilename, Quality:=xlQualityStandard, IncludeDocProperties _
        :=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

End Sub