Returning a value or Cancel from Excel userform

二次信任 提交于 2019-12-25 16:25:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!