【发布时间】:2021-08-20 21:32:16
【问题描述】:
对 VBA 如此陌生,几乎可以正常工作。我正在尝试重置一些数据验证列表,以便如果有人更改国家/地区选择,那么几个单元格将重置。例如,如果我选择 USA,那么我希望相应的 State 和 Shift 列显示“请选择...”,如果用户将国家/地区更改为 USA 以外的其他内容,则 State 列中不会显示任何内容,仅在班次列。我得到了这个工作,但它只在第一行运行。我不确定我的范围是否错误,或者我是否应该循环,这两者我完全一无所知。
Option Explicit
'The way this works is if the Payroll Country changes then the sub selections
'of State and Shift should reset based on if the country is USA
'Payroll Country = A column
'State = X column
'Shift = Y column
Private Sub Worksheet_Change(ByVal Target As Range)
'"If Target.Count > 1 Then Exit Sub" is the VBA code to prevent an error if user highlights the range and deletes the data
If Target.Count > 1 Then Exit Sub
If Target.Address = "$A$6" And Target.Value = "USA" Then
Range("X6").Value = "Please select..."
Range("Z6").Value = "Please select..."
ElseIf Target.Address = "$A$6" And Target.Value <> "USA" Then
Range("X6").Value = ""
Range("Z6").Value = "Please select..."
End If
End Sub
【问题讨论】:
-
它应该在什么范围内运行?您是否希望它对第 6 行及以下的 ColA 中的更改做出反应?
-
谢谢,是的,范围应该是从 ColA 到第 6 行,正确。