Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将Excel工作表导出为PDF时出现400和1004错误_Vba_Excel_Pdf - Fatal编程技术网

Vba 将Excel工作表导出为PDF时出现400和1004错误

Vba 将Excel工作表导出为PDF时出现400和1004错误,vba,excel,pdf,Vba,Excel,Pdf,请参阅下面的代码: Sub SmartPDF_Generator() Dim answer As Integer pdfName = ActiveSheet.Cells(1, 5) & " __ " & Cells(1, 8) answer = MsgBox("Please confirm creat dashboard for Carrier" & Cells(1, 5) & " Event" & Cells(

请参阅下面的代码:

Sub SmartPDF_Generator()

    Dim answer As Integer

    pdfName = ActiveSheet.Cells(1, 5) & " __ " & Cells(1, 8)        
    answer = MsgBox("Please confirm creat dashboard for Carrier" & Cells(1, 5) & " Event" & Cells(1, 8), vbYesNo + vbQuestion, "Comfirm dashboard")

    If answer = vbNo Then        
        MsgBox ("Please select your carrier and event combination again")        
    Else        
        ChDir ("C:\test\")                
        fileSaveName = ActiveSheet.Cells(1, 5) & " __ " & Cells(1, 8)

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

        MsgBox "File Saved " & " " & fileSaveName        
    End If        
Application.StatusBar = False

End Sub

您需要检查
ActiveSheet.Cells(1,5)
Cells(1,8)
是否为空,然后才能继续执行代码。为什么在第二种情况下不使用
ActiveSheet
?如果没有该限定符,您只会自找麻烦。

您的代码在填充了E1和H1的工作表上对我有效,但如果它们都为空,我会得到错误1004…您应该使用定义良好的工作表对象,而不是依赖Excel猜测您正在处理的工作表。坏习惯