VBA 中的 Excel 下拉列表:“无法获取 Worksheet 类的 DropDowns 属性”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4633829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel dropdowns in VBA: "Unable to get the DropDowns property of the Worksheet class"
提问by sandos
I have this code:
我有这个代码:
Sub addDropdown(Name)
ActiveSheet.DropDowns.Add(74.25, 60, 188.25, 87.75).Select
Set n = ActiveSheet.DropDowns(Name)
If Not (n Is Nothing) Then
ActiveSheet.DropDowns(Name).Delete
End If
With Selection
.ListFillRange = "$K:$M"
.LinkedCell = "$K:$L"
.DropDownLines = 6
.Display3DShading = False
.Name = Name
End With
ActiveSheet.DropDowns(Name).Display3DShading = True
End Sub
Which results in "Runtime error 1004: Unable to get the DropDowns property of the Worksheet class"
这导致“运行时错误 1004:无法获取 Worksheet 类的 DropDowns 属性”
I am a VBA noob, so why is it refering to a property? According to the Object Browser DropDowns is a function (although that doesnt rime with the .Add later on).
我是 VBA 菜鸟,为什么它指的是属性?根据对象浏览器 DropDowns 是一个函数(尽管稍后与 .Add 无关)。
Also, I can access this exact thing later on after having added something to DropDowns. I just dont get it.
此外,我可以在向 DropDowns 添加一些东西后访问这个确切的东西。我只是不明白。
What I want to do, is to delete any pre-existing dropdown with the same name.
我想要做的是删除任何具有相同名称的预先存在的下拉列表。
回答by Charles Williams
You need to handle the error if the named Dropdown does not exist
如果指定的 Dropdown 不存在,您需要处理错误
on error resume next
Set n = ActiveSheet.DropDowns(Name)
on error goto 0