首页 > 解决方案 > 以编程方式向 Excel 添加和删除组合框

问题描述

虽然我没有困难以编程方式添加,但我无法删除组合框。在这段代码中,我尝试分配名称,但是当我尝试删除时,我无法以任何方式使用该名称。

当我尝试删除时,我也尝试使用手机号码,但它没有用。我很感激这个问题的帮助,这让我现在待了好几个小时:(

Set curCombo = Worksheets("Nodes").Shapes.AddFormControl(xlDropDown, Left:=Cells(i, 1).Left, Top:=Cells(i, 1).Top, Width:=100, Height:=Rows(2).RowHeight)

method_name = "Set_Node_Name"


With curCombo
        .ControlFormat.DropDownLines = 44
        .ControlFormat.ListFillRange = "Node_Type_Names!$A$1:$A$44"
        .ControlFormat.LinkedCell = "B" & i
        .OnAction = method_name
        '.Name = "myCombo" & i

End With

标签: excelvba

解决方案


使用Intersect您可以确定组合框是否在您要删除的范围内。

我在这里使用 B 列。

Sub removeCombos()
    Dim combo As DropDown
    Dim irng As Range
    With ActiveSheet
        For Each combo In .DropDowns
            Set irng = Application.Intersect(combo.TopLeftCell, .Range("B:B"))
            If Not irng Is Nothing Then
                combo.Delete
            End If
        Next
    End With
End Sub

推荐阅读