【问题标题】:Automatically update cells with dependent dropdown values自动更新具有相关下拉值的单元格
【发布时间】:2021-11-23 11:13:05
【问题描述】:

我有一张表,其中有两列(C 列和 E 列)。 E 列的单元格的下拉菜单取决于同一行 C 列中单元格的值。

我试图让 E 列中的值在 C 列中的值发生更改时自动更改为新的相应下拉菜单的第一个选项。就目前而言,当 C 列中的值发生变化时,相应 E 列单元格中之前的值仍然存在,我必须手动单击并从新列表中选择。

这是我必须开始的:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng(1) As Range, rng1 As Range
Set rng(0) = Range("C71:C91")
Set rng(1) = Range("E71:E91")
Application.EnableEvents = False
If Not Intersect(Target, rng(0)) Is Nothing Then
   For Each rng1 In rng(1)
     i = i + 1
     rng1 = Range("" & rng(0).Value2)(i, 1)
   Next
End If
Application.EnableEvents = True
End Sub

【问题讨论】:

  • Col E 中的列表是如何分配的,当 Col C 中的值发生变化时,它们如何变化?

标签: excel vba dropdown


【解决方案1】:

带有数据验证的工作表更改(下拉菜单)

  • 假定E71:E91 中的下拉菜单“获取”来自C71:C91 的值,并且当您更改(手动或通过VBA)C71:C91 中的值时,@ 同一行中的值987654324@ 将被此值覆盖。
  • 完成测试后,将Debug.Print 行注释掉或删除。
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Debug.Print "Worksheet Change Sequence at " & Now
    Debug.Print "1. '" & Target.Address(0, 0) & "' has changed."

    Dim srg As Range: Set srg = Range("C71:C91")
    Dim drg As Range: Set drg = Range("E71:E91")
    Dim irg As Range: Set irg = Intersect(srg, Target)
    Debug.Print "2. Range references created."
    
    If irg Is Nothing Then
        Debug.Print "3. No intersecting range. Exiting."
        Exit Sub
    Else
        Debug.Print "3. Intersecting range at '" & irg.Address(0, 0) & "'."
    End If
    
    On Error GoTo ClearError
    Application.EnableEvents = False
    Debug.Print "4. Error handler activated. Events disabled."
     
    ' Write to intersecting rows only.
    Dim dCol As Long: dCol = drg.Column
    Dim iCell As Range
    For Each iCell In irg.Cells
        iCell.EntireRow.Columns(dCol).Value = iCell.Value
    Next iCell
    Debug.Print "5. Written to '" _
        & Intersect(irg.EntireRow, drg).Address(0, 0) & "'."

    'Or:
    ' Write to whole destination range.
    'drg.Value = srg.Value
    'Debug.Print "5. Written to '" & drg.Address(0, 0) & "'."

SafeExit:
    Application.EnableEvents = True
    Debug.Print "6. Events enabled. Exiting."

    Exit Sub
ClearError:
    Debug.Print "Run-time error '" & Err.Number & "': " & Err.Description
    Resume SafeExit
End Sub

' Multi-range example. Best run from 'VBE' with the Immediate window open.
Sub Test()
    Range("C71,C73,C75").Value = "A"
    Range("C73,C75").Value = "B"
    Range("C75").Value = "C"
End Sub

【讨论】:

    【解决方案2】:

    在 if 语句后使用 Application.CalculateFull 重新计算工作表。

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案3】:

    尚未完全测试代码,但可以看到一个基本错误。线路:

    For Each rng1 In rng(1)
    

    应阅读:

    For Each rng1 In rng(1).Cells
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多