跳到主要内容

如何在Excel中的UserForm上基于组合框选择填充文本框?

假设您有一个包含“名称”和“职员编号”列的表,如下图所示。 现在,您需要创建一个嵌入有一个组合框的用户窗体,该组合框收集内部的所有名称,并且在从组合框选择名称时将填充一个文本框,该文本框将填充相应的职员编号。 本文中的方法可以帮助您轻松实现目标。

使用VBA根据用户窗体上的组合框选择填充文本框


使用VBA根据用户窗体上的组合框选择填充文本框

请执行以下操作,以根据用户窗体上的组合框选择为文本框填充相应的值。

1。 按 其他 + F11 键打开 Microsoft Visual Basic应用程序 窗口。

2.在 Microsoft Visual Basic应用程序 窗口中,单击 插页 > 用户表格。 看截图:

3.然后将组合框控件和文本框控件插入到创建的用户窗体中,如下图所示。

4.右键单击用户窗体的任何空白,然后单击 查看代码 从上下文菜单中。 看截图:

5.在打开的用户窗体(代码)窗口中,请复制并粘贴以下VBA代码以替换原始代码。

VBA代码:根据用户窗体上的组合框选择填充文本框

Dim xRg As Range
'Updated by Extendoffice 2018/1/30
Private Sub UserForm_Initialize()
    Set xRg = Worksheets("Sheet5").Range("A2:B8")
    Me.ComboBox1.List = xRg.Columns(1).Value
End Sub
Private Sub ComboBox1_Change()
    Me.TextBox1.Text = Application.WorksheetFunction.VLookup(Me.ComboBox1.Value, xRg, 2, False)
End Sub

备注:在代码中,A2:B2是范围,其中包含您将分别在“用户窗体”组合框和文本框中填充的值。 并且Sheet5是包含数据范围的工作表的名称。 请根据需要更改它们。

6。 按 其他 + Q 退出键 Microsoft Visual Basic应用程序 窗口。

7。 点击 开发商 > 插页 > 命令按钮(ActiveX控件) 在工作表中插入命令按钮。

8.右键单击工作表选项卡,然后选择 查看代码 从右键单击菜单中。 然后将VBA代码复制并粘贴到代码窗口中。

VBA代码:显示用户表单

Private Sub CommandButton1_Click()
'Updated by Extendoffice 2018/1/30
    UserForm1.Show
End Sub

9.在工作簿中关闭“设计模式”。

单击命令按钮以打开指定的用户窗体。 然后,您可以看到所有名称都收集在组合框中。 从组合框中选择一个名称时,相应的人员编号将自动填充到文本框中,如下面的屏幕截图所示。


相关文章:

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (11)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
can the same be don on excel sheet? instead of userform? i am using sheet not userform
This comment was minimized by the moderator on the site
alguem poderia me ajudar estou tentando carregar a combobox e puxar informações para text box so que esta dando erro 381

Private Sub ComboNome_Emissor_Change()
TextArea.Text = ComboNome_Emissor.List(ComboNome_Emissor.ListIndex, 1)
TextTreinamento_PT.Text = ComboNome_Emissor.List(ComboNome_Emissor.ListIndex, 3)
TextTreinamento_EC.Text = ComboNome_Emissor.List(ComboNome_Emissor.ListIndex, 5)
TexStatus_Bloqueio.Text = ComboNome_Emissor.List(ComboNome_Emissor.ListIndex, 6)


End Sub
This comment was minimized by the moderator on the site
The method is very simple and populates the combo box quite efficiently. However, when I try populating the text box, I get a run-time error '1004': Unable to get the VLookup property of the WorksheetFunction class. Kindly assist with resolving this error.
This comment was minimized by the moderator on the site
I just tried this code and was running into the same issue. I realized I had the xRg variable inside the UserForm block, so it was not being accessed by my ComboBox function. Maybe others are running into the same issue
This comment was minimized by the moderator on the site
Hi,
May I have your Office version? I need the feedback to check for the error.
Thanks for your comment.
This comment was minimized by the moderator on the site
office 2010 32 bit
This comment was minimized by the moderator on the site
Good day,
I have tried the code in Office 2010 32 bit, but no errors occured.
Please make sure the sheet name and cell range in the first VBA code are the same with your actual sheet name and range.
Thanks for your comment.
This comment was minimized by the moderator on the site
Hi,
i have same issue with office 2013. Any solution please
This comment was minimized by the moderator on the site
i have the same problem....
This comment was minimized by the moderator on the site
I too have the same problem
This comment was minimized by the moderator on the site
Hi Chaylon,
Have you replaced the sheet name in the below code line with your actual used worksheet name?
Set xRg = Worksheets("Sheet5").Range("A2:B8")
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations