从一个工作簿复制模块到另一个在Excel中使用VBA
使用下面的宏,您可以将模块从一个工作簿复制到另一个工作簿:
Sub CopyModule(SourceWB As Workbook, strModuleName As String, _ TargetWB As Workbook) ' copies a module from one workbook to another ' example: ' CopyModule Workbooks("Book1.xls"), "Module1", _ Workbooks("Book2.xls") Dim strFolder As String, strTempFile As String strFolder = SourceWB.Path If Len(strFolder) = 0 Then strFolder = CurDir strFolder = strFolder & "\" strTempFile = strFolder & "~tmpexport.bas" On Error Resume Next SourceWB.VBProject.VBComponents(strModuleName).Export strTempFile TargetWB.VBProject.VBComponents.Import strTempFile Kill strTempFile On Error GoTo 0 End Sub