Prendendo un ulteriore passo avanti nella risposta di @Demosthenex, se desideri tenere traccia anche del codice nei tuoi oggetti Microsoft Excel e UserForm devi diventare un po 'complicato.
Innanzitutto ho modificato la mia SaveCodeModules()funzione per tenere conto dei diversi tipi di codice che intendo esportare:
Sub SaveCodeModules(dir As String)
'This code Exports all VBA modules
Dim moduleName As String
Dim vbaType As Integer
With ThisWorkbook.VBProject
For i = 1 To .VBComponents.count
If .VBComponents(i).CodeModule.CountOfLines > 0 Then
moduleName = .VBComponents(i).CodeModule.Name
vbaType = .VBComponents(i).Type
If vbaType = 1 Then
.VBComponents(i).Export dir & moduleName & ".vba"
ElseIf vbaType = 3 Then
.VBComponents(i).Export dir & moduleName & ".frm"
ElseIf vbaType = 100 Then
.VBComponents(i).Export dir & moduleName & ".cls"
End If
End If
Next i
End With
End Sub
I form utente possono essere esportati e importati proprio come il codice VBA. L'unica differenza è che verranno creati due file quando viene esportato un modulo (otterrai .frmun .frxfile e un file per ciascun Form utente). Uno di questi contiene il software che hai scritto e l'altro è un file binario che (ne sono abbastanza sicuro) definisce il layout del modulo.
Microsoft Excel Objects (MEO) (che significa Sheet1, Sheet2, ThisWorkbookecc) possono essere esportati come .clsfile. Tuttavia, quando si desidera ripristinare questo codice nella cartella di lavoro, se si tenta di importarlo nello stesso modo in cui si farebbe un modulo VBA, verrà visualizzato un errore se tale foglio esiste già nella cartella di lavoro.
Per ovviare a questo problema, ho deciso di non provare a importare il file .cls in Excel, ma invece di leggere il .clsfile in Excel come stringa, quindi incollare questa stringa nel MEO vuoto. Ecco i miei ImportCodeModules:
Sub ImportCodeModules(dir As String)
Dim modList(0 To 0) As String
Dim vbaType As Integer
' delete all forms, modules, and code in MEOs
With ThisWorkbook.VBProject
For Each comp In .VBComponents
moduleName = comp.CodeModule.Name
vbaType = .VBComponents(moduleName).Type
If moduleName <> "DevTools" Then
If vbaType = 1 Or _
vbaType = 3 Then
.VBComponents.Remove .VBComponents(moduleName)
ElseIf vbaType = 100 Then
' we can't simply delete these objects, so instead we empty them
.VBComponents(moduleName).CodeModule.DeleteLines 1, .VBComponents(moduleName).CodeModule.CountOfLines
End If
End If
Next comp
End With
' make a list of files in the target directory
Set FSO = CreateObject("Scripting.FileSystemObject")
Set dirContents = FSO.getfolder(dir) ' figure out what is in the directory we're importing
' import modules, forms, and MEO code back into workbook
With ThisWorkbook.VBProject
For Each moduleName In dirContents.Files
' I don't want to import the module this script is in
If moduleName.Name <> "DevTools.vba" Then
' if the current code is a module or form
If Right(moduleName.Name, 4) = ".vba" Or _
Right(moduleName.Name, 4) = ".frm" Then
' just import it normally
.VBComponents.Import dir & moduleName.Name
' if the current code is a microsoft excel object
ElseIf Right(moduleName.Name, 4) = ".cls" Then
Dim count As Integer
Dim fullmoduleString As String
Open moduleName.Path For Input As #1
count = 0 ' count which line we're on
fullmoduleString = "" ' build the string we want to put into the MEO
Do Until EOF(1) ' loop through all the lines in the file
Line Input #1, moduleString ' the current line is moduleString
If count > 8 Then ' skip the junk at the top of the file
' append the current line `to the string we'll insert into the MEO
fullmoduleString = fullmoduleString & moduleString & vbNewLine
End If
count = count + 1
Loop
' insert the lines into the MEO
.VBComponents(Replace(moduleName.Name, ".cls", "")).CodeModule.InsertLines .VBComponents(Replace(moduleName.Name, ".cls", "")).CodeModule.CountOfLines + 1, fullmoduleString
Close #1
End If
End If
Next moduleName
End With
End Sub
Nel caso in cui tu sia confuso dirdall'input di entrambe queste funzioni, questo è solo il tuo repository di codice! Quindi, chiamereste queste funzioni come:
SaveCodeModules "C:\...\YourDirectory\Project\source\"
ImportCodeModules "C:\...\YourDirectory\Project\source\"