b747fp
b747fp

Reputation: 175

How to implement a Cancel button on a userform?

I have a UserForm with a cancel button.

Sub DialogTest()
    MyForm.Show
End Sub

Private Sub CancelButton_Click()
    Unload Me
    End
End Sub

I also tried MyForm.Hide, End by itself, cmdExit_Click.

The cancel button does not close the dialog nor does it cause the debugger to come up.

Upvotes: 0

Views: 7311

Answers (1)

urdearboy
urdearboy

Reputation: 14580

I was only able to replicate your issue when the Unload Me Sub was pasted in a Worksheet or Module. When the Sub is in the Userform, it works fine.

Here, the code is pasted in a module and does not close the Userform

Instead, from VBE, double click on your UserForm, then double click on your Cancel Button.

Then paste the code here

enter image description here

Upvotes: 3

Related Questions