vba 使用退出按钮关闭用户表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20240677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:18:29  来源:igfitidea点击:

Close userform with escape button

excelvbaexcel-vba

提问by Tomz

I have 2 questions.

我有2个问题。

  1. When I pressed escbutton then close Userform1

  2. When I input openin TextBox1then Userform2should show. Also clear TextBox1in Userform1automatically.

  1. 当我按下esc按钮然后关闭Userform1

  2. 当我输入openTextBox1Userform2应该显示。还清楚TextBox1Userform1自动。

I have tried the below code:

我试过下面的代码:

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

回答by Jesus

  1. Insert a new Command Button
  2. Switch its Cancel property to True
  3. You May Name it as cmdClose
  4. Add next code:

    Private Sub cmdClose_Click()
    
        Unload Me
    
    End Sub
    
  1. 插入一个新的命令按钮
  2. 将其 Cancel 属性切换为 True
  3. 您可以将其命名为 cmdClose
  4. 添加下一个代码:

    Private Sub cmdClose_Click()
    
        Unload Me
    
    End Sub
    

5.Set height and widht of the button to 0

5.设置按钮的高度和宽度为0

that's it

就是这样

回答by Siddharth Rout

Close userform1 with Esc

关闭 userform1 Esc

If you don't have any controls on userform then simply use this code

如果您在用户表单上没有任何控件,则只需使用此代码

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

If you have say a TextBox and a Command Button then use this

如果你说一个 TextBox 和一个命令按钮然后使用这个

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

If you have any other control that can take focus then you will have to use the KeyPressevent of that control like I did for TextBox

如果您有任何其他可以获取焦点的控件,那么您将必须KeyPress像我一样使用该控件的事件TextBox

when I input "open" to textbox1 then userform2 showed also clear textbox1 in userform1 automatically.

当我向 textbox1 输入“打开”时,userform2 也会自动在 userform1 中显示清除 textbox1。

KeyPresswill capture only one key. Use the Changeevent to compare what is there in the textbox.

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

Private Sub TextBox1_Change()
    If LCase(TextBox1.Value) = "open" Then
        TextBox1.Value = ""
        UserForm2.Show
    End If
End Sub

回答by Mahmoud Sayed

if you have a button the closes the form, just set the (Cancel) propertyto Trueand that will fire the cancel button on (Esc).. Cheers.

如果您有一个关闭表单的按钮,只需将 (Cancel)属性设置True,这将触发 (Esc) 上的取消按钮。干杯。

回答by Gautam Dixit

Very Correct... If You Have Close Button On User Form And You Have Written Code for that ( Unload Me ) then Set Cancel Property To True ( By Default It Is False)

非常正确......如果您在用户表单上有关闭按钮并且您已经为此编写了代码(卸载我)然后将取消属性设置为真(默认情况下它是假的)