简体   繁体   English

Excel VBA-如何触发列表框从另一个控件单击

[英]Excel VBA - How to trigger Listbox Click from another control

I have been working on a small project using Excel VBA. 我一直在使用Excel VBA进行一个小项目。 I have a listbox (listbox1) on the sheet that is populated with client names from a SQLServer database when the worksheet is open. 我在工作表上有一个列表框(listbox1),当工作表打开时,它用来自SQLServer数据库的客户端名称填充。 Clicking an item in the listbox selects and displays services provided. 单击列表框中的项目以选择并显示提供的服务。 This is all working correctly. 这一切都正常工作。

I have added two data picker controls that are to be used to reduce the rows selected, depending on the dates entered. 我添加了两个数据选择器控件,这些控件将用于减少所选的行,具体取决于输入的日期。 I would like to trigger the click event on the listbox with the selected entry when the dates change in the date picker controls. 当日期选择器控件中的日期更改时,我想使用所选条目触发列表框上的click事件。

My VBA is a bit rusty and I have not, for the life of me, been able to determine how to write the code in the data picker control change event to trigger the click on the list box. 我的VBA有点生疏,而且我一生都无法确定如何在数据选择器控件更改事件中编写代码以触发单击列表框。 I've tried several ways, always getting an error indicating that the "object doesn't support the property or method". 我尝试了几种方法,总是得到一个错误消息,指出“对象不支持属性或方法”。 Here is the simple code from the latest attempt: 这是最近一次尝试的简单代码:

Private Sub StartDatePicker_Change()

    ThisWorkbook.ActiveSheet.ListBox1_Click

End Sub

I'm sure that there is something simple that I'm missing. 我确定我缺少一些简单的东西。 Any help would be greatly appreciated! 任何帮助将不胜感激!

If I create a ListBox and add assign a macro to right click context menu the macro ends up in its own module. 如果我创建了一个ListBox并添加了一个为右键单击上下文菜单分配的宏,则该宏最终将位于其自己的模块中。

I am then able to simply call it by the name of the macro like ListBox1_Click . 然后,我可以通过像ListBox1_Click这样的宏名称来简单地调用它。 Did you try that already? 你已经尝试过了吗?

edited after OP's further specifications (see last part) 根据OP的进一步规范进行编辑 (请参阅最后一部分)

I'm not sure of you goal but with an ActveX Listbox control on the sheet, this will trigger its click event handler: 我不确定您的目标,但是在工作表上有一个ActveX Listbox控件,这将触发其click事件处理程序:

Private Sub StartDatePicker_Change()
    Me.ListBox1.ListIndex = 0
End Sub

of course, being an ActiveX control, this code is to be placed in the sheet code pane 当然,作为ActiveX控件,此代码将放置在工作表代码窗格中


or you could 或者你可以

  • move all your current ListBox1_Click code doing the populating work to a specific sub : 将当前所有执行填充工作的ListBox1_Click代码移动到特定的sub:

     Sub Populate() ... move here all code from ListBox1_Click doing the populating work End Sub 
  • change your Listbox1_Click() event handler as follows: 更改您的Listbox1_Click()事件处理程序,如下所示:

     Private Sub ListBox1_Click() Populate ' this will call the sub that does the populating work ... here goes other code (if any) directly connected with ListBox1 click event but not relevant for populating End Sub 
  • change your StartDatePicker_Change() event as follows: 如下更改您的StartDatePicker_Change()事件:

     Private Sub StartDatePicker_Change() Populate 'this will do the populating work End Sub 

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

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