我正在 Excel 中构建一个复杂的工作簿,并尝试使其尽可能用户友好。
示例:
我有一个名为 Tbltst 的表,其中包含三列 Control #、Dollars 和 Returned。
控件编号是自动填充的,美元是用户输入的,返回的是验证下拉列表。
当我将 Returned 中的值从 N 更改为 Y 时,我希望该 Control# 的 Dollars 列中的所有金额都更改为 0,并且 Returned 的其他条目更改为 Match。
假设我更改了列表中的第一个 1234。 50 美元应该被清空,第 3 行和第 6 行也应该被清空,并且它们返回的列更新为 Y 以匹配第 1 行。
工作簿已锁定,Control# 不可编辑(如上所述由公式驱动),但 Dollars 和 Returned 均可编辑。
我确实尝试使用公式,但要根据上面的内容设置单元格需要宏。
用户在
Returned
列中的输入会触发Worksheet_Change
更新数据。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngTab As Range
Set rngTab = ActiveSheet.Range("Tbltst")
If Not Intersect(Target, rngTab) Is Nothing Then
With Target
If .CountLarge = 1 And .Cells(1).Column = 3 Then
If UCase(.Value) = "Y" Then
Dim sKey, c
sKey = Cells(.Row, 1)
Application.EnableEvents = False
For Each c In rngTab.Columns(1).Cells
If c.Value = sKey Then
Cells(c.Row, 2) = 0
Cells(c.Row, 3) = "Y"
End If
Next
Application.EnableEvents = True
End If
End If
End With
End If
End Sub