You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过ComboBox选择名称,将Excel对应行数据填充至UserForm文本框

Excel UserForm 联动填充文本框实现方案

一、初始化ComboBox加载A列名称

在UserForm的Initialize事件中添加代码,自动将工作表A列的非空名称导入ComboBox:

Private Sub UserForm_Initialize()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1") '替换为你的实际工作表名称
    
    '加载A列从第2行开始的非空数据到ComboBox
    ComboBox1.List = ws.Range("A2:A" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row).Value
End Sub

二、选择名称后自动填充文本框

在ComboBox的Change事件中添加代码,找到选中名称的位置,然后将对应下方单元格的内容填充到文本框:

Private Sub ComboBox1_Change()
    Dim ws As Worksheet
    Dim targetCell As Range
    Dim selectedName As String
    
    Set ws = ThisWorkbook.Worksheets("Sheet1") '替换为你的实际工作表名称
    selectedName = ComboBox1.Value
    
    If selectedName <> "" Then
        '在工作表中精确查找选中的名称
        Set targetCell = ws.Cells.Find(What:=selectedName, LookIn:=xlValues, LookAt:=xlWhole)
        
        If Not targetCell Is Nothing Then
            'TextBox1填充目标单元格下一行的内容
            TextBox1.Value = ws.Cells(targetCell.Row + 1, targetCell.Column).Value
            'TextBox2填充目标单元格下两行的内容
            TextBox2.Value = ws.Cells(targetCell.Row + 2, targetCell.Column).Value
            '如需更多文本框,按此格式继续添加
            'TextBox3.Value = ws.Cells(targetCell.Row + 3, targetCell.Column).Value
        Else
            '未找到匹配名称时清空文本框
            TextBox1.Value = ""
            TextBox2.Value = ""
        End If
    Else
        '未选择任何名称时清空文本框
        TextBox1.Value = ""
        TextBox2.Value = ""
    End If
End Sub

注意事项

  • 务必将代码中的"Sheet1"替换成你实际使用的工作表名称。
  • 如果名称固定在A列,可以把查找范围限定为A列,提升效率:Set targetCell = ws.Range("A:A").Find(...)。
  • 若需要填充的是同一行的其他列数据(比如名称在A4,TextBox1取B4),只需修改列参数,例如ws.Cells(targetCell.Row, "B").Value。
  • 保证ComboBox加载的名称与工作表中的名称完全一致(无空格、大小写匹配),避免查找失败。

内容的提问来源于stack exchange,提问作者Fgomes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 03:01:05