1

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
Community
  • 1
  • 1
  • 2
    where do you place your `DeleteLine` sub? It should be in _standart_ module rather than in UserForm module – Dmitry Pavliv Apr 14 '14 at 15:53
  • With the exception of Unload Me, it worked for me without changes. – smoore4 Apr 14 '14 at 16:12
  • I solve the problem. I think the problem is that I put the function DeleteLine in the Userform script. So when I click the delete button on the sheet, it cannot find the function. To solve this, I put the function in the script of sheet 1, and for onaction parameter of the button, I use onaction = "sheet1.DeleteLine". Then it works. It's not very beautiful, but at least it works... – Little Tree Apr 15 '14 at 07:18

0 Answers0