Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
674 views
in Technique[技术] by (71.8m points)

excel - VBA Worksheet Change Event

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

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

Private Sub Worksheet_Change(ByVal Target As Range)
    Range("J1").Select
    If Target.Address = "$J$1" 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...