Excel VBA Cannot call function with onaction

2019-07-28 02:48发布

问题:

I write a script for a ok button in a userform to create a delete button on the sheet to delete the whole line. The problem is that when I click the delete button, it cannot call the function I assigned with onaction parameter.

Private Sub OKButton_Click()
    Dim emptyRow As Long
    'Make Feuil1 Active
    Feuil1.Activate

    'Determine emptyRow
    emptyRow = WorksheetFunction.CountA(Range("C:C")) + 1

    Dim deleteButton As Button
    Dim t As Range

    Set t = ActiveSheet.Range(Cells(emptyRow, 2), Cells(emptyRow, 2))
    Set deleteButton = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
    With deleteButton
        .OnAction = "DeleteLine"
        .Caption = "Delete" & emptyRow
        .Name = "DeleteButton" & emptyRow
    End With

    'Close user form
    Unload Me

End Sub

Sub DeleteLine()
    MsgBox "You Clicked Me"
End Sub