Excel将模块/用户表单复制到宏创建的新工作簿

时间:2015-08-31 03:09:02

标签: excel excel-vba vba

这是我一直试图解决的问题。我有一个宏,根据从主工作簿(名为CSJ_Master)复制的一些工作表创建一个新的工作簿。但是我意识到模块/用户表单没有被复制过,因此当我将宏创建的文件发送给其他人时,他们无法使用我在工作簿中放置的宏指定按钮。以下是我在线使用一些教程拼凑的内容(注意:这是用户表单的代码):

Private Sub CommandButton1_Click()

Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Data List")

'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

'check for a Name number
If Trim(Me.ProjectName.Value) = "" Then
Me.ProjectName.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If

'copy the data to the database
ws.Cells(iRow, 1).Value = Me.ProjectName.Value
ws.Cells(iRow, 2).Value = Me.ProjectNumber.Value
ws.Cells(iRow, 3).Value = Me.SPVComp.Value
ws.Cells(iRow, 4).Value = Me.Contact.Value
ws.Cells(iRow, 5).Value = Me.ProjMan.Value
ws.Cells(iRow, 6).Value = Me.PODate.Value
ws.Cells(iRow, 7).Value = Me.PONumber.Value
ws.Cells(iRow, 8).Value = Me.PRNumber.Value
ws.Cells(iRow, 9).Value = Me.EstTime.Value


MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"

' Delete any old stray copies of the module1
On Error Resume Next
Kill ("C:\MrXL1.bas")
On Error GoTo 0
' Export Modules
ActiveWorkbook.VBProject.VBComponents(Array("Module1", "Module2", "Module3", "Module4", "Module5", "Module6", "Module7", "Module8", "Module9", "Module10", "Module11", "Module12", "UserForm1", "Userform2")).Export ("c:\MrXL1.bas")


Sheets(Array("Document Data", "Invoice data", "Hours", "Summary", "Invoice")).Copy
fname = Application.GetSaveAsFilename(InitialFileName:=Me.ProjectName.Value, FileFilter:="Excel Files (*.xlsm), *.xlsm")
ActiveWorkbook.SaveAs Filename:=fname, FileFormat:=52

Application.VBE.ActiveVBProject.VBComponents.Import ("c:\MrXL1.bas")


'clear the data
Me.ProjectName.Value = ""
Me.ProjectNumber.Value = ""
Me.SPVComp.Value = ""
Me.Contact.Value = ""
Me.ProjMan.Value = ""
Me.PODate.Value = ""
Me.PONumber.Value = ""
Me.PRNumber.Value = ""
Me.EstTime.Value = ""
Me.ProjectName.SetFocus

End Sub

此代码用于处理来自userform的数据,然后在输入后,它会创建一个新的工作簿,命名为ProjName的值。代码工作得很好,除了这一部分:

' Delete any old stray copies of the module1
On Error Resume Next
Kill ("C:\MrXL1.bas")
On Error GoTo 0
' Export Modules
ActiveWorkbook.VBProject.VBComponents(Array("Module1", "Module2", "Module3", "Module4", "Module5", "Module6", "Module7", "Module8", "Module9", "Module10", "Module11", "Module12", "UserForm1", "Userform2")).Export ("c:\MrXL1.bas")



Sheets(Array("Document Data", "Invoice data", "Hours", "Summary", "Invoice")).Copy
fname = Application.GetSaveAsFilename(InitialFileName:=Me.ProjectName.Value, FileFilter:="Excel Files (*.xlsm), *.xlsm")
ActiveWorkbook.SaveAs Filename:=fname, FileFormat:=52

Application.VBE.ActiveVBProject.VBComponents.Import ("c:\MrXL1.bas")

这是代码的主要部分,它复制工作表和模块/用户表单并将其保存到名为ProjName的新工作簿。但是,我总是得到运行时错误' 13' "类型不匹配"在这一行:

ActiveWorkbook.VBProject.VBComponents(Array("Module1", "Module2", "Module3", "Module4", "Module5", "Module6", "Module7", "Module8", "Module9", "Module10", "Module11", "Module12", "UserForm1", "Userform2")).Export ("c:\MrXL1.bas")

我怎样才能这样做呢?我对VBA很新,并且大多使用教程来达到这一点,所以如果你能详细解释一下,那将非常感激!

0 个答案:

没有答案