Excel VBA - 创建一个列表框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14482435/
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 VBA - Creating a ListBox
提问by user985779
How do you create (not modify/populate) a ListBox in Excel VBA? I need to generate various ListBoxes for each cell in a column, doing it without VBA would be impractical. I'm looking for something like:
如何在 Excel VBA 中创建(而不是修改/填充)列表框?我需要为列中的每个单元格生成各种 ListBoxes,在没有 VBA 的情况下这样做是不切实际的。我正在寻找类似的东西:
Dim lb as ListBox
' Populate ListBox Here '
worksheet.Cells(1,1) = lb ' Apply ListBox to cell '
I've googled this thoroughly (maybe my terminology is wrong) but I cant find a single reference to CREATING a listbox, and applying it to various cells.
我已经彻底搜索了这个(也许我的术语是错误的),但我找不到关于创建列表框并将其应用于各种单元格的单个参考。
Thanks!
谢谢!
EDIT: It should be noted that Dim lb As MSForms.ListBox
throws the error
编辑:应该注意的是Dim lb As MSForms.ListBox
抛出错误
"User-defined type not defined"
“未定义用户定义类型”
. Maybe this is how to create ListBoxes... but I need to include this library????? I don't know how to do that though...
. 也许这就是创建 ListBoxes 的方法……但我需要包含这个库????虽然我不知道该怎么做...
回答by GeorgeVremescu
If you know how to create the listbox from Excel, not using VBA, just record one macro with you doing it and then check the resulted code.
如果您知道如何从 Excel 创建列表框,而不是使用 VBA,只需记录一个宏,然后检查生成的代码。
LE: This is what i could record. The source values were on the same sheet as the destination list, but i think you can take over from here and expand as needed.
LE:这就是我可以记录的。源值与目标列表在同一张表上,但我认为您可以从这里接管并根据需要进行扩展。
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$A:$A"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With