将工作表复制到带有模块的新工作簿

问题描述 投票:0回答:1

我有一些代码成功地将所需的工作表拉入新的工作簿并将所述工作簿保存在与源工作簿相同的位置。但是,我也无法复制模块。当我运行此代码时,我没有收到任何错误,但模块也不会复制到新工作簿。请指教。

Dim path As String
Dim filename1 As String
filename1 = Worksheets("Instructions").Range("P1").Text & " Tech Eval"
path = ThisWorkbook.path & "\"

Worksheets(Array("Narrative", "Summary Page", "Exceptions", "Offer Rating Sheet")).Copy

 With ActiveWorkbook
 .SaveAs Filename:=path & filename1, FileFormat:=xlOpenXMLWorkbookMacroEnabled
 End With

Dim strFolder As String, strTempFile As String
strFolder = ActiveWorkbook.path
If Len(strFolder) = 0 Then strFolder = CurDir
strFolder = strFolder & "\"
strTempFile = strFolder & "~tmpexport.bas"
On Error Resume Next
ActiveWorkbook.VBProject.VBComponents("Module2").Export strTempFile
Workbooks(filename1).VBProject.VBComponents.import strTempFile
Kill strTempFile
excel vba
1个回答
0
投票

我修复代码的建议

Dim path As String
Dim filename1 As String
filename1 = Worksheets("Instructions").Range("P1").Text & " Tech Eval"
path = ThisWorkbook.path & "\"


Worksheets(Array("Narrative", "Summary Page", "Exceptions", "Offer Rating Sheet")).Copy

Dim tgWkb As Workbook
Set tgWkb = ActiveWorkbook  '

With tgWkb
    .SaveAs Filename:=path & filename1, FileFormat:=xlOpenXMLWorkbookMacroEnabled
End With

Dim strFolder As String, strTempFile As String
strFolder = ActiveWorkbook.path
If Len(strFolder) = 0 Then strFolder = CurDir
strFolder = strFolder & "\"
strTempFile = strFolder & "~tmpexport.bas"
'On Error Resume Next
'You have to refer to the right workbooks
ThisWorkbook.VBProject.VBComponents("Module2").Export strTempFile
tgWkb.VBProject.VBComponents.Import strTempFile
Kill strTempFile
© www.soinside.com 2019 - 2024. All rights reserved.