vba 根据另一个单元格计算清除单元格内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13995119/
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 18:55:52  来源:igfitidea点击:

Clear cell content based on another cell calculation

excelvbaexcel-vba

提问by zozo23

this is an Excel/VBA question:

这是一个 Excel/VBA 问题:

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

我在sheet2链接的单元格A1中有一个单元格A1 sheet1(简单地A1='sheet1'!A1)。A1insheet1是一个数据验证下拉菜单。

I want to clear the content of A2in sheet2every time the content of A1in sheet2 changes/is updated. That is every time the value of A1in sheet1is changed using the drop down menu.

每次更改/更新 in sheet2的内容时,我都想清除A2insheet2的内容A1。即每次使用下拉菜单更改A1in的值时sheet1

I tried using a Worksheet_Changeevent 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 A1on sheet1in this case).

我尝试使用Worksheet_Change事件宏(我不完全理解),但它不适用于从计算更新的单元格。它还如果从另一个工作表(我试着将其链接到细胞的细胞触发的不工作A1sheet1在这种情况下)。

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 A2in sheet2when A1in sheet2updates?

你能想到一个简单的解决方案,以清除细胞的含量A2sheet2A1sheet2更新?

回答by Siddharth Rout

This works for me...

这对我有用...

This code goes in the Sheet code area of Sheet1

此代码位于 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