Excel 和在两个下拉列表之间更改值

标签 excel excel-formula excel-2010 vba

我有两个下拉列表 - 一个依赖于另一个 - 这意味着如果我从 A1 中的列表中选择特定值,则 A2 中会出现一个特定列表。这很好用。但是,当我更改 A1 中的值时,A2 会保留在单元格中,直到我单击列表 - 然后 A2 中的值将根据我的选择而更改。

例如,如果列表 1 为 ['Yes','No'],列表 to 为 Yes: [1,2,3] No: [4,5,6]。首先,我为 A1 选择"is",然后为 A2 选择 2。然后,如果我为 A1 选择“否”,则“2”将保留在 A2 中,直到我实际单击 A2 以选择新值 (4,5,6)。一旦我更改了 A1 选择,有什么方法可以“清除”A2 吗?

谢谢!

最佳答案

将其放入工作表的 VBA 代码中:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Address = Range("A1").Address Then
        Dim dependentCell As Range
        Set dependentCell = Target.Offset(1, 0)     'Cell A2
        If dependentCell.Validation.Value = False Then dependentCell.Clear
    End If        
End Sub

关于Excel 和在两个下拉列表之间更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18383989/

相关文章:

Excel用#na查找行

excel - 如果范围包含值 > 8 的单元格,则 MsgBox

excel - 复制并粘贴到下一个以 B59 开头的空行

excel - 如何用VBA语言表达 "if value is not empty"?

vba - 搜索完全相同的日期或过去最接近的日期的函数 + 一个额外条件

excel - 将计算的日期值复制为真实日期

Excel 对 Index - match 和 sumifs 组合的帮助?

Excel 2010 - 在工作簿中存储功能区自定义

python - 使用 Python Pandas 的 Excel 'COUNTIF() ' 功能

Excel DATEVALUE 函数不适用于今天的日期