How to exclude 1 sheet from my save to pdf VBA macro

南楼画角 提交于 2020-02-25 12:55:16

问题


I have a VBA code that works well, apart from the fact that i don't know how to exclude one sheet from saving to the PDF. I would like the exclude the sheet named 'Control' from being included in the export and save to PDF. Any ideas how or where i should add this?

Thanks

Sub CreatePDF()

Dim saveAsName As String
Dim WhereTo As String
Dim sFileName As String
Dim ws As Worksheet
Dim myrange

' Retrieve information from Control sheet
Sheets("Control").Activate
Range("C4").Activate
periodName = ActiveCell.Value
Range("C5").Activate
saveAsName = ActiveCell.Value
Range("C6").Activate
WhereTo = ActiveCell.Value

Set myrange = Worksheets("Control").Range("range_sheetProperties")

' Check if Stamp-field has any value at all
' if not, add the current date.
If Stamp = "" Then Stamp = Date

' Assemble the filename
     sFileName = WhereTo & saveAsName & " (" & Format(CDate(Date), "DD-MMM-YYYY") & ").pdf"

' Format all sheets as landsape, autofit to 1 page and provide header
For Each ws In ActiveWorkbook.Worksheets

    With ws.PageSetup
    Application.PrintCommunication = False
    .Orientation = xlLandscape
    .Zoom = False
    .FitToPagesWide = 1
    .CenterHorizontally = True
    .ScaleWithDocHeaderFooter = False
    .AlignMarginsHeaderFooter = False
    .HeaderMargin = Application.InchesToPoints(0.31496062992126)
    Application.PrintCommunication = True

    DisplayHeader = Application.VLookup(ws.Name, myrange, 2, False)
        If Not IsError(DisplayHeader) Then
        .LeftHeader = "&L &""Arial,Bold""&11&K00-048DIVA: " & DisplayHeader
        Else: .LeftHeader = "&L &""Arial,Bold""&11&KFF0000WORKSHEET NOT DEFINED IN CONTROL SHEET "
        End If
    .CenterHeader = "&C &""Arial,Bold""&11&K00-048" & periodName
    End With

Next

' Save the File as PDF
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        sFileName, Quality _
        :=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=True

     MsgBox "PDF document has been created and saved to : " & sFileName

' Make sure we open the Control sheet upon Exit
    Sheets("Control").Activate

End Sub

回答1:


You could hide the worksheet at the beginning of the code and then make it visible again at the end.

TESTED:

' Retrieve information from Control sheet

Sheets("Control").Visible = False

'YOUR PDF CREATION CODE

Sheets("Control").Visible = True
Sheets("Control").Activate



回答2:


I ran into the same problem and just hid the sheet during the export function, then I brought it back... Here is the code:

 'Hide the log sheet to exclude from export
 ActiveWorkbook.Sheets("Log").Visible = xlSheetHidden

ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FilePath + Today + "\" + Range("H2").Value _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True

'Bring back the log sheet to allow for editing
ActiveWorkbook.Sheets("Log").Visible = xlSheetVisible


来源:https://stackoverflow.com/questions/27283670/how-to-exclude-1-sheet-from-my-save-to-pdf-vba-macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!