在Excel中使用VBA文件添加一个模块的内容
如果您不想添加完整的模块,则可以使用下面的宏将缺少的过程仅添加到现有模块中。它将文本文件的内容添加到现有模块:
Sub ImportModuleCode(ByVal wb As Workbook, _ ByVal ModuleName As String, ByVal ImportFromFile As String) ' imports code to ModuleName in wb from a textfile named ImportFromFile Dim VBCM As CodeModule If Dir(ImportFromFile) = "" Then Exit Sub On Error Resume Next Set VBCM = wb.VBProject.VBComponents(ModuleName).CodeModule If Not VBCM Is Nothing Then VBCM.AddFromFile ImportFromFile Set VBCM = Nothing End If On Error GoTo 0 End Sub
例如:
ImportModuleCode ActiveWorkbook, "TestModule", "C:\FolderName\NewCode.txt"