以编程方式在单元格内插入ComboBox

时间:2017-06-27 05:53:17

标签: excel excel-vba 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

1 个答案:

答案 0 :(得分:3)

  

你的意思是你指定的哪个单元格,它应该重叠吗? - Siddharth Rout 1小时前

     先生,是的。 Thant就是这样。 - AratioD 3分钟前

这是你在尝试的吗?

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