如何使用中央存储库通过 VBA 代码管理多个分布式工作簿

发布于 2025-01-09 11:20:56 字数 96 浏览 0 评论 0原文

在 VBA for excel 中,我有多个依赖相同代码的工作簿。将它们全部更新是一件痛苦的事情。如何在运行时从网络文件夹导入模块,以便我可以保存更改一次,并让所有文件自动更新?

Within VBA for excel, I have multiple workbooks that rely on the same code. It’s a pain to update them all. How can I import modules from a network folder at runtime, so that I can save changes once, and have all the files auto update?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朱染 2025-01-16 11:20:56

这是我想出的。如果您有改进我的实施的想法,请发表评论。如果您有自己认为更好的解决方案,请添加您自己的答案!谢谢!

在“ThisWorkook”模块中:

Option Explicit
'Requires reference to "Microsoft Visual Basic for Applications Extensibility"
Const RepPath As String = "X:\MyNetworkLocation\"
Const Deprecator As String = "_DEP"
    
Private Sub Workbook_Open()
    'VBA has trouble managing the removal and adding of code modules
    'within a single procedure, so we'll handle this in two steps.
    LoadCode
    RemoveDeprecated

    'Using Application.Ontime to get around potential compile errors caused by
    'subs/functions being referenced in modules that don't get loaded until runtime
    Application.OnTime Now(), "DoSomething"
    Application.OnTime Now(), "DoSomethingElse"
End Sub

Private Sub LoadCode()
    'This sub loads code from a central repository directory
    Dim vbP As VBIDE.vbProject
    Dim vbC As VBIDE.VBComponent
    Dim FileName As String
    Dim CodeName As String
    
    Set vbP = Application.ThisWorkbook.vbProject

    FileName = Dir(RepPath)
        Do While Len(FileName) > 0
            'get the module name without the file extension
            CodeName = Left(FileName, InStrRev(FileName, ".") - 1)
            Select Case CodeName
                'Using Select Case to ignore certain modules...
                Case "ThisWorkbook"
                'do nothing
                Case "CodeLoader"
                'do nothing
                Case Else
                'test if module exists in VB Project
                    On Error Resume Next
                    Set vbC = vbP.VBComponents(CodeName)
                    On Error GoTo 0
                    'if the module already exists, we need to remove it
                    'VBA struggles with doing this within a single procedure,
                    'so for now, we'll just rename it
                    If Not vbC Is Nothing Then vbC.Name = vbC.Name & Deprecator
                    'load the new code module
                    vbP.VBComponents.Import RepPath & FileName
            End Select
            'reset variables
            Set vbC = Nothing
            CodeName = ""
            'next file
            FileName = Dir
        Loop

End Sub

Private Sub RemoveDeprecated()
    'This sub removes the deprecated code modules that we previously renamed
    Dim vbP As VBIDE.vbProject
    Dim vbC As VBIDE.VBComponent
    Set vbP = Application.ThisWorkbook.vbProject
    
    For Each vbC In vbP.VBComponents
        If InStr(1, vbC.Name, Deprecator) > 0 Then vbP.VBComponents.Remove vbC
    Next

End Sub

Here is what I came up with. If you have an idea to improve my implementation, please comment. If you have your own solution you think is better, please add your own answer! Thanks!

In the "ThisWorkook" module:

Option Explicit
'Requires reference to "Microsoft Visual Basic for Applications Extensibility"
Const RepPath As String = "X:\MyNetworkLocation\"
Const Deprecator As String = "_DEP"
    
Private Sub Workbook_Open()
    'VBA has trouble managing the removal and adding of code modules
    'within a single procedure, so we'll handle this in two steps.
    LoadCode
    RemoveDeprecated

    'Using Application.Ontime to get around potential compile errors caused by
    'subs/functions being referenced in modules that don't get loaded until runtime
    Application.OnTime Now(), "DoSomething"
    Application.OnTime Now(), "DoSomethingElse"
End Sub

Private Sub LoadCode()
    'This sub loads code from a central repository directory
    Dim vbP As VBIDE.vbProject
    Dim vbC As VBIDE.VBComponent
    Dim FileName As String
    Dim CodeName As String
    
    Set vbP = Application.ThisWorkbook.vbProject

    FileName = Dir(RepPath)
        Do While Len(FileName) > 0
            'get the module name without the file extension
            CodeName = Left(FileName, InStrRev(FileName, ".") - 1)
            Select Case CodeName
                'Using Select Case to ignore certain modules...
                Case "ThisWorkbook"
                'do nothing
                Case "CodeLoader"
                'do nothing
                Case Else
                'test if module exists in VB Project
                    On Error Resume Next
                    Set vbC = vbP.VBComponents(CodeName)
                    On Error GoTo 0
                    'if the module already exists, we need to remove it
                    'VBA struggles with doing this within a single procedure,
                    'so for now, we'll just rename it
                    If Not vbC Is Nothing Then vbC.Name = vbC.Name & Deprecator
                    'load the new code module
                    vbP.VBComponents.Import RepPath & FileName
            End Select
            'reset variables
            Set vbC = Nothing
            CodeName = ""
            'next file
            FileName = Dir
        Loop

End Sub

Private Sub RemoveDeprecated()
    'This sub removes the deprecated code modules that we previously renamed
    Dim vbP As VBIDE.vbProject
    Dim vbC As VBIDE.VBComponent
    Set vbP = Application.ThisWorkbook.vbProject
    
    For Each vbC In vbP.VBComponents
        If InStr(1, vbC.Name, Deprecator) > 0 Then vbP.VBComponents.Remove vbC
    Next

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文