![](/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.