根据同一行中下拉验证列表的选择清除 Excel 表格中的单元格,同时根据该选择更新类似的项目

问题描述 投票:0回答:1

我正在 Excel 中构建一个复杂的工作簿,并尝试使其尽可能用户友好。

示例:
我有一个名为 Tbltst 的表,其中包含三列 Control #、Dollars 和 Returned。
控件编号是自动填充的,美元是用户输入的,返回的是验证下拉列表。

Workbook

当我将 Returned 中的值从 N 更改为 Y 时,我希望该 Control# 的 Dollars 列中的所有金额都更改为 0,并且 Returned 的其他条目更改为 Match。

假设我更改了列表中的第一个 1234。 50 美元应该被清空,第 3 行和第 6 行也应该被清空,并且它们返回的列更新为 Y 以匹配第 1 行。

工作簿已锁定,Control# 不可编辑(如上所述由公式驱动),但 Dollars 和 Returned 均可编辑。

我确实尝试使用公式,但要根据上面的内容设置单元格需要宏。

excel vba excel-tables
1个回答
0
投票

用户在

Returned
列中的输入会触发
Worksheet_Change
更新数据。

工作表.更改事件(Excel)

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

© www.soinside.com 2019 - 2024. All rights reserved.