excel - Uptade Sheet.("print") 并导出为单个 pdf
问题描述
我建立了一个循环来更新工作表(“打印”)。
每次更新此表时,我都必须将其打印/导出为 PDF 文件。
是否可以将同一张纸多次打印/导出为单个 PDF?
'Loop
For i = 4 To lastrow
If Planilha4.Range("B" & i).Value = Empty Then GoTo continua
If Planilha4.Range("J" & i).Value = Range("cart_ref_tipo_cartaz").Value Then
Range("imp_linha").Value = i - 3
**'I'm currently printing it to Fine Print**
PlanilhaA4.PrintOut Copies:=Range("imp_copias").Value, IgnorePrintAreas:=False
End If
End If
continua:
Next i
解决方案
在同一个 PDF 上多次打印到 PDF 的一种方法是复制该工作表并打印/保存它们。下面的代码在 Excel 2016/365 中测试。如果您的 excel 创建了一个带有 3 张纸的新 WB,请删除所有 3 张,而不是像我一样只删除一张。
Sub printSheet()
Set wb = ThisWorkbook
'The next line creates a new Workbook
Set newWB = Workbooks.Add
'The next line creates a copy of Sheet "Testing" on the new Workbook
wb.Sheets("Testing").Copy newWB.Sheets(newWB.Sheets.Count)
'Delete the empty Default Sheet1 from the new WB
Application.DisplayAlerts = False
newWB.Sheets("Sheet1").Delete
Application.DisplayAlerts = True
'Create two Copies of Testing to print three.
newWB.Sheets("Testing").Copy newWB.Sheets(newWB.Sheets.Count)
newWB.Sheets("Testing").Copy newWB.Sheets(newWB.Sheets.Count)
'Save to PDF
'Unfurtonately, you have to use ActiveSheet to print to PDF, so we have to use Activate and Select to print both sheets.
newWB.Activate
newWB.Sheets(Array(newWB.Worksheets(1).Name, newWB.Worksheets(2).Name, newWB.Worksheets(3).Name)).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\username\Documents\filename.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
'close new WB
newWB.Close False
End Sub
推荐阅读
- hadoop - 更改主机名后的 java.net.ConnectException
- r - 使用 R 中的查找表基于另一个 df 创建新列
- python - 当索引是日期时间时,如何将两个 pandas 日期时间序列 DataFrame 相减?
- c++ - 非空函数潜在缺乏回报
- python - 处理多个用户 URL 输入,然后需要单独拆分和处理
- javascript - Angular - 选择错误 - NgFor 仅支持绑定到可迭代对象,例如数组
- javascript - 如果定义为函数参数,匿名函数何时执行?
- sql - 使用 kingswaysoft 优化插入到 crm 的记录数
- mysql - 与同一张表中的father_id和mother_id的雄辩关系
- arrays - Excel-VBA - 在数据字段数组中插入新的第一列,无需循环或 API 调用