简体   繁体   English

工作表更改事件上的 Excel VBA 组合框

[英]Excel VBA combobox on worksheet change event

I am creating a set of comboboxes dynamically on an Excel sheet but I want just one generic "Action" method for them all.我正在 Excel 工作表上动态创建一组组合框,但我只想要一个通用的“操作”方法。 I get the data from one worksheet to populate the combos on the main worksheet我从一张工作表中获取数据以填充主工作表上的组合

My code is below but I can't get access to each individual combobox when the event fires (it does fire).我的代码在下面,但是当事件触发(它确实触发)时,我无法访问每个单独的组合框。 Is there a way to know which combobox has fired?有没有办法知道哪个组合框已触发? Even if it's just the index, or name, or something, so I can then go and find the relevant combobox.即使它只是索引、名称或其他东西,我也可以去查找相关的组合框。 (The number of combos is going to be 200 in total and a Form is not what we want here for other reasons which is why its in a sheet.) (组合的数量总共将是 200,出于其他原因,我们在这里不想要一个表格,这就是为什么它在一张纸中。)

Option Explicit

Dim i As Integer
' This is used to programme the comboboxes
Private Sub GenerateComboboxes()

    On Error GoTo ErrorHandler

    Dim DestinationDataTypeCombo As Object, DestinationDataTextCombo As Object, oObject As Object
    Dim ws As Worksheet, sString As String, rng As Range
    
    Dim nCombos, nStartLine As Integer
    Dim i2, iHeight, iLeft, iTop, iWidth As Integer
    
    nCombos = 5
    nStartLine = 2
    
    Set ws = Worksheets("User Entry")
    ws.Activate
    
    'Clear what was there before
    For Each oObject In ws.Shapes
        oObject.Delete
    Next
    
    ' add each combo to the worksheet
    For i = 0 To nCombos - 1
        sString = "D" & (i + nStartLine)
        Set rng = ws.Range(sString)
        
        ' Create a Combo instance
        Set DestinationDataTypeCombo = ws.Shapes.AddFormControl(xlDropDown, _
                                      Left:=rng.Left, _
                                      Top:=rng.Top, _
                                      Width:=rng.Width, _
                                      Height:=rng.Height)
                                      
        ' Set up the properties
        With DestinationDataTypeCombo
            .ControlFormat.DropDownLines = 5
            .Name = "cbDataType" & i
            For i2 = 2 To 17
                sString = Worksheets("Static Data").Cells(i2, 1)
               .ControlFormat.AddItem sString
                
            Next
            ' Set a generic OnAction for ALL combos to use
            .OnAction = "cbDataType_Change"
        End With
       
    Next i
    
    Exit Sub
    
ErrorHandler:
    MsgBox Err.Description
     
End Sub
Public Sub cbDataType_Change()

    On Error GoTo ErrorHandler
    
    Dim sValue As String
    'This works so I know the change event fires
    MsgBox "Test"
    ' Want to get the value selected, this line errors
    sValue = cbDataType.Value
    MsgBox sValue
    
    Exit Sub
    
ErrorHandler:
    MsgBox Err.Description
     
End Sub

Your code creates a Form Drop-Down Combo box.您的代码创建了一个表单下拉组合框。 A Sheet Form control type does not expose any event ... Sheet Form 控件类型不公开任何事件......

You can assign a macro to it, named whatever you want (even cbDataType_Change ), using .OnAction , as you did.您可以使用.OnAction为它分配一个宏,命名为您想要的任何名称(甚至cbDataType_Change ),就像您所做的那样。

Now, the used object can be returned starting from the Application.Caller , which returns the control name.现在,可以从返回控件名称的Application.Caller开始返回使用的对象。 Based on it, the control object can be set, using Sheet.Shapes .基于它,可以使用Sheet.Shapes设置控制对象。 And finally, the value of such a control can be retrieved in a little more complicated way, using the object OLEFormat.Object.list .最后,可以使用对象OLEFormat.Object.list以更复杂的方式检索此类控件的值。 So, your Sub assigned to all controls should be like this:因此,分配给所有控件的Sub应该是这样的:

Sub cbDataType_Change() 'this is not an event!
    Dim cb As Object
    
    Set cb = ActiveSheet.Shapes(Application.Caller) 'the object which called this Sub
    Debug.Print cb.Name, cb.ControlFormat.Value     'returns the control name and its index
    MsgBox cb.OLEFormat.Object.list(cb.ControlFormat.Value) 'the control value
End Sub

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

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