工作表运行时错误"13":类型不匹配



我试图将工作表保存为。pdf并遇到运行时错误'13':mismatch。有时,如果我重新启动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:UsersDIMASDocumentsWork DocumentsOrganizational Development7. 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

Thanks in Advance

有条件导出为PDF

Sub ExportToPDF()

Const FolderPath As String = "D:UsersDIMASDocumentsWork Documents" _
& "Organizational Development7. 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

最新更新