2015-07-10 77 views
3

我在Microsoft Excel中準備了一個包含許多用戶表單和宏的VBA項目。我想導出所有的文件,但是看起來你只能一個接一個地做,這會花費很長時間。如何在Excel中導出VBAProject

有沒有辦法導出整個項目?謝謝!

+3

請參閱以下[鏈接](http://www.rondebruin.nl/win/s9/win002.htm) – 0m3r

+1

將它變成加載項如何? –

+1

http://www.appspro.com/Utilities/CodeCleaner.htm是我用它來做的。不過,我認爲它不適用於64位辦公室。 –

回答

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 

在傳遞路徑變量是通向你要導出代碼的文件。如果你有多個文件,只需要在循環中使用這個函數。