vba Combobox控件的点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9034432/
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
Click event of Combobox control
提问by ranganatha channarayapatna
I have hardcopies of 3000 invoices, in which the details of the product and the name of the dealer are available. The sales data from SAP has been exported to excel for analysis. But unfortunaltely the name of the dealer is not found in the exported data against each invoice. Hence I have decided to incorporate the dealer's name against each invoice. For this I have embedded a Combobox Control on to the worksheet containing the sales data, to which I have loaded the names of all dealers.
我有3000张发票的硬拷贝,里面有产品的详细信息和经销商的名字。SAP 的销售数据已导出到 excel 进行分析。但不幸的是,在每张发票的导出数据中都没有找到经销商的名字。因此我决定在每张发票上加上经销商的名字。为此,我在包含销售数据的工作表中嵌入了一个组合框控件,我已将所有经销商的名称加载到该工作表中。
To the left of the Combo control in a cell say Cells(1,1) I enter the invoice number and select the corresponding dealer as mentioned in hardcopy of the invoice from the Combo control so that with the click on the name of the dealer the name gets printed against the invoice number in the next column. To do this I have written the following macro in the Combobox1_ Click event.
在单元格中 Combo 控件的左侧说 Cells(1,1) 我输入发票编号并选择 Combo 控件中发票硬拷贝中提到的相应经销商,以便单击经销商名称名称将根据下一列中的发票编号打印。为此,我在 Combobox1_ Click 事件中编写了以下宏。
Sub Combobox1_Click()
For i = 5 to 3000
If cells(1,1).value = Cells(i,4).value then
Cells(i,5).value = Combobox1.Text
End if
Next
This works fine as long as I select different dealers for each click event. But when I select the same dealer consecutively twice the click event is not triggered and the name of the dealer is not printed in the second instance.
只要我为每个点击事件选择不同的经销商,就可以正常工作。但是当我连续两次选择同一个经销商时,不会触发点击事件,并且不会在第二次打印经销商名称。
To make the point clear say for Invoice 1233 I have selected the dealer X and the name X gets printed against invoice 1233.The next invoice 1244 also belongs to dealer X and after entering 1244 in Cells(1,1) if I select the same delaer X, the name X doesnot get printed against invoice 1244.
为了清楚地说明发票 1233,我选择了经销商 X,名称 X 打印在发票 1233 上。下一张发票 1244 也属于经销商 X,如果我选择相同,则在 Cells(1,1) 中输入 1244 后延迟 X,名称 X 不会根据发票 1244 打印。
Please advise me resolve the problem
请建议我解决问题
回答by jdh
The click event isn't being triggered because the control hasn't detected a change. You can reset the control by changing its display value either by adding this code at the end of the click event:
由于控件未检测到更改,因此未触发单击事件。您可以通过在单击事件的末尾添加以下代码来更改其显示值来重置控件:
ComboBox1.Text = "Select Dealer"
Or change the combobox display text only when you move away from the control, by adding this event:
或者仅在您离开控件时更改组合框显示文本,通过添加此事件:
Private Sub ComboBox1_LostFocus()
ComboBox1.Text = "Select Dealer"
End Sub