【问题标题】:Adding and Removing ComboBoxes Programmatically to Excel以编程方式向 Excel 添加和删除组合框
【发布时间】:2021-09-17 00:02:09
【问题描述】:

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

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

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

【问题讨论】:

  • “在此代码中,我无法为组合指定名称,这会影响我的删除/删除问题”,但您没有说明原因或错误。
  • 如果你想删除所有的组合框,this answer 会告诉你一个快速的方法。可以向组合框添加名称,但可以有点tricky
  • @sorceri 你是对的。我不能重复错误,所以我将编辑问题。谢谢。
  • @PeterT 谢谢。我只想从某些特定的单元格中删除。
  • 您尝试过什么删除组合框?如果您想按名称删除,您应该能够使用工作表的Shapes 集合。

标签: excel vba


【解决方案1】:

使用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

【讨论】:

    猜你喜欢
    • 2020-08-12
    • 2011-08-02
    • 2016-05-02
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多