1

我有一张表,我想让用户选择计算类型。计算类型是通过数据验证中的列表选择来完成的。选择后,我希望它触发一个事件,然后为该类型的选择加载正确的单元格。如何在数据验证下拉菜单中检测数据更改事件,或者我是否需要为此使用活动 x 控件?

工作表更改事件的代码未激活:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.count > 1 Then Exit Sub

Application.EnableEvents = False

On Error GoTo Errortrap


'~~> Change it to the relevant string with which you want to compare
StringToCheck = "+"

If Not Intersect(Target, Range("D47")) Is Nothing Then
    '~~> Check for the cell value
    If Target.Value = StringToCheck Then
      'setup row to capture addition fields
       Cells(33, 4).Value = "Input File 1"
       Cells(33, 4).Value = "Worksheet 1"
       Cells(33, 4).Value = "Cell 1"
       Cells(33, 4).Value = "Input File 2"
       Cells(33, 4).Value = "Worksheet 2"
       Cells(33, 4).Value = "Cell 2"
    End If
End If

LetsContinue:
   Application.EnableEvents = True
   Exit Sub
Errortrap:
    MsgBox Err.Description
    Resume LetsContinue
End Sub
4

1 回答 1

1

你的代码很好。我在一个新的工作簿中尝试了它,它完成了它应该做的事情。
当您将D47中的值更改为“+”(无论是通过下拉菜单还是手动)时,它会在单元格D33中一个接一个地写入六个值。

也许你打算写

        Cells(33, 4).Value = "Input File 1"
        Cells(33, 5).Value = "Worksheet 1"
        Cells(33, 6).Value = "Cell 1"
        Cells(33, 7).Value = "Input File 2"
        Cells(33, 8).Value = "Worksheet 2"
        Cells(33, 9).Value = "Cell 2"

因此代码将填充范围D33:I33而不是将所有内容写入D33

于 2015-11-20T12:03:54.600 回答