【问题标题】:Outside code for Excel 2003 macrosExcel 2003 宏的外部代码
【发布时间】:2012-06-03 11:17:54
【问题描述】:

有人知道如何在外部文件中编写 VBA 代码,每次您在 Excel 中运行此宏时,该代码都会“导入”到 Excel 2003 VBA 宏中 - 就像自我更新代码一样?

我的想法是我在外部编辑器 (VIm) 中编写代码,每次启动 Excel 宏时,宏都会检查更新的数据,如有必要,将代码导入内部然后运行它(可能是一个或多个函数/子程序) .

步骤是:打开Excel,打开XLS文件,点击按钮,宏启动,检查更新的外部数据(我在外部编辑器中编写的代码),如果数据是新的,删除旧代码,导入外部代码然后实际运行宏本身。

有人吗?

【问题讨论】:

  • 什么是outside editor (VIm)
  • 高级文本编辑器我用来编写各种东西,包括 Visual Basic 代码。一旦你学会了如何使用它,它就非常有用——比如外部编辑器。 VIm editor

标签: excel vba


【解决方案1】:

找到解决方案(部分来自http://www.cpearson.com/excel/vbe.aspx)10x Scott:

Sub AddOutsideCode()
    Dim VBProjekt As VBIDE.VBProject
    Dim VBKomponenta As VBIDE.VBComponent
    Set VBProjekt = ThisWorkbook.VBProject
    Call DeleteAllButThis(ThisWorkbook, "Fixed")
    Set VBKomponenta = VBProjekt.VBComponents.Import(Filename:="C:\bat\bat.bas")
    VBKomponenta.Name = "OutsideCode"
End Sub

'Delete all modules but named
Private Sub DeleteAllButThis(ByVal wb As Workbook, ByVal CompName As String)
    Application.DisplayAlerts = False
    On Error Resume Next
    For Each komponenta In ThisWorkbook.VBProject.VBComponents
        If komponenta.Name <> CompName Then
            If komponenta.Type = vbext_ct_StdModule Then
                ThisWorkbook.VBProject.VBComponents.Remove komponenta
            End If
        End If
    Next komponenta
    On Error GoTo 0
    Application.DisplayAlerts = True
End Sub

但是如何检查外部代码是否更改?

【讨论】:

【解决方案2】:

假设您使用this VIM(一个文本编辑器)来编写.bas 文件,那么您可以使用该站点Programming the VBE 中的许多功能来覆盖模块或过程。

所以,你的结构应该是这样的:

Sub MyMacro()

Overwrite MyModule in this Workbook with MyModule in .bas file (you could do line count checks or something if you need to, or just overwrite each time to ensure latest code

Call MyModule

End Sub

我认为,您想要合并一些明显的检查,以确保您的最新代码始终适用于工作簿...... 检查更新的外部数据(我在外部编辑器中编写的代码),如果数据是新的,则删除旧代码,导入外部代码,然后实际运行宏本身。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 2021-07-25
    • 1970-01-01
    相关资源
    最近更新 更多