Excel VBA错误:";编译错误:预期的结束子;
试图将“GetFullNamePDF()”传递给Filename属性,但出现以下错误:“编译错误:预期的结束子” 我对VBA一无所知,从a获得了上面的代码,但当时无法测试。猜测错误与函数有关,因为代码在没有添加函数和硬编码文件路径/名称的情况下工作Excel VBA错误:";编译错误:预期的结束子;,excel,vba,Excel,Vba,试图将“GetFullNamePDF()”传递给Filename属性,但出现以下错误:“编译错误:预期的结束子” 我对VBA一无所知,从a获得了上面的代码,但当时无法测试。猜测错误与函数有关,因为代码在没有添加函数和硬编码文件路径/名称的情况下工作 代码的思想是动态地使用文件名本身来命名PDF的路径和文件。如果您有任何问题,请发表评论--谢谢 在子类中声明函数是非法的。 应该是这样的: Function GetFullNamePDF() As String GetFullNamePDF
代码的思想是动态地使用文件名本身来命名PDF的路径和文件。如果您有任何问题,请发表评论--谢谢 在子类中声明函数是非法的。 应该是这样的:
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
像这样:
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
不能在过程中嵌套函数。您需要将其移动到上面:
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
感谢您花时间阅读代码并确保一切正常;wise我的代码说我的代码更好,但从未使用过Excel的VBA。@Remou:代码中没有“sFilename”的引用,我认为这不起作用。@错误你忘了我写了这个函数吗?我非常非常熟悉VBA(检查VBA标签上的用户)。将变量设置为函数的结果并不少见,这使调试更加容易。重写函数时出现错误,我将予以纠正。@Remou:你是对的,它会起作用--将变量设置为函数结果的原因是什么?正如我所说,它使调试更容易,也使代码更易于阅读。@RolandTumble如果你在该级别遇到编码问题,考虑ActheSeGET,它可能返回或不返回对象。
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