excel 将动态范围的图纸导出到单个PDF文档

jm81lzqq  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(209)

我目前正在尝试导出一些工作表(从1到500),基于包含某个单词ie的单元格:“打印”到单个PDF文档。
下面是我到目前为止尝试做的,然而,即使第8,9和10张包含“打印”在单元格A1我仍然得到消息“PDF文件未创建”。

Sub Save_Sheets_As_PDF()

Dim sheetsInPDF As Long
Dim replaceSelected As Boolean
Dim ws As Worksheet
Dim PDFfileName As String
    
    With ThisWorkbook

        sheetsInPDF = 0
        replaceSelected = True
        For Each ws In .Worksheets
            If LCase(ws.Range("A1").Value) = "Print" Then
                ws.Select replaceSelected
                replaceSelected = False
                sheetsInPDF = sheetsInPDF + 1
            End If
        Next
            
        If sheetsInPDF > 0 Then
            PDFfileName = Worksheets("QUOTE").Range("B6") & ".pdf"
            .ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfileName, _
                Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            MsgBox "Created '" & PDFfileName & "' containing " & sheetsInPDF & " Elevations"
        Else
            MsgBox "PDF file not created"
        End If
        
        .Worksheets(1).Select True
    
    End With
    
End Sub

任何帮助将不胜感激。即使完全替换此代码可能是最好的。TIA布拉德

xriantvc

xriantvc1#

这行代码。。

If LCase(ws.Range("A1").Value) = "Print" Then

...将始终被评估为False。请尝试以下操作...

If LCase(ws.Range("A1").Value) = "print" Then

要指定路径,请尝试...

PDFfileName = .Path & "\" & Worksheets("QUOTE").Range("B6") & ".pdf"

实际上,如果Worksheets(“QUOTE”)位于ThisWorkbook中,请尝试以下操作...

PDFfileName = .Path & "\" & .Worksheets("QUOTE").Range("B6") & ".pdf"

相关问题