首页 > 解决方案 > 根据 Active-x Dropdown 自动填充一行中的单元格

问题描述

我有一个 Active-x 下拉菜单,并希望根据此下拉菜单自动填充行中的其他单元格。我在工作表更改事件中编写了代码,但是当我从此下拉列表中选择时,它不会触发其他行的自动填充代码。任何帮助,将不胜感激。 第 9 列是我的 active-x 下拉列表,但是当我从列表中选择时,在我的 Resource(sheet) 中显示下一个单元格的代码不会触发。

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim wsSource As Worksheet
    Dim r As Long
    Set wsSource = ThisWorkbook.Sheets("Source")     'Source sheet
    Application.EnableEvents = False
    If Target.Column = 9 Then
       r = Application.Match(Target.Value, wsSource.Columns(8), 0)
       Target.Offset(0, 1) = wsSource.Cells(r, 9)
    End If
    Application.EnableEvents = True
    End Sub

标签: excelvba

解决方案


更改 a 中的值Active X ComboBox不会触发 a Worksheet_Change。相反,使用ComboBox_Change这样的事件:

Private Sub ComboBox1_Change()
    MsgBox "Please share your code next time you post. It will greatly help others help you :)"
End Sub

您可能需要在运行代码之前验证所选值,这可以通过简单的If ComboBox1.Value = "?" Then


推荐阅读