Clear cell content based on another cell calculation

人走茶凉 提交于 2020-01-05 05:56:12

问题


this is an Excel/VBA question:

I have a cell A1 in sheet2 linked cell A1 in sheet1 (simply A1='sheet1'!A1). A1 in sheet1 is a data validation drop down menu.

I want to clear the content of A2 in sheet2 every time the content of A1 in sheet2 changes/is updated. That is every time the value of A1 in sheet1 is changed using the drop down menu.

I tried using a Worksheet_Change event macro (which I do not fully understand) but it won't work with a cell that updates from a calculation. It also doesn't work if triggered from a cell from another worksheet (I tried linking it to cell A1 on sheet1 in this case).

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    Range("A2").ClearContents
End Sub

Can you think of a simple solution to clear the content of cell A2 in sheet2 when A1 in sheet2 updates?


回答1:


This works for me...

This code goes in the Sheet code area of Sheet1

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1")) Is Nothing Then _
    Sheets("Sheet2").Range("A2").ClearContents
End Sub


来源:https://stackoverflow.com/questions/13995119/clear-cell-content-based-on-another-cell-calculation

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