Restrict Worksheet_Change to a specified range of cells

痴心易碎 提交于 2021-01-29 19:25:30

问题


I want to record a list of live data in a separate sheet.

Found this code online which works.

How to do I change the range from one cell A1 to a Range A1:D30?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim dest As Range
Application.EnableAnimations = False
On Error GoTo line1
If Target.Address <> "$A$1" Then GoTo line1
    Set dest = Worksheets("sheet2").Cells(Rows.Count, "a").End(xlUp).Offset(1, 0)
    'MsgBox dest.Address
    Target.Copy dest
line1:
Application.EnableEvents = True
End Sub

回答1:


This can be done without a custom function. VBA already contains all you need.

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:D30")) Is Nothing Then

    ' run some code
End If

End Sub


来源:https://stackoverflow.com/questions/63210200/restrict-worksheet-change-to-a-specified-range-of-cells

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!