I want to print a PDF of specified sheets. I currently have a code that attaches the active sheet when I dont want it to

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

I want to print a PDF of specified sheets. I currently have a code that attaches the active sheet when I dont want it to

问题

以下是代码的中文翻译部分:

  1. Sub Print_Investor_Summary()
  2. Save_PDF
  3. End Sub
  4. Function Save_PDF() As Boolean
  5. Dim Thissheet As String, ThisFile As String, PathName As String
  6. Dim SvAs As String
  7. Dim MyName As String ' 声明了变量MyName
  8. Dim ws As Worksheet ' 声明了变量ws
  9. Set ws = ThisWorkbook.Worksheets("Print Packages") ' 将"Print Packages"更改为实际的工作表名称
  10. MyName = Range("C3").Value & " 汇总" ' 使用Range("C3")的值设置MyName
  11. SvAs = ThisWorkbook.Path & "\" & MyName & ".pdf" ' 使用完整路径更新文件保存名称
  12. Dim lastRow As Long
  13. lastRow = ws.Cells(ws.Rows.Count, "E").End(xlUp).Row
  14. If lastRow < 13 Then lastRow = 13 ' 将lastRow的最小值设置为13
  15. Dim mySheets1() As Variant
  16. ReDim mySheets1(1 To lastRow - 12) ' 根据值的数量调整数组大小
  17. Dim i As Long
  18. For i = 13 To lastRow ' 从E列的第13行开始
  19. mySheets1(i - 12) = ws.Cells(i, "E").Value ' 将值添加到数组中
  20. Next i
  21. Application.ScreenUpdating = False
  22. Dim sheetName As Variant
  23. Dim sheetNames As String
  24. ' 将mySheets1的内容连接为字符串
  25. For Each sheetName In mySheets1
  26. sheetNames = sheetNames & sheetName & vbCrLf
  27. Next sheetName
  28. ' 在消息框中显示mySheets1的内容
  29. MsgBox "mySheets1 包含:" & vbCrLf & sheetNames
  30. ' 选择并激活必要的工作表
  31. For Each sheet In mySheets1
  32. ThisWorkbook.Worksheets(sheet).Select False
  33. Next sheet
  34. ' 设置打印质量
  35. On Error Resume Next
  36. ws.PageSetup.PrintQuality = 600
  37. Err.Clear
  38. On Error GoTo 0
  39. On Error GoTo SaveError
  40. Dim currentSheet As Worksheet
  41. For Each sheet In mySheets1
  42. Set currentSheet = ThisWorkbook.Worksheets(sheet)
  43. If currentSheet.Name <> ActiveSheet.Name Then
  44. currentSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SvAs, Quality:=xlQualityMaximum, IncludeDocProperties:=False, IgnorePrintAreas:=False
  45. End If
  46. Next sheet
  47. On Error GoTo 0
  48. Save_PDF = True ' 返回True以指示成功执行
  49. ' 打开用户计算机上的PDF文件
  50. On Error Resume Next
  51. Shell "explorer.exe " & Chr(34) & SvAs & Chr(34), vbNormalFocus
  52. On Error GoTo 0
  53. Sheets("Print Packages").Select
  54. EndMacro:
  55. Application.DisplayAlerts = True ' 重新启用显示警报
  56. Application.ScreenUpdating = True ' 重新启用屏幕更新
  57. Exit Function
  58. SaveError:
  59. MsgBox "无法保存为PDF。请检查文件路径并重试。"
  60. Save_PDF = False ' 返回False以指示错误
  61. Resume EndMacro ' 转到EndMacro标签以重新启用显示警报和屏幕更新
  62. End Function

请注意,我只提供了代码的翻译部分,没有包括问题陈述或额外的内容。如果您需要进一步的帮助或解释,请随时提问。

英文:

I will attach code below.
I essentially have a list of the names of sheets in the workbook that I want to use a macro button to print to a pdf. This list starts in cell e13 on the "Print Packages" sheet of the workbook and proceeds vertically, (next sheet contained in e14, and e15, and so on). For some reason, my code attaches the Print Packages sheet to the pdf it prints out, which I do not want it to do.

If there is a solution to this or a more efficient way to do this code as well please let me know.

CODE:

  1. Sub Print_Investor_Summary()
  2. Save_PDF
  3. End Sub
  4. Function Save_PDF() As Boolean
  5. Dim Thissheet As String, ThisFile As String, PathName As String
  6. Dim SvAs As String
  7. Dim MyName As String &#39; Added variable declaration for MyName
  8. Dim ws As Worksheet &#39; Added variable declaration for the worksheet
  9. Set ws = ThisWorkbook.Worksheets(&quot;Print Packages&quot;) &#39; Change &quot;Print Packages&quot; to the actual sheet name
  10. MyName = Range(&quot;C3&quot;).Value &amp; &quot; Summary&quot;
  11. SvAs = ThisWorkbook.Path &amp; &quot;\&quot; &amp; MyName &amp; &quot;.pdf&quot; &#39; Updated the file save name with the full path
  12. Dim lastRow As Long
  13. lastRow = ws.Cells(ws.Rows.Count, &quot;E&quot;).End(xlUp).Row
  14. If lastRow &lt; 13 Then lastRow = 13 &#39; Set the minimum lastRow value to 13
  15. Dim mySheets1() As Variant
  16. ReDim mySheets1(1 To lastRow - 12) &#39; Adjust the array size based on the number of values
  17. Dim i As Long
  18. For i = 13 To lastRow &#39; Start from row 13 in Column E
  19. mySheets1(i - 12) = ws.Cells(i, &quot;E&quot;).Value &#39; Add the value to the array
  20. Next i
  21. Application.ScreenUpdating = False
  22. Dim sheetName As Variant
  23. Dim sheetNames As String
  24. &#39; Concatenate the contents of mySheets1 into a string
  25. For Each sheetName In mySheets1
  26. sheetNames = sheetNames &amp; sheetName &amp; vbCrLf
  27. Next sheetName
  28. &#39; Display the contents of mySheets1 in a message box
  29. MsgBox &quot;mySheets1 contains:&quot; &amp; vbCrLf &amp; sheetNames
  30. &#39; Select and activate the necessary worksheets
  31. For Each sheet In mySheets1
  32. ThisWorkbook.Worksheets(sheet).Select False
  33. Next sheet
  34. &#39; Set Print Quality
  35. On Error Resume Next
  36. ws.PageSetup.PrintQuality = 600
  37. Err.Clear
  38. On Error GoTo 0
  39. On Error GoTo SaveError
  40. Dim currentSheet As Worksheet
  41. For Each sheet In mySheets1
  42. Set currentSheet = ThisWorkbook.Worksheets(sheet)
  43. If currentSheet.Name &lt;&gt; ActiveSheet.Name Then
  44. currentSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=SvAs, Quality:=xlQualityMaximum, IncludeDocProperties:=False, IgnorePrintAreas:=False
  45. End If
  46. Next sheet
  47. On Error GoTo 0
  48. Save_PDF = True &#39; Return True to indicate successful execution
  49. &#39; Open the PDF file on the user&#39;s computer
  50. On Error Resume Next
  51. Shell &quot;explorer.exe &quot; &amp; Chr(34) &amp; SvAs &amp; Chr(34), vbNormalFocus
  52. On Error GoTo 0
  53. Sheets(&quot;Print Packages&quot;).Select
  54. EndMacro:
  55. Application.DisplayAlerts = True &#39; Added to re-enable display alerts
  56. Application.ScreenUpdating = True &#39; Added to re-enable screen updating
  57. Exit Function
  58. SaveError:
  59. MsgBox &quot;Unable to save as PDF. Please check your file path and try again.&quot;
  60. Save_PDF = False &#39; Return False to indicate error
  61. Resume EndMacro &#39; Go to EndMacro label to enable re-enabling display alerts and screen updating
  62. End Function

I have tried forcibly excluding it from the variant that contains the names of the sheets I desire to be printed as well as many other methods and just cant seem to get it to work.

You will see I even have a portion of the code to display the names of the sheets contained in mysheets1 that shows that the active sheet is not a part of the array but yet still shows up in the final product PDF.

答案1

得分: 1

ThisWorkbook.Worksheets(sheet).Select False 将工作表 sheet 添加到当前所选工作表集合中。对于第一个工作表,您需要使用 True,这样它会替换当前的工作表。

  1. Function Save_PDF() As Boolean
  2. Dim rng As Range, c As Range, replacePrevious As Boolean
  3. Dim ws As Worksheet
  4. With ThisWorkbook.Worksheets("Print Packages")
  5. Set rng = .Range("E13:E" & .Cells(.Rows.Count, "E").End(xlUp).Row)
  6. replacePrevious = True '第一个工作表将替换当前所选工作表
  7. For Each c In rng.Cells
  8. Set ws = ThisWorkbook.Sheets(c.Value)
  9. On Error Resume Next
  10. ws.PageSetup.PrintQuality = 600
  11. Err.Clear
  12. On Error GoTo 0
  13. ws.Select replacePrevious '选择工作表
  14. '切换为将工作表添加到已选择的工作表集合中
  15. replacePrevious = False
  16. Next c
  17. End With
  18. ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
  19. Filename:= "yourFilePathHere", Quality:= xlQualityStandard, _
  20. IncludeDocProperties:=True, IgnorePrintAreas:=False, _
  21. OpenAfterPublish:=True
  22. End Function
英文:

ThisWorkbook.Worksheets(sheet).Select False adds the worksheet sheet to the current sheet selection. You need to use True for the first sheet, so it replaces the current sheet.

  1. Function Save_PDF() As Boolean
  2. Dim rng As Range, c As Range, replacePrevious As Boolean
  3. Dim ws As Worksheet
  4. With ThisWorkbook.Worksheets(&quot;Print Packages&quot;)
  5. Set rng = .Range(&quot;E13:E&quot; &amp; .Cells(.Rows.Count, &quot;E&quot;).End(xlUp).row)
  6. replacePrevious = True &#39;first sheet repalces whatever is selected
  7. For Each c In rng.Cells
  8. Set ws = ThisWorkbook.Sheets(c.Value)
  9. On Error Resume Next
  10. ws.PageSetup.PrintQuality = 600
  11. Err.Clear
  12. On Error GoTo 0
  13. ws.Select replacePrevious &#39;select the sheet
  14. &#39;switch to adding the sheet to the already-selected sheet(s)
  15. replacePrevious = False
  16. Next c
  17. End With
  18. ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
  19. Filename:= &quot;yourFilePathHere&quot;, Quality:= xlQualityStandard, _
  20. IncludeDocProperties:=True, IgnorePrintAreas:=False, _
  21. OpenAfterPublish:=True
  22. End Function

huangapple
  • 本文由 发表于 2023年6月26日 22:25:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557622.html
匿名

发表评论

匿名网友

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

确定