如何从我保存为pdf VBA宏中排除1张纸

时间:2014-12-03 23:32:44

标签: vba excel-vba excel

我有一个运行良好的VBA代码,除了我不知道如何从保存到PDF中排除一个工作表这一事实。我希望排除名为“Control”的工作表包含在导出中并保存为PDF。任何想法我应该如何或在哪里添加这个?

由于

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

2 个答案:

答案 0 :(得分:0)

您可以在代码的开头隐藏工作表,然后在最后再次显示它。

<强>试验:

' Retrieve information from Control sheet

Sheets("Control").Visible = False

'YOUR PDF CREATION CODE

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

答案 1 :(得分:0)

我遇到了同样的问题,只是在导出功能期间隐藏了工作表,然后我把它带回来......这是代码:

 '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