【问题标题】:VBA Save Visible Cells on Active Sheet as PDFVBA 将活动工作表上的可见单元格另存为 PDF
【发布时间】:2021-08-03 08:56:18
【问题描述】:

我有一个可以成功运行的代码,但我想对其进行扩展,使其仅导出可见单元格。当它运行时,它会根据需要保存 PDF,但 PDF 有很多空白空间。

Sub OrderFormHide()

    Worksheets("Order Form").Unprotect "!Product1@"
    
'AutoFit All Columns on Worksheet
ThisWorkbook.Worksheets("Order Form").Cells.EntireRow.AutoFit
Application.ScreenUpdating = False

'Hide rows with no data requirements
Dim c As Range
For Each c In Range("A:A")
    If InStr(1, c, "DELETE") Or InStr(1, c, "DELETE") Then
            c.EntireRow.Hidden = True
        ElseIf InStr(1, c, "") Or InStr(1, c, "") Then
            c.EntireRow.Hidden = False
    End If
    Next
    
    Worksheets("Order Form").Protect "!Product1@"
    

Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim MyFile As Variant
On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strDate = Format(Now(), "ddmmyyyy")
strC = Worksheets("Start Page").Range("$C$10").Value



'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")

'create default name for saving file
strFile = strName & "_" & strC & "_" & strDate & ".pdf"
strPathFile = strPath & strFile

'use can enter name and
' select folder for file
MyFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

'export to PDF if a folder was selected
If MyFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=MyFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & MyFile
End If

 exitHandler:
    Exit Sub
 errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
    
Application.ScreenUpdating = True

End Sub

我使用了以前构建的代码中的位,但我不知道如何实现此更改。任何帮助将不胜感激。

【问题讨论】:

  • 当您尝试ExportAsFixedFormat 用于不连续范围时,每个间隙将确定一个空格,甚至是导出的 pdf 中的一个新页面。因此,最好先将可见单元格范围复制到新工作表上,然后导出新工作表并在导出后将其删除。我会贴一段代码来举例说明。

标签: excel vba excel-2010 export-to-pdf


【解决方案1】:

请尝试实施下一种方法。它使用一个新的辅助表,将不连续的范围复制到那里(作为连续的),导出此表并在之后将其删除:

Sub testExportVisibleCellsRange()
  Dim sh As Worksheet, shNew As Worksheet, rngVis As Range, strPDF As String
  
  strPDF = ThisWorkbook.path & "\testVisible.pdf"
  Set sh = ActiveSheet 'use here the necessary sheet
  
  Set rngVis = sh.UsedRange.SpecialCells(xlCellTypeVisible)

  Set shNew = Worksheets.Add(After:=sh)
  rngVis.Copy shNew.Range("A1")
  shNew.UsedRange.EntireColumn.AutoFit
  With shNew.PageSetup
      .Orientation = xlPortrait
      .FitToPagesWide = 1
  End With
  shNew.ExportAsFixedFormat Type:=xlTypePDF, fileName:=strPDF
  Application.DisplayAlerts = False
    shNew.Delete
  Application.DisplayAlerts = True
End Sub

【讨论】:

  • 谢谢你,我已经让它工作了,但我希望它是纵向格式,列适合页面。
  • 我现在想通了,需要进行一些细微的调整以满足我的需要。我将这些设置添加到新页面设置中。 .Orientation = xlPortrait .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = False .LeftMargin = Application.InchesToPoints(0.25) .RightMargin = Application.InchesToPoints(0.25) .TopMargin = Application.InchesToPoints(0.75) .BottomMargin = Application.InchesToPoints( 0.75) .HeaderMargin = Application.InchesToPoints(0.3) .FooterMargin = Application.InchesToPoints(0.3)
  • @Peter Mogford 我已经根据您的需要调整了代码。如果您在评论中发布这样的代码,则很难阅读和理解。也可以设置PrintArea,但发布时设置IgnorePrintAreas = False。默认是True,意思是忽略它。无需使用 PageSetup 属性= False。如果您忽略它们,它们的行为将相同...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 2013-03-08
相关资源
最近更新 更多