vba - 从 Excel 电子表格中提取 VBA

标签 vba excel version-control

有没有一种干净的方法可以从电子表格中提取 VBA 并将其存储在存储库中。

电子表格几乎不会改变,但 VBA 会改变。将整个电子表格存储在存储库中使得很难在不同的 VBA 修订版之间进行比较,以了解哪些代码已更改以及谁更改了它。

我们使用 VBA 6.5/Excel 2003。

最佳答案

以前版本的 Excel 和 Access(2003 年之前)通过加载项支持 VBA 源代码版本控制。我在 Office 2000 中非常有效地使用了这一点。

Visual SourceSafe (VSS) 对 VBA 的支持是 dropped with the release of Office 2003 ,但 Office XP Developer 附带的加载项显然是 works with Office 2003 .

微软知识库文章:

如果失败,您可以使用此代码提取 VBA 代码(来自 here 但缺少最终的对象清理)。阅读网页以了解注意事项:

option explicit

Const vbext_ct_ClassModule = 2
Const vbext_ct_Document = 100
Const vbext_ct_MSForm = 3
Const vbext_ct_StdModule = 1

Main

Sub Main
    Dim xl
    Dim fs
    Dim WBook
    Dim VBComp
    Dim Sfx
    Dim ExportFolder

    If Wscript.Arguments.Count <> 1 Then
        MsgBox "As the only argument, give the FULL path to an XLS file to extract all the VBA from it."
    Else

        Set xl = CreateObject("Excel.Application")
        Set fs = CreateObject("Scripting.FileSystemObject")

        xl.Visible = true

        Set WBook = xl.Workbooks.Open(Trim(wScript.Arguments(0)))

        ExportFolder = WBook.Path & "\" & fs.GetBaseName(WBook.Name)

        fs.CreateFolder(ExportFolder)

        For Each VBComp In WBook.VBProject.VBComponents
            Select Case VBComp.Type
                Case vbext_ct_ClassModule, vbext_ct_Document
                    Sfx = ".cls"
                Case vbext_ct_MSForm
                    Sfx = ".frm"
                Case vbext_ct_StdModule
                    Sfx = ".bas"
                Case Else
                    Sfx = ""
            End Select
            If Sfx <> "" Then
                On Error Resume Next
                Err.Clear
                VBComp.Export ExportFolder & "\" & VBComp.Name & Sfx
                If Err.Number <> 0 Then
                    MsgBox "Failed to export " & ExportFolder & "\" & VBComp.Name & Sfx
                End If
                On Error Goto 0
            End If
        Next

        xl.Quit

        Set fs = Nothing
        Set xl = Nothing

    End If
End Sub

关于vba - 从 Excel 电子表格中提取 VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/846565/

相关文章:

javascript - 如何将 VBA 转换为 Javascript,以便将启用宏的 Excel 工作簿上传到 Google Drive?

excel - 使用 Offset、Vlookup 和 Concatenate 返回列表

vba - 将带有数据的行从另一个工作表动态插入到 Excel 工作表中

excel - 将不同文件中的多个工作表中的数据导入单个工作簿

excel - 如何复制单元格格式和样式

excel - excel中如何计算小时数

git - 如何查看git存储库中已经存在的行尾类型?

Git - 从一个远程仓库 pull ,推送到另一个 : can I push names of remotes?

svn - 源代码控制 – 每个产品都需要一个单独的分支吗?

excel - VBA For 循环,其中每个答案必须 = True