尝试编写 VBA 以将所有格式控件下拉值设置为 0

Trying to write a VBA to set all Format control Dropdown values to 0

四处寻觅,有没有人可以帮忙。一直在尝试创建一个将所有下拉菜单重置为 0 的宏(格式控件)

到目前为止我有这个但是我收到一条错误消息“无法设置下拉列表 class 的值 属性”

Sub callmacro()

Const NAME As String = "Drop Down"
Dim dropdown As Shape
Dim Sht As Worksheet

Set Sht = ThisWorkbook.Sheets("Pricing Tool")


On Error Resume Next
For Each dropdown In Sht.Shapes


If Left(dropdown.NAME, 9) = NAME Then


dropdown.ControlFormat.Value = 0

On Error GoTo 0

End If

Next dropdown
End Sub

有什么想法吗?

谢谢

我不会使用 .Shapes 集合。如果您的工作表中有其他类型的形状,它很容易变得混乱。

相反,假设您已经为您的 ComboBoxes 分配了一些列表(如果您没有向所有 ComboBox 添加至少一项,则会引发错误),我会使用隐藏集合 .DropDowns 像这样:

Option Explicit

Sub InitializeDropDown()
Dim sht As Worksheet
Dim dd As DropDown
Set sht = ThisWorkbook.Sheets("Pricing Tool")
For Each dd In sht.DropDowns
    If dd.ListCount > 0 Then
        dd.Value = 0
    End If
Next dd
End Sub

这会将您的组合框初始化为空白。

请注意,此代码适用于表单控件组合框,不适用于 ActiveX 组合框。

编辑:

正如 FunThomas 指出的那样,您可以轻松地检查某个项目是否已添加到组合框中。添加了相关的 If 语句。