简体   繁体   English

VBA Excel:关闭但不是从Workbook_BeforeClose函数调用宏?

[英]VBA Excel: call a macro on close but not from the Workbook_BeforeClose function?

Simply put chaps I want to call a macro just before a workbook closes. 简单地说,我想在工作簿关闭之前调用一个宏。 You may say why not just put it into the ThisWorkbook section? 您可能会说为什么不将它放入ThisWorkbook部分? The reason is that I am uploading a code module via VBA when the book is initially created. 原因是我在最初创建本书时通过VBA上传代码模块。 So there are basically two solution to this I just cannot figure it out 所以基本上有两个解决方案,我只是想不出来

  1. Call a sub based in a standard module before the close of the book 在本书结束之前调用基于标准模块的子
  2. Find a way to upload with a VBA procedure a code sub into the ThisWorkbook section when the book is created 在创建工作簿时,找到一种使用VBA过程将代码子上载到ThisWorkbook部分的方法

Any suggestion? 有什么建议吗?

I agree with Gary that it's probably best to use templates for this, but in any case here is an example of using VBA to add code to an existing VBModule, and to add a new standard module with code. 我同意加里(Gary)的观点,也许最好是使用模板,但是在任何情况下,这里都是使用VBA向现有VBModule添加代码并添加带有代码的新标准模块的示例。

Obviously, the more complicated are the procedure's you're trying to add, the more difficult it will be to do this by building the strings in the procedure. 显然,您尝试添加的过程越复杂,通过在过程中构建字符串来实现此操作就越困难。 You can add modules from file (assuming you've exported them -- which you can also do via VBA...) or you can also copy from existing modules in open workbooks, etc. For how to do these things, please refer to the link provided below. 您可以从文件中添加模块(假设您已经导出它们 - 您也可以通过VBA执行...)或者您也可以从打开的工作簿中的现有模块中复制等。有关如何执行这些操作,请参阅以下提供的链接。

NOTE You will need to enable the application to Trust Access to the VBA Project Object Model (File > Options > Trust Center > Trust Center Settings > Macro Settings > Trust Access to the VBA Project Object Model) 注意您需要启用应用程序以信任对VBA项目对象模型的访问(文件>选项>信任中心>信任中心设置>宏设置>信任访问VBA项目对象模型)

Option Explicit

Sub AddWorkbookWithCode()

Dim wb As Workbook
Set wb = Workbooks.Add

AddVBCodeToWorkbook wb

End Sub
Sub AddVBCodeToWorkbook(wb As Workbook)
Dim procString$

procString = "Sub Workbook_BeforeClose()"
procString = procString & vbCrLf & vbTab & "On Error Resume Next"
procString = procString & vbCrLf & vbTab & "Call SayGoodby"
procString = procString & vbCrLf & "End Sub"

'## Insert some code to ThisWorkbook module:
With wb.VBProject.VBComponents("ThisWorkbook")
    .CodeModule.AddFromString procString
End With

'## Add a new standard module and insert some code there, too:
procString = "Public Sub SayGoodby()"
procString = procString & vbCrLf & vbTab & "MsgBox ""Goodby!"""
procString = procString & vbCrLf & "End Sub"

With wb.VBProject.VBComponents.Add(1)
    .Name = "Module1"
    .CodeModule.AddFromString procString
End With



End Sub

HERE is some good reference material on programming the VBE. 是VBE编程的一些很好的参考资料。

Say at run-time we insert a module and in that module place a Public sub: 假设在运行时我们插入一个模块,然后在该模块中放置一个Public子目录:

在此输入图像描述

Then in the workbook code area: 然后在工作簿代码区域中:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    On Error Resume Next
    Call SayGoodby
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM