问题
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:
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
来源:https://stackoverflow.com/questions/32190506/returning-a-value-or-cancel-from-excel-userform