1

问题:我发现许多帖子(包括 Microsoft 自己的支持站点)包含有关在 Excel 中某些单元格更改时如何运行宏的信息。我可以让它工作,但我需要将该子存储在我的 Personal.xlsb 工作簿中,并让它影响另一个工作簿中的命名工作表。

背景:我经常从第三方收到一个 Excel 文件并在其上运行一系列例程,所有宏都存储在我的 Personal.xlsb 隐藏工作簿中。该过程的一部分要求我“撤消”当用户在特定单元格中输入日期时自动进行的许多格式更改。一旦用户在给定的 5 个工作表中指定的单元格中输入日期,我想在 5 个单独的工作表上调用这些“撤消”子项。但是,我已经在线阅读的所有关于此的帮助都让我将代码添加到需要进行更改的确切工作表中。我想将该代码存储在我的个人工作簿中的一个模块中,以便它可以在任何包含工作表名称的工作簿上运行......类似于我布置其他查询的方式。

到目前为止的代码

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Set KeyCells = Range("W9")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
        MsgBox "You changed THE CELL!"
    End If
End Sub

问题:如何修改子以使其在包含指定工作表的所有工作簿中生效?或者在外面的某个地方对我来说有更好的选择吗?

4

1 回答 1

0

由于我遇到了同样的问题,因此我按照 Chip Pearson 的说明进行了工作,并将为您提供更全面的指南。

我还可以鼓励您阅读Chip Pearson对事件的精彩总结。我知道要消化的内容很多,但这将极大地帮助您了解我们在这里实际所做的事情。

作为概述,这些是我们将要执行的步骤:

  1. 为我们的事件处理程序添加一个类模块(这是实际事件处理代码所在的位置)
  2. 将事件处理程序添加到我们的personal.xlsbThisWorkbook模块以连接我们的事件处理程序类
  3. 享受无限的事件处理循环,因为我们更改了Target.Value:-P (可选

让我们开始吧。

1.添加类模块

首先,我们需要创建一个舒适的广场来放置我们所有的事件处理程序。我们将为此使用一个类模块,因为它提供了一些很好的结构,并且您可以立即知道在哪里寻找您的应用程序级事件处理程序。

在您personal.xlsb的 VBA 项目中,通过右键单击项目浏览器 ->插入->类模块来创建一个新类。

通过在属性窗格中更改名称,将类重命名为CAppEventHandler

将下面列出的源代码(设置部分和事件处理部分)添加到您刚刚创建的类中。(请务必阅读评论,因为它们为我们正在做的事情和原因添加了一些额外的信息。

2.添加初始化事件处理程序

现在我们需要确保我们的事件处理程序在我们personal.xlsb打开时被“激活”(所以无论何时打开 Excel :))。

为此,请双击您的ThisWorkbook模块并添加以下代码。

现在您实际上已经可以开始测试您的事件处理了。您必须事先做的唯一一件事(为了触发“接线”过程)是重新启动 Excel

笔记

  • 我在事件处理程序的开头添加了对当前工作表名称的检查 - 因此请确保您的工作表名为“MySheet”:)
  • 您可以在处理程序中设置断点并像任何其他 VBA 代码一样观察它的运行情况

还有一句警告(又名3. 享受无限的事件处理循环
当我测试这个时,我天真地使用Target.Value = "It wooooorks!"了当我们更改右侧工作表上的右侧单元格时要执行的代码。那不好。有关如何防止此类事件循环的信息
,请参阅最后一段代码(也取自Chip 的帖子)。

源代码

CAppEventHandler课堂上(设置)

Option Explicit

' Declare our own reference to the Application object
' WithEvents is needed to capture the events of the application object
' (Note: The events 'bubble up' from the Worksheet to the Workbook and
'        finally to the Application.
'        So event handlers in the Sheet module are executed first
'        (if any handlers are declared), then the ones in the Workbook
'        module (again, if they are declared) and finally the ones
'        in the Application module.)
Private WithEvents App As Application

' Whenever a new object of a class is instantiated, the _Initialize-Sub is called,
' that's why we use this Sub to get the reference to the current Application object
Private Sub Class_Initialize()
    Set App = Application
End Sub

CAppEventHandler课堂上(实际事件处理)

' Here is the actual code executed whenever the event reaches the Application level
' (see above for the order of 'bubbling up' of the events) and hasn't been marked
' as handled before
Private Sub App_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    If Sh.Name <> "MySheet" Then
        Exit Sub
    End If

    Dim rngKeyCells As Range
    Set rngKeyCells = Sh.Range("A5")

    If Intersect(rngKeyCells, Target) Is Nothing Then
        Exit Sub
    End If

    ' Do our actual work
    MsgBox "It wooooorks!", vbInformation

    ' Note: If you want to change the contents of your keyCells, you will
    '       have to make sure to prevent an infinite event loop (i.e. using
    '       Application.EnableEvents = False because otherwise changing
    '       the value in your macro will trigger the event again

End Sub

ThisWorkbook模块中

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' The following code must be placed in the "ThisWorkbook" module of your personal.xlsb
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Option Explicit

Private OurEventHandler As CAppEventHandler

Private Sub Workbook_Open()
    ' Since we declared so in our _Initialize-Sub this wire-up the current
    ' Application object in our EventHandler class
    Set OurEventHandler = New CAppEventHandler
End Sub

如何防止事件循环

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    Target.Value = Target.Value + 1
    Application.EnableEvents = True
End Sub

(来自Chip Pearson 关于事件的综合帖子

于 2015-11-09T17:30:49.540 回答