我有一个按钮,在执行buttons命令之前执行用户表单以显示所需的密码,但是当用户表单显示时,它会冻结表单的其余部分。在用户表单上,我创建了一个取消按钮,以便用户可以退出用户窗体,如果他不知道密码并使用我在工作表上的其他按钮。当用户单击取消按钮时,即使他/她没有输入密码,它仍然会执行命令。当您输入正确的密码/错误的密码时,用户表单正常工作,只有当您单击取消时它才能正常工作工作。可以提供任何帮助吗?请参阅下面的代码,了解按钮和取消按钮的代码
Sub Feeder_Schedule()
UserForm1.Show
If Sheets("Bulk_Data").Visible = xlVeryHidden Then
Sheets("Bulk_Data").Visible = True
End If
Sheets("Bulk_Data").Select
Sheets("Home").Visible = xlVeryHidden
End Sub
取消按钮的代码
Private Sub CommandButton1_Click()
Unload Me
End Sub
答案 0 :(得分:2)
如果您想正确更改/添加到用户表单的代码
Option Explicit
' USERFORM CODE
Private m_Cancelled As Boolean
' Returns the cancelled value to the calling procedure
Public Property Get Cancelled() As Variant
Cancelled = m_Cancelled
End Property
Private Sub buttonCancel_Click()
' Hide the Userform and set cancelled to true
Hide
m_Cancelled = True
End Sub
' Handle user clicking on the X button
Private Sub UserForm_QueryClose(Cancel As Integer _
, CloseMode As Integer)
' Prevent the form being unloaded
If CloseMode = vbFormControlMenu Then Cancel = True
' Hide the Userform and set cancelled to true
Hide
m_Cancelled = True
End Sub
您的代码可能看起来像那样
Sub Feeder_Schedule()
Dim frm As UserForm1
Set frm = New UserForm1
frm.Show
If Not frm.Cancelled Then
If Sheets("Bulk_Data").Visible = xlVeryHidden Then
Sheets("Bulk_Data").Visible = True
End If
Sheets("Bulk_Data").Select
Sheets("Home").Visible = xlVeryHidden
End If
End Sub