以编程方式从excel文件中提取excel vbamacros

我有很多我写了多年的Excelmacros,我想将它们编译成一个文档或模块,其中包含我最常使用的function(写得非常模块化,可以被其他人重用)。

任何人都知道如何编程访问使用VBA或其他自动化的Excel VBA模块?

这个主题在这里被覆盖了:

培生的编程VBA编辑器

这个代码

  • 打开由StrDir指定的目录中的所有xlsm文件(本例中为C:\ temp
  • 如果该模块中至less有一行代码,则将每个代码组件导出到由StrDir2C:\ mycode )指定的第二个目录

 Sub GetCode() Dim WB As Workbook Dim VBProj Dim VBComp Dim StrDir As String Dim StrDir2 As String Dim StrFile As String StrDir = "c:\temp\" StrDir2 = "c:\mycode\" If Len(Dir(StrDir2, vbDirectory)) = 0 Then MkDir StrDir2 StrFile = Dir(StrDir & "*.xlsm") With Application .ScreenUpdating = False .EnableEvents = False End With Do While Len(StrFile) > 0 Set WB = Workbooks.Open(StrDir & StrFile, False) Set VBProj = WB.VBProject For Each VBComp In VBProj.vbcomponents If VBComp.codemodule.countoflines > 0 Then VBComp.Export StrDir2 & StrFile & "_" & VBComp.Name & ".txt" Next WB.Close False StrFile = Dir Loop With Application .ScreenUpdating = True .EnableEvents = True End With End Sub