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

如何在VBA中获取ComboBox控件的选中值?

VBA获取UserForm中ComboBox选中值并添加表格行的问题与解决

问题描述

我尝试了多种语法,始终无法获取ComboBox控件的选中值,后续需要用两个ComboBox的值向表格添加新行。

原测试代码

Sub add_termin()
    
    Dim lastrow As Long
    Dim typ As String
    Dim frist As Date
    Dim tops As String
    
    ThisWorkbook.Activate
    Set diesesworkbook = ActiveWorkbook
    
    lastrow = diesesworkbook.Sheets("Termine").Range("A" & Rows.Count).End(xlUp).Row
    typ = ComboBox1.Column(1).Value
    MsgBox (lastrow)

'Hinzufügen neuer Zeile
    'diesesworkbook.Sheets("Termine").Range("A" & (lastrow + 1)).Value = ComboBox1.Value
    'diesesworkbook.Sheets("Termine").Range("B" & (lastrow + 1)).Value = TestBox1.Value
    'diesesworkbook.Sheets("Termine").Range("C" & (lastrow + 1)).Value = ComboBox2.Value
End Sub

解决方案

关键在于必须明确指定ComboBox所属的UserForm名称,修正后的取值代码如下:

typ = Termineingabe.ComboBox1.Value

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 01:12:28