在列表框中选择一个值并将焦点设置在该值上 - VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44260829/
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
Select a value in Listbox and set focus over that value - VBA
提问by MMS
I am trying to select a value in Listbox dynamically. But am not able to do so. I am getting an error, Object doesn't support this property or method.
我正在尝试动态选择列表框中的值。但我不能这样做。我收到一个错误,对象不支持此属性或方法。
Here is the code,
这是代码,
ReqSearchvalue = Range("B" & reqrow).Value
Sheets("Main").Activate
With Sheets("Main").Ent_ListBox
.Values = ReqSearchvalue
.SetFocus = ReqSearchvalue
End With
Kindly share your thoughts. Thanks in advance !
请分享您的想法。提前致谢 !
回答by Kostas K.
Credit to @Peh for correcting this.
感谢@Peh 纠正了这一点。
Loop through the List
to find the index of the value you're looking and then set the .Selected(idx)
to True.
循环List
查找您正在查找的值的索引,然后将 设置.Selected(idx)
为 True。
Dim i As Long, found As Boolean
With Worksheets("Main").OLEObjects("Ent_ListBox").Object
For i = 0 To .ListCount - 1
If .List(i) = ReqSearchvalue Then
found = True
Exit For
End If
Next i
If found then .Selected(i) = True
End With
回答by avb
ListBox.SetFocus
is a method, it sets the focus on listbox itself, you cannot assign value to it. You must find desired item's index and use .SelectedItem(index)
then.
ListBox.SetFocus
是一种方法,它将焦点设置在列表框本身上,您不能为其赋值。您必须找到所需项目的索引并使用.SelectedItem(index)
。