Returning a value or Cancel from Excel userform

2019-09-16 16:05发布

I have a sub that calls a userform to show and would only like to proceed if the user didn't click my Cancel button. I don't want to put all my other sub calls within the userform.

Is it possible to have a userform return a value or a way to check if the user clicked a particular button?

I suppose I can use a global variable, but was wondering if I could pass things to and from a userform.

1条回答
Lonely孤独者°
2楼-- · 2019-09-16 16:41

I prefer to use properties.

Inside your userForm

Private m_bCancel As Boolean

Public Property Get Cancel() As Boolean
    Cancel = m_bCancel
End Property

Public Property Let Cancel(ByVal bCancel As Boolean)
    m_bCancel = bCancel
End Property

Code for the cancel button

Private Sub cmdCancel_Click()
    Me.Cancel=True
    Me.Hide
End Sub

Call the userForm from outside like this

sub loadForm()

dim frm 

set frm= new UserForm1
frm.show

if frm.Cancel then
   Msgbox "Cancelled"
end if

End Sub
查看更多
登录 后发表回答