Excel VBA以选定数量的副本将所选工作表导出为PDF

该工作簿由4个隐藏的表单组成。 如果在pdf中包含A1,则每个表中的A1值为1,否则为0。

作为一个新手这是我到目前为止:

Private Sub CommandButton21_Click() Application.ScreenUpdating = False Sheets("print1").Visible = True Sheets("print2").Visible = True Sheets("print3").Visible = True Sheets("print4").Visible = True ThisWorkbook.Sheets(Array("print1", "print2")).Select iPtr = InStrRev(ActiveWorkbook.FullName, ".") If iPtr = 0 Then sFileName = ActiveWorkbook.FullName & ".pdf" Else sFileName = Left(ActiveWorkbook.FullName, iPtr - 1) & ".pdf" End If sFileName = Application.GetSaveAsFilename(InitialFileName:=sFileName, FileFilter:="PDF Files (*.pdf), *.pdf") If sFileName = "False" Then Exit Sub ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sFileName, Quality:=xlQualityStandard, openAfterPublish:=True Sheets("print1").Visible = False Sheets("print2").Visible = False Sheets("print3").Visible = False Sheets("print4").Visible = False Application.ScreenUpdating = True End Sub 

1)必须有可能使这更优雅; 表(Array(“print1”,“print2”…))。Visible = True将显示4张,但如果设置为false,则只会隐藏print1。

2)我怎样才能从A1得到的价值构build一个PDF与正确的工作表?

3)我也想在同一个PDF文件中有多张表格。 如果我要用相同的PDF格式导出print1和print3,我可能想要两个副本; print1,print3,(并从同一pdf文件中重新开始:) print1,print3。 复制的数量是从让我们说A2获得的。

请参阅我的代码,下面是您的问题的第1部分和第2部分。 我删除了iPtr if语句行,因为当sFilename设置为等于Application.GetSaveAsFileName时,它们为variablessFilenameinput的值将被覆盖。

 Private Sub CommandButton21_Click() Application.ScreenUpdating = False sheet_name_array = Array("print1", "print2", "print3", "print4") sfilename = Application.GetSaveAsFilename(InitialFileName:=sfilename, _ FileFilter:="PDF Files (*.pdf), *.pdf") If sfilename = "False" Then Exit Sub For Each sheet_name In sheet_name_array Sheets(sheet_name).Visible = True If sheets(sheet_name).Range("A1") = 0 Then sheets(sheet_name).visible = false End If Next activeworkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sfilename, _ Quality:=xlQualityStandard, openAfterPublish:=True For Each sheet_name In sheet_name_array Sheets(sheet_name).Visible = False Next Application.ScreenUpdating = True End Sub