简体   繁体   English

检测 ComboBox 更改 Excel VBA

[英]Detect ComboBox Change Excel VBA

I have a sheet with a bunch of ComboBoxes(form control) and I want to detect when a user changes any one of them and write text in a cell.我有一张带有一堆组合框(表单控件)的工作表,我想检测用户何时更改其中任何一个并在单元格中写入文本。 Using Worksheet_Change on the target cells doesn't work.在目标单元格上使用 Worksheet_Change 不起作用。 I have tried a bunch of things that don't work.我尝试了一堆不起作用的东西。 I'm not sure what needs to be in the private sub line or the if statement.我不确定私有子行或 if 语句中需要包含什么。

Private Sub DropDowns_DropButtonClick()
If ActiveSheet.DropDowns.Value > 1 Then
    Cells(13, 5).Font.Bold = True
    Cells(13, 5).Font.Color = vbRed
    Cells(13, 5).Value = "!!! Selections have been changed. !!!"
End If
End Sub

I have tried我努力了

ComboBox_AfterUpdate()
ComboBox_Change()
DropDowns_AfterUpdate()
DropsDowns_Change()

and anything else I could find.以及我能找到的任何其他东西。 I've also tried a few different things in the if statement with no luck.我还在 if 语句中尝试了一些不同的东西,但没有运气。

I appreciate any help.我很感激任何帮助。

Chris克里斯

Here's how you can do it by assigning a macro to the combobox (right click on the combobox>assign macro) as @BigBen mentioned in the comments section:这是通过将宏分配给 combobox (右键单击组合框>分配宏)作为评论部分中提到的@BigBen 的方法:

Option Explicit

Sub DropDown1_Change()
    Dim sht As Worksheet
    Dim dd As DropDown
    Set sht = ThisWorkbook.Worksheets("Name of your Worksheet") 'Name of the worksheet in which the combobox is located
    Set dd = sht.DropDowns("Drop Down 1") 'name of your combobox
    sht.Range("G1").Value = "The selected value is: " & dd.List(dd.Value) 'dd.value returns the index of the selected value
End Sub

You can use the same code for each one of your comboboxes.您可以为每个组合框使用相同的代码。 For demonstration purposes i have used the following set-up:出于演示目的,我使用了以下设置:

在此处输入图像描述

You can easily modify the code to best fit your needs.您可以轻松修改代码以最适合您的需求。

If I'm reading you correctly, you're comboboxes are in a userform.如果我没看错,那么您的组合框就在用户表单中。 If I'm correct, simply open your userform in 'Visual Basic' and double click on the relavant combobox.如果我是正确的,只需在“Visual Basic”中打开您的用户表单,然后双击相关的 combobox。 This will open the code pane and create an empty Private Sub routine called 'Private Sub < Combobox Name > ()'.这将打开代码窗格并创建一个名为“Private Sub < Combobox Name > ()”的空 Private Sub 例程。

Enter your code to place your data in the sheet (or whatever else you want) into the subroutine and Bob should be your uncle.输入您的代码以将您的数据(或您想要的任何其他内容)放入子例程中,Bob 应该是您的叔叔。

Apologies in advance if there's something I've missed.如果我错过了什么,请提前道歉。

RannochRob兰诺克罗布

Edit...编辑...

OK, my mistake, it's a form control.好的,我的错误,它是一个表单控件。

My first comment is that it's easier to use an activex control if you can... however, with a form control, should (a) Use the cell link box in the 'Format Control' drop down ('Control' tab) to place the result in a cell... however, that result will not be the content of the box but an integer equal to the position of the selected entry on the list of entries in the combobox.我的第一条评论是,如果可以的话,使用 activex 控件会更容易......但是,对于表单控件,应该(a)使用“格式控件”下拉菜单(“控件”选项卡)中的单元格链接框来放置单元格中的结果...但是,该结果将不是框的内容,而是 integer 等于 ZA8284521647549D6ECBB00383A3C28ZBD 中条目列表中所选条目的 position。 You then need to (b) assign a macro to the combobox which will pick up the result and use it to get the required information from the range containing the list of entries.然后,您需要 (b) 为 combobox 分配一个宏,它将获取结果并使用它从包含条目列表的范围中获取所需的信息。 Like I say, much easier with an activex control...就像我说的,使用 activex 控件更容易......

RannochRob兰诺克罗布

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

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