VBA 工作表更改事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10474356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:03:11  来源:igfitidea点击:

VBA Worksheet Change Event

excelvba

提问by Luke

I am trying to use the Worksheet Change Event in Excel VBA, but it doesn't seem to work.

我正在尝试在 Excel VBA 中使用工作表更改事件,但它似乎不起作用。

From what I gather, it is enough to just define the handling function "Worksheet_Change" as I have done here:

从我收集的信息来看,只定义处理函数“Worksheet_Change”就足够了,就像我在这里所做的那样:

Private Sub Worksheet_Change(ByVal Target As Range)
    Range("J1").Select
    If Target.Address = "$J" And ActiveCell.Value = 1 Then
        Range("B1").Select
        Dim c As Integer
        c = ActiveCell.Value
        c = c + 1
       ActiveCell.Value = c
    End If
End Sub

The problem is that I am not sure exactly where I am supposed to define it. I have just put it in "module1" which was automatically generated when I made my first macro. Is this correct? I am quite new to VBA, so I don't know much about it yet.

问题是我不确定我应该在哪里定义它。我刚刚把它放在“module1”中,它是我制作第一个宏时自动生成的。这样对吗?我对 VBA 很陌生,所以我对它还不太了解。

回答by assylias

You need to put it in the sheet that it applies to. In other words, if you want to capture change events on Sheet1, in the VBA editor, you need to put it in VBAProject (Book_Name) > Microsoft Excel Objects > Sheet1.

你需要把它放在它适用的工作表中。换句话说,如果要捕获Sheet1上的更改事件,则在VBA编辑器中,需要将其放在VBAProject (Book_Name) > Microsoft Excel Objects > Sheet1 中。

enter image description here

在此处输入图片说明