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

问题描述

我正在Excel表格上动态创建一组组合框,但是我只希望它们一个通用的“ Action”方法。我从一个工作表中获取数据,以填充主工作表上的组合

下面是我的代码,但是在事件触发(事件触发)时,我无法访问每个单独的组合框。有没有办法知道哪个组合框已触发?即使只是索引,名称或其他名称,我也可以去找到相关的组合框。 (组合的总数将是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

解决方法

您的代码将创建一个“表单”下拉组合框。工作表表单控件类型不公开任何事件 ...

您可以像以前一样,使用cbDataType_Change为其分配一个宏,命名为任意名称(甚至.OnAction)。

现在,可以从Application.Caller开始返回使用的对象,该Sheet.Shapes返回控件名称。基于此,可以使用OLEFormat.Object.list设置控制对象。最后,可以使用对象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 应该是这样的:

const object1 = [
  { x: 96,y: 0,type: 'type1',yaw: 0 },{ x: 192,yaw: 0 }]

const object2 = [
  { x: 96,type: 'type2',yaw: 0 }]
  
const objArray = [object1,object2]
const base_buttons = document.querySelectorAll(".base_build");

for (let i = 0; i < base_buttons.length; i++) {
    base_buttons[i].addEventListener('click',e => {
    build(objArray[i]) // pass object here as parameter here!
  });
}

const build = (builds) => {
  console.log(builds);
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...