在VBA中将不同标签上的Excel范围导出为PDF。

huangapple go评论100阅读模式
英文:

Exporting Excel ranges on different tabs as a pdf in VBA

问题

我尝试过以下方法,但它会将我在Sheet1中指定的范围应用于所有其他工作表。我还尝试过为每个工作表使用ActiveSheet.UsedRange.Select,但这会包括我不希望显示在PDF中的多余单元格。有没有人有任何想法?我在Windows上运行Office 365。

Sub SaveRangesAsPDF()

   Sheets("Sheet1").Activate
   ActiveSheet.Range("A1:L42").Select
   Sheets("Sheet2").Activate
   ActiveSheet.Range("A1:G35").Select
  

   ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Select
   Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
      ThisWorkbook.Worksheets("Sheet3").Range("A2").Value, Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
      True
End Sub
英文:

I've tried the below, but what it does it apply the range I specify in Sheet1 to ALL the other sheets. I've also tried using ActiveSheet.UsedRange.Select for each sheet, but that includes extraneous cells that I don't want displayed in the PDF. Does anyone have any ideas? I'm running Office 365 on Windows.

Sub SaveRangesAsPDF()

   Sheets("Sheet1").Activate
   ActiveSheet.Range("A1:L42").Select
   Sheets("Sheet2").Activate
   ActiveSheet.Range("A1:G35").Select
  

   ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Select
   Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
      ThisWorkbook.Worksheets("Sheet3").Range("A2").Value, Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
      True
End Sub

Thanks so much!

答案1

得分: 2

以下是翻译好的部分:

没有选项可以在多个工作表上打印选择内容。您可以选择以下之一:

  1. 单个工作表上的选择。
  2. 活动工作表(或选择的工作表) - 考虑打印区域或不考虑打印区域。
  3. 整个工作簿 - 考虑打印区域或不考虑打印区域。

解决方案是在每个工作表上定义打印区域,然后打印所选的工作表。

英文:

There is no option to print selections on multiple sheets. You can choose one of:

  1. Selection on a single sheet.
  2. Active sheet(s) - an active sheet or selected sheets considering the printing area or not.
  3. Entire workbook - all sheets considering the printing area or not.

The solution for you is to define the printing area on each sheet and print selected sheets.

Sheet1.PageSetup.PrintArea = "$A$1:$L$42"
Sheet2.PageSetup.PrintArea = "$A$1:$G$35"
Sheets(Array("Sheet1", "Sheet2")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
  ThisWorkbook.Worksheets("Sheet3").Range("A2").Value, Quality:=xlQualityStandard, _
  IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
  True

huangapple
  • 本文由 发表于 2023年7月20日 22:10:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定