Excel VBA OnAction事件没有';不起作用,但不';也没有错

Excel VBA OnAction事件没有';不起作用,但不';也没有错,vba,excel-2010,Vba,Excel 2010,我有以下动态创建按钮的代码: Private Sub Worksheet_Change(ByVal Target As Range) Dim i, sInfo As String For i = 2 To GetLastRow("Deliverables", "A") sInfo = "CmdButton" & i Me.Buttons(sInfo).Delete ActiveSheet.Buttons.Add(Cells(i, "AA").L

我有以下动态创建按钮的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i, sInfo As String


    For i = 2 To GetLastRow("Deliverables", "A")

    sInfo = "CmdButton" & i

    Me.Buttons(sInfo).Delete


    ActiveSheet.Buttons.Add(Cells(i, "AA").Left + Cells(i, "AA").Width * 0.05, Cells(i, "AA").Top + Cells(i, "AA").Height * 0.05, Cells(i, "AA").Width * 0.9, Cells(i, "AA").Height * 0.9).Select
    With Selection
        .Caption = "Update Task: " & Cells(i, "B").Value
        .Name = sInfo
        .Text = "Update Task: " & Cells(i, "B").Value
        Selection.OnAction = "CmdButton2_Click"

    End With
    Next
End Sub
这运行时没有错误,但我似乎无法正常工作的是Selection.OnAction事件。当我点击按钮时,什么也没发生。我正试图让OnAction事件调用VBA代码中的另一个子对象。我在这里和网络上的其他地方尝试了一些例子,但似乎无法让它们发挥作用


有人知道我遗漏了什么吗?

我创建了一个按钮,并让它调用宏例程,代码如下:

Sub CreateButton()
Dim I, sInfo As String
Dim sBut
    I = 2        
    sInfo = "CmdButton" & I

    ActiveSheet.Buttons(sInfo).Delete

    Set sBut = ActiveSheet.Shapes.AddFormControl(xlButtonControl, _
        Cells(I, "AA").Left + Cells(I, "AA").Width * 0.05, Cells(I, "AA").Top + Cells(I, "AA").Height * 0.05, Cells(I, "AA").Width * 0.9, Cells(I, "AA").Height * 0.9)
    With sBut
        .Name = sInfo
        .OnAction = "MyMacro"
        .TextFrame.Characters.Text = "Update Task: " & Cells(I, "B").Value
    End With
End Sub

Sub MyMacro()
MsgBox "You Clicked Me"
End Sub
为我工作

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i, sInfo As String, c As Range
Dim rngUpdate As Range, rw As Range, r As Long
Dim tsk As String

    Set rngUpdate = Application.Intersect(Target, Me.Range("C2:G20"))

    If rngUpdate Is Nothing Then Exit Sub

    Set rngUpdate = rngUpdate.EntireRow

    For Each rw In rngUpdate.Rows

        r = rw(1).Row
        sInfo = "CmdButton" & r

        On Error Resume Next
        Me.Buttons(sInfo).Delete
        On Error GoTo 0

        Set c = ActiveSheet.Cells(r, "B")
        With ActiveSheet.Buttons.Add(c.Left + c.Width * 0.05, _
                                c.Top + c.Height * 0.05, _
                                c.Width * 0.9, c.Height * 0.9)

            .Caption = "Update Task: " & Cells(r, "C").Value
            .Name = sInfo
            tsk = Cells(r, "C").Value
            .Text = "Update Task: " & tsk
            .OnAction = "'Sheet1.CmdButton2_Click """ & tsk & """'"

        End With
    Next rw

End Sub

Sub CmdButton2_Click(r)
    Debug.Print "clicked update for : " & r
End Sub

CmdButton2\u单击
子按钮在哪里?如果不在常规模块中,则需要在其前面加上对象模块名称-例如
Sheet1.CmdButton2\u单击
CmdButton2\u单击
中有什么?确保它不是空的。您的代码对我来说很好。@TimWilliams:如果onaction找不到
CmdButton2\u单击
,它将给出一个错误。就像Op提到的,他没有收到任何错误…@SiddharthRout-我读到这可能意味着“除了Selection.OnAction之外,这运行时没有错误”这段代码对我来说很好。正如Sid所提到的,确保
CmdButton2\u Click
包含可执行代码。我需要做的是将我的工作表名称添加到OnAction事件中。我尝试了“Sheet!CmdClick”,但不起作用,但“Sheet.CmdClick”起作用。