如何在文本框中保持setfocus – vba excel

我是新的VBA,首先我有一个用户textbox1textbox1和一个commandbutton1 。 当我input或在textbox1Tab ,我有问题,我想textbox1仍然在setfocus但它单击commandbutton1

我有下面的代码:

 Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _ ByVal Shift As Integer) If KeyCode = 13 Then TextBox1.SetFocus End If End Sub 

当我按下键盘上的“F4”时什么是closuresuserform1的VBA代码? 谢谢…

改变你的代码

 Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _ ByVal Shift As Integer) If KeyCode = 13 Then TextBox1.SetFocus End If End Sub 

 Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _ ByVal Shift As Integer) '~~> 13 is for Enter, 9 is for Tab If KeyCode = 13 Or KeyCode = 9 Then KeyCode = 0 End If End Sub 

当我按下键盘上的“F4”时什么是closuresuserform1的VBA代码

看到这个 。 这个线程是用于Esc键的。 将其更改为F4

大声笑。 这是你的一个线索。

根据你需要做什么,你可能不需要任何代码; 尝试将文本框的TabKeyBehavior属性设置为True。 如果这样做,则Tab键被input为文本,而不是将焦点移动到窗体上的下一个控件。

你可以用文本控件的EnterKeyBehavior属性来做同样的事情。