简体   繁体   English

将Excel VBA ActiveX ListBox .Selection分配给TextBox

[英]Assigning Excel VBA ActiveX ListBox .Selection to a TextBox

I'm trying to click a selection on an ActiveX ListBox and have value assigned to a TextBox then clear the ListBox. 我试图单击ActiveX ListBox上的选择并将值分配给TextBox,然后清除ListBox。 It seems straightforward but I'm getting 'Object doesn't support this property or method' on the first line 看起来很简单,但是我在第一行得到“对象不支持此属性或方法”

This is what I'm using: 这就是我正在使用的:

Private Sub ListBox1_Click()

    ActiveSheet.OLEObjects("TextBox3").Object.Value = ActiveSheet.OLEObjects("ListBox1").Object.Selection
    ActiveSheet.OLEObjects("ListBox1").Object.Selection = ""

End Sub

Any thoughts on how to make this word or resources to search are appreciated. 任何有关如何使该单词或资源可供搜索的想法都值得赞赏。

Your code would be cleaner written like this: 您的代码将更简洁地编写为:

Private Sub ListBox1_Click()
  TextBox1.Value = ListBox1.Text
  ListBox1.Selected(ListBox1.ListIndex) = False
End Sub

But there is a problem: the second line doesn't work inside the ListBox1_Change event (because it would create an infinite loop). 但是有一个问题:第二行在ListBox1_Change事件内部不起作用(因为它将创建一个无限循环)。 I tried with Application.EnableEvents = False , but it didn't help. 我尝试使用Application.EnableEvents = False ,但没有帮助。

I think you need to put the reset of the selection in another event, like the KeyUp or MouseUp . 我认为您需要将选择的重置置于另一个事件中,例如KeyUpMouseUp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM