2017-07-24 228 views
0

因此,我有一個工作表,它使用2個其他工作表上的信息生成圖表類型的事物。在它上面,我有一個提取按鈕,應該將整個工作簿複製到一個新的工作簿中,同時製作將數據從用戶不可見的工作表。我的問題是,圖表工作表具有需要運行宏的其他功能,例如隱藏一些按鈕的按鈕等。問題是我找不到實際上是否可以通過宏從工作簿複製到新複製的工作簿?任何人都有這個答案,如果是的話,你會如何做到這一點?下面是代碼我現在有哪份工作簿到新工作簿:Excel VBA - 將工作簿複製到帶有宏的新工作簿中

Sub EWbtn() 

Dim OriginalWB As Workbook, NewCRCWB As Workbook 

Set OriginalWB = ThisWorkbook 
Set NewCRCWB = Workbooks.Add 


OriginalWB.Sheets("Generator").Copy Before:=NewCRCWB.Sheets("Sheet1") 
OriginalWB.Sheets("Module Part Number Tracker").Copy Before:=NewCRCWB.Sheets("Generator") 
OriginalWB.Sheets("CRC").Copy Before:=NewCRCWB.Sheets("Module Part Number Tracker") 

Application.DisplayAlerts = False 
NewCRCWB.Worksheets("Generator").Visible = False 
NewCRCWB.Worksheets("Module Part Number Tracker").Visible = False 
NewCRCWB.Worksheets("Sheet1").Delete 
Application.DisplayAlerts = True 

End Sub 
+0

我會複製原始Excel文件('SaveCopyAs')並刪除任何不需要的工作表。 –

回答

0

我設法找到一個答案,我的問題..此代碼工作正常,但是你需要添加「Microsoft Visual Basic的應用程序擴展5.x「作爲參考通過工具 - >參考。下面是代碼:

Dim src As CodeModule, dest As CodeModule 

Set src = ThisWorkbook.VBProject.VBComponents("Sheet1").CodeModule 
Set dest = Workbooks("Book3").VBProject.VBComponents("ThisWorkbook") _ 
.CodeModule 

dest.DeleteLines 1, dest.CountOfLines 
dest.AddFromString src.Lines(1, src.CountOfLines) 

信用:Copy VBA code from a Sheet in one workbook to another?

0

我會拿原始文件的副本,並刪除其中的/隱藏表。
所有代碼都作爲保存的一部分進行復制。

Sub Test() 

    Dim wrkBk As Workbook 
    Dim sCopyFileName As String 
    Dim wrkSht As Worksheet 

    sCopyFileName = "C:\MyFolderPaths\Book2.xlsm" 

    'Create copy of original file and open it. 
    ThisWorkbook.SaveCopyAs (sCopyFileName) 
    Set wrkBk = Workbooks.Open(sCopyFileName) 

    'wrkbk.Worksheets does not include Chart sheets. 
    'wrkbk.Sheets would take into account all the types of sheet available. 
    For Each wrkSht In wrkBk.Worksheets 
     Select Case wrkSht.Name 
      Case "Generator", "Module Part Number Tracker" 
       wrkSht.Visible = xlSheetVeryHidden 
      Case "CRC" 
       'Do nothing, this sheet is left visible. 
      Case Else 
       Application.DisplayAlerts = False 
       wrkSht.Delete 
       Application.DisplayAlerts = True 
     End Select 
    Next wrkSht 

    wrkBk.Close SaveChanges:=True 

End Sub 
相關問題