当用户窗体激活时,VBA 使用户窗体中的文本框处于活动状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22104710/
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-12 02:09:41  来源:igfitidea点击:

VBA Make a text box in a UserForm Active when the UserForm activates?

vbaexcel-vbaexcel-2010excel

提问by ilarson007

How can I make a text box in a UserForm activate when the UserForm activates, so that the user can start typing without having to click in the text box?

如何在用户窗体激活时激活用户窗体中的文本框,以便用户无需单击文本框即可开始键入?

回答by user2140261

What you are looking for is the TabIndex Property.

您正在寻找的是 TabIndex 属性。

Every object on your userform has one, it is the order to which object on the userform are selected when you press the tab button. The object with 0 TabIndexwill be the active object when a form is loaded also:

用户表单上的每个对象都有一个,它是按选项卡按钮时选择用户表单上的对象的顺序。TabIndex加载表单时,带有 0 的对象也将是活动对象:

So with the textboxselected go to the properties pane and look for Tabindexset this to 0 and your textboxwill be selected on open.

因此,textbox选中后转到属性窗格并查找将其Tabindex设置为 0,您textbox将在打开时被选中。

enter image description here

在此处输入图片说明

You can also set other textboxesindex to 1,2,3 and on, so that if the form is being filled out you can simply press tab to go from one text box to another.

您还可以将其他textboxes索引设置为 1、2、3 等,这样如果正在填写表单,您只需按 Tab 键即可从一个文本框转到另一个文本框。

回答by Jerome Montino

Use .SetFocus. If your textbox's name is TextBox1, the following works:

使用.SetFocus. 如果您的文本框的名称是TextBox1,则以下内容有效:

Private Sub UserForm_Initialize()
    TextBox1.SetFocus
End Sub

Let us know if this helps.

如果这有帮助,请告诉我们。