Sub SaveAsPDF()
Dim NewFN As Variant
NewFN = "C:\Users\Brock\Desktop\MPI\Financials\" & Range("b10").Value & ".pdf"
Dim ActiveWorksheet As Variant
ActiveWorksheet = "sheet1"
ActiveWorksheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=NewFN, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
每次都会弹出424错误。突出显示的部分是:
ActiveWorksheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=NewFN, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
我实在搞不懂问题出在哪里。我正在学习 VBA 编码,所以很多东西我都不知道是什么意思!
答案1
ActiveWorkSheet
WorkSheets()
必须是工作表,或者在用作字符串时必须用 包装。
作为工作表:
Dim ActiveWorksheet As WorkSheet
Set ActiveWorksheet = WorkSheets("sheet1")
原样:
Dim ActiveWorksheet As Variant
ActiveWorksheet = "sheet1"
Worksheets(ActiveWorksheet).ExportAsFixedFormat ...
您收到错误的原因是:
"sheet1".ExportAsFixedFormat
在 vba 中不起作用。