Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用转义按钮关闭用户窗体_Vba_Excel - Fatal编程技术网

Vba 使用转义按钮关闭用户窗体

Vba 使用转义按钮关闭用户窗体,vba,excel,Vba,Excel,我有两个问题 当我按下esc按钮,然后关闭Userform1 当我在TextBox1中输入open时,应显示Userform2。还可以自动清除Userform1中的TextBox1 我尝试了以下代码: Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If textbox1.value = "open" then userform2.show textbox1.va

我有两个问题

  • 当我按下esc按钮,然后关闭
    Userform1

  • 当我在
    TextBox1
    中输入
    open
    时,应显示
    Userform2
    。还可以自动清除
    Userform1
    中的
    TextBox1

  • 我尝试了以下代码:

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        If textbox1.value = "open" then
            userform2.show
            textbox1.value =""
        End If
    End Sub
    
    使用Esc关闭userform1

    如果您在userform上没有任何控件,那么只需使用以下代码

    Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        If KeyAscii = 27 Then Unload Me
    End Sub
    
    如果你有一个文本框和一个命令按钮,那么使用这个

    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
    
    如果您有任何其他可以对焦的控件,则必须使用该控件的
    按键
    事件,就像我对
    文本框

    当我向textbox1输入“open”时,userform2也会自动在userform1中显示clear textbox1

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

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

    Private Sub cmdClose_Click()
    
        Unload Me
    
    End Sub
    
  • 5.将按钮的高度和宽度设置为0


    就是这样

    如果您有一个关闭表单的按钮,只需将(Cancel)属性设置为True,就可以启动(Esc)上的取消按钮。。
    欢呼。< /P>请确保,新按钮是唯一一个属性取消设置为true的按钮。还考虑将此按钮的“代码> TabSture< /Cult>属性设置为<代码> false <代码>以从键盘的选项卡周期中删除按钮。我不知道为什么这个方法应该起作用,但它确实如此。我在主要答案中添加了注释。有没有办法在不为每个可以聚焦的按钮创建按键子的情况下实现这一点?我有一个userform,上面有很多控件,我希望能够在任何时候使用Esc卸载表单。很抱歉,我错过了这个评论。。。是,创建一个控制数组。我已经在我的屏幕上和stackoverflow上覆盖了它。由于某种原因,当我按下
    Esc
    键时,什么也没有发生。有什么建议吗?!userform中有哪些控件@Karganesanyikes,我错过了。现在它工作了!谢谢你的回复!