使用退出buttonclosures用户窗体

我有2个问题。

  1. 当我按下esc键然后closuresUserform1

  2. 当我input在TextBox1 open ,那么Userform2应该显示。 同时自动清除TextBox1中的TextBox1

我已经尝试了下面的代码:

 Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If textbox1.value = "open" then userform2.show textbox1.value ="" End If End Sub 

Escclosuresuserform1

如果你没有任何对用户表单的控制,那么就使用这个代码

 Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii = 27 Then Unload Me End Sub 

如果你有一个文本框和一个命令button,然后使用这个

 Private Sub UserForm_Initialize() CommandButton1.Cancel = True End Sub Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii = 27 Then Unload Me End Sub Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii = 27 Then Unload Me End Sub Private Sub CommandButton1_Click() Unload Me End Sub 

如果你有任何其他的控件可以把焦点,那么你将不得不使用该控件的KeyPress事件,就像我为TextBox所做的那样

当我input“打开”到textbox1然后userform2自动显示在userform1清除textbox1。

KeyPress将只捕获一个密钥。 使用“ Change事件来比较文本框中的内容。

 Private Sub TextBox1_Change() If LCase(TextBox1.Value) = "open" Then TextBox1.Value = "" UserForm2.Show End If End Sub 
  1. 插入一个新的命令button
  2. 将其取消属性切换为True
  3. 您可以将其命名为cmdClose
  4. 添加下一个代码:

     Private Sub cmdClose_Click() Unload Me End Sub 

5.将button的高度和宽度设置为0

而已

如果你有一个buttonclosures表单,只需将(取消) 属性设置True ,这将触发(Esc)上的取消button..干杯。