2016-07-26 85 views
0

我有一個很多工作表的工作簿。我在用戶表單上創建了許多複選框,如果我想創建工作表副本,只需選中該複選框並單擊按鈕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。

回答

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 

希望它能幫助!

+0

但我想移動很多具有相同名稱的工作表,比如在6張工作表中,我只是最後移動3個工作表具有相同的名字_(Excel)_:「Data」; **「格蘭特(Excel)」**;「資產」; **「資產(Excel)」**; **「Bub(Excel)」**; 「最終」 – kobebryant

+0

@kobebryant代碼我發佈了你移動工作簿的所有工作表,無論他們有什麼名字。 – jsanchezs

+0

親愛的jsanchezs,但我只是想移動牀單我想要的,而那些牀單最後有同名「...(Excel)」 – kobebryant