![](/img/trans.png)
[英]Clear contents of one cell when another is changed - for multiple cells
[英]Clear cell contents if another is changed for a range of cells in vba
我在C8到FU8中有下拉列表,在C9到FU9中有下拉列表,这取决于在第一个下拉列表中选择的值。 如果C8到FU8中的值已更改,我正在尝试清除C9到FU9的单元格内容。 当我指定单个单元格时,我设法使它起作用
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
For Each Cell In Target
If Cell.Address = "$C$8" Then
Application.EnableEvents = False
Range("C9").ClearContents
Application.EnableEvents = True
End If
Next Cell
End Sub
但是,我无法使它适用于整个单元格范围。 我还希望每一列都是独立的,因此,如果更改D8中的值,则不会清除范围C9:FU9中所有单元格的内容,而只会清除D9中的内容。
任何帮助将不胜感激,谢谢!
您的第一段听起来像是您希望在更新单个值时清除所有值。
您的最后一段说您希望更改后的单元格下方的单元格清晰可见-听起来更有可能,因此我将给出答案:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
'Check the cell being changed is in the range C8:FU8.
If Not Intersect(Target, Range("C8:FU8")) Is Nothing Then
'Clear the cell below the one that was changed.
Target.Offset(1).ClearContents
End If
Application.EnableEvents = True
End Sub
尝试检查更改的单元格是否在所讨论的列中(在此示例中为3),然后清除在同一行中但列为FU的单元格(177)。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myCell As Range
For Each myCell In Target
If myCell.Column= 3 Then
Application.EnableEvents = False
Cells(myCell.Row, 177).ClearContents
Application.EnableEvents = True
End If
Next myCell
End Sub
另外,变量名称Cell使我感到困惑,我更想清楚地表明这是一个变量,而不是现有的Excel对象-因此将其重命名为myCell。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.