This will create a folder using the base name of your Excel file, which is the filename without its extension, along with a vba subfolder. Your VBA modules will be placed there. Pass UseTimestamp:=True to put each export in its own timestamped subfolder so repeated runs don’t overwrite earlier exports.
Set Reference to Microsoft Visual Basic for Applications Extensibility
Sub ExportModules( _
Optional PathToVBAModules As String = "", _
Optional UseTimestamp As Boolean = False _
)
Dim objMyProj As VBProject
Dim objVBComp As VBComponent
Dim strExt As String
Dim strBase As String
Dim strTimestamp As String
Set objMyProj = Application.VBE.ActiveVBProject
If PathToVBAModules = "" Then
strBase = ThisWorkbook.Path & "" & GetBaseName(ThisWorkbook.Name) & ""
MakeFolder strBase
If UseTimestamp Then
strTimestamp = Format(Now, "yyyymmdd_hhmmss")
PathToVBAModules = strBase & "vba_" & strTimestamp & ""
Else
PathToVBAModules = strBase & "vba"
End If
MakeFolder PathToVBAModules
End If
For Each objVBComp In objMyProj.VBComponents
Select Case objVBComp.Type
Case vbext_ct_StdModule
strExt = ".bas"
Case vbext_ct_ClassModule
strExt = ".cls"
Case vbext_ct_MSForm
strExt = ".frm"
Case vbext_ct_Document
strExt = ".txt"
Case Else
strExt = ".txt"
End Select
If objVBComp.CodeModule.CountOfLines > 0 Then
objVBComp.Export PathToVBAModules & objVBComp.Name & strExt
End If
Next
MsgBox "Modules exported to " & PathToVBAModules
End Sub