我正在嘗試將作業表保存到 .pdf 并遇到運行時錯誤“13”:不匹配。有時如果我重新啟動 excel 它沒有錯誤,但第二次我再次運行它時會顯示錯誤。試圖搜索相同的案例,但其他案例正在處理資料型別。編碼 :
Sub save_sheet_in_pdf()
Dim ws As Worksheets
Dim name_PDF As String
Dim path_PDF As String
name_PDF = "Test.pdf"
path_PDF = "D:\Users\DIMAS\Documents\Work Documents\Organizational Development\07. PROJECT\" & name_PDF
If Range("Y5") = "PS" Then
Set ws = Worksheets("PensiunA") 'Error while run : Run-Time error '13': Type missmatch
ws.Select
Else
Set ws = Worksheets("PensiunB") 'Sometimes the error detected here
ws.Select
End If
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path_PDF, Quality:=xlQualityStandart, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
提前致謝
uj5u.com熱心網友回復:
有條件地匯出為 PDF
Sub ExportToPDF()
Const FolderPath As String = "D:\Users\DIMAS\Documents\Work Documents\" _
& "Organizational Development\07. PROJECT\"
Const Filename As String = "Test.pdf"
Const CriteriaWorksheetName As String = "Sheet1" ' adjust, it is unknown
Const CriteriaAddress As String = "Y5"
Const Criteria As String = "PS"
Const MatchWorksheetName As String = "PensiunA"
Const NoMatchWorksheetName As String = "PensiunB"
' If you accidentally forget the folder path's trailing backslash...
Dim FilePath As String
If Right(FolderPath, 1) = "\" Then
FilePath = FolderPath & Filename
Else
FilePath = FolderPath & "\" & Filename
End If
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet
' Use 'StrComp' with 'vbTextCompare'
' to compare case-insensitively i.e. 'PS = Ps = pS = ps'
' (If it needs to be exactly 'PS', use 'vbBinaryCompare').
If StrComp(CStr(wb.Worksheets(CriteriaWorksheetName) _
.Range(CriteriaAddress).Value), Criteria, vbTextCompare) = 0 Then
Set ws = wb.Worksheets(MatchWorksheetName)
Else
Set ws = wb.Worksheets(NoMatchWorksheetName)
End If
' No need to select or activate, just use the variable ('ws').
ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path_PDF, _
Quality:=xlQualityStandart, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/486699.html
