我在Microsoft Excel中准备了一个包含许多用户表单和宏的VBA项目。我想导出所有文件,但看起来你只能逐个进行,这需要我很长时间。
有没有办法导出整个项目?谢谢!
答案 0 :(得分:2)
以下是我用来导出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
传入的Path变量是您要从中导出代码的文件的路径。如果您有多个文件,只需在循环中使用此功能。