如何在Excel中导出VBAProject

我在Microsoft Excel中准备了一个具有许多用户表单和macros的VBA项目。 我想导出所有的文件,但似乎只能一个一个地做,这会花费很长时间。

有没有办法导出整个项目? 谢谢!

以下是我用来导出VBA代码的一些VBA代码:

'Requires Microsoft Visual Basic for Applications Extensibility Private Function exportvba(Path As String) Dim objVbComp As VBComponent Dim strPath As String Dim varItem As Variant Dim fso As New FileSystemObject Dim filename As String filename = fso.GetFileName(Path) On Error Resume Next MkDir ("C:\Create\directory\for\VBA\Code\" & filename & "\") On Error GoTo 0 'Change the path to suit the users needs strPath = "C:\Give\directory\to\save\Code\in\" & filename & "\" For Each varItem In ActiveWorkbook.VBProject.VBComponents Set objVbComp = varItem Select Case objVbComp.Type Case vbext_ct_StdModule objVbComp.Export strPath & "\" & objVbComp.name & ".bas" Case vbext_ct_Document, vbext_ct_ClassModule ' ThisDocument and class modules objVbComp.Export strPath & "\" & objVbComp.name & ".cls" Case vbext_ct_MSForm objVbComp.Export strPath & "\" & objVbComp.name & ".frm" Case Else objVbComp.Export strPath & "\" & objVbComp.name End Select Next varItem End Function 

传入的Pathvariables是要从中导出代码的文件的path。 如果你有多个文件,只需要在循环中使用这个函数。