将表格导出/移动到新书

时间:2016-07-26 14:39:03

标签: excel vba excel-vba

我有一张包含多张纸的工作簿。我在userform上创建了许多复选框,如果我想创建工作表副本,只需选中该复选框并单击按钮1,这些副本的名称如:" (Excel)"。如何移动这些工作表" (Excel)"一本新书?

'Create sheets
Dim Ctl As Control
For Each Ctl In Me.Controls
  If Ctl.Value = True Then Run Ctl.Tag
Next

'Move Sheets name *(Excel) to other workbook. But it's only move one sheet
Dim bReplace As Boolean, sh As Worksheet
Dim bk As Workbook
bReplace = True
For Each sh In Worksheets
If sh.Name Like "*(Excel)" Then
sh.Select Replace:=bDontReplace
bReplace = False
End If
Next

此外,我想通过另一个按钮将这些工作表导出为PDF。

1 个答案:

答案 0 :(得分:0)

这是一种移动多张纸的简单方法:

Sub CopyWorkbook()

Dim currentSheet as Worksheet
Dim sheetIndex as Integer
sheetIndex = 1

For Each currentSheet in Worksheets

    Windows("source workbook").Activate 
    currentSheet.Select
    if currentSheet.Name like "*(Excel)" then
       currentSheet.Move Before:=Workbooks("target workbook").Sheets(sheetIndex) 
    End if

    sheetIndex = sheetIndex + 1

Next currentSheet

End Sub

要将它们导出为pdf,这将在按钮点击事件中执行:

   SourceWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
   Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
      "C:\Users\you\Desktop\exported_sheet.pdf", Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
      True

希望它有所帮助!