excel - 以编程方式将 ComboBox 插入单元格内

标签 excel combobox vba

我想在事件单元格内插入对象 curCombo 组合框,而不定义宽度和高度。在我的一些测试中,我遵循了这种原则,但我也没有做到这一点

Set curCombo = ActiveSheet.Shapes.AddFormControl(.Left, .Top, .Width, .Height)

这是我的代码:

Sub comboBox1()
    Dim curCombo As Object

    '// Main challange is this set procedure here.
    Set curCombo = ActiveSheet.Shapes.AddFormControl(xlDropDown, _
    Left:=Cells(ActiveCell.Row, 3).Left, _
    Top:=Cells(ActiveCell.Row, 3).Top, Width:=100, Height:=20)

    With curCombo
        .ControlFormat.DropDownLines = 3
        .ControlFormat.AddItem "1", 1
        .ControlFormat.AddItem "2", 2
        .ControlFormat.AddItem "3", 3
        .Name = "myCombo" & ER.Row
        '.OnAction = "myCombo_Change" & ER.row
    End With
End Sub

最佳答案

do you mean which ever cell you specify, it is should overlap that? – Siddharth Rout 1 hour ago

Yes Sir. Thant is the case. – AratioD 3 mins ago

这就是你正在尝试的吗?

Sub comboBox1()
    Dim curCombo As Object
    Dim ws As Worksheet
    Dim rng As Range

    '~~> Change this to the relevant sheet
    Set ws = ActiveSheet

    With ws
        '~~> Change this to the relevant cell where
        '~~> you want the combobox
        Set rng = .Range("B5")

        Set curCombo = .Shapes.AddFormControl(xlDropDown, _
                                              Left:=rng.Left, _
                                              Top:=rng.Top, _
                                              Width:=rng.Width, _
                                              Height:=rng.Height)

        With curCombo
            .ControlFormat.DropDownLines = 3
            .ControlFormat.AddItem "1", 1
            .ControlFormat.AddItem "2", 2
            .ControlFormat.AddItem "3", 3
            .Name = "myCombo" & ER.Row
            '.OnAction = "myCombo_Change" & ER.row
        End With
    End With
End Sub

屏幕截图

enter image description here

关于excel - 以编程方式将 ComboBox 插入单元格内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44773069/

相关文章:

java - 从组合框获取值(value)到jsp中的servlet

c# - 将项目动态添加到 WPF 组合框,然后在某些事件上重置值

sql - 在 MS Access 查询中使用多值字段

excel - 我的 Excel 2007 宏 - 参数不可选是什么意思?

excel - VBA 中 "Windows"键的 Application.Sendkeys 是什么?

vba - _Change 移动数据透视图的事件过程

vba - 以编程方式将格式应用于 Excel 文件

xml - 使用 spreadsheetml 包装文本的样式

Python 3.x tkinter 组合框同步 ['variables' ]

PHP 文件 - 导出 xls 而不是 csv