使用VBA创buildcombobox的列表,更新代码中指定的单元格的值
我想要做的是创build一个程序,添加一些选项的combobox。 这些选项应该根据select的选项来改变我在代码中指定的某些单元格中的某些值。
这是我如何制作组合列表:
Private Sub Workbook_Open() With Worksheets("Sheet1").Columns("E") .ColumnWidth = 25 End With For i = 1 To 6 Set curCombo = Sheet1.Shapes.AddFormControl(xlDropDown, Left:=Cells(i, 5).Left, Top:=Cells(i, 5).Top, Width:=100, Height:=15) With curCombo .ControlFormat.DropDownLines = 3 .ControlFormat.AddItem "Completed", 1 .ControlFormat.AddItem "In Progress", 2 .ControlFormat.AddItem "To be done", 3 .Name = "myCombo" & CStr(i) .OnAction = "myCombo_Change" End With Next i End Sub
我希望每个下拉值触发事件myCombo_Change
,然后简单地更改单元格“D”例如,combobox3位于E3,我想要“完成”来清除单元格D3,并完成简单地存储到单元格D3的date(和时间)。 这应该在E列中的所有combobox中完成。
Private Sub myCombo_Change(index As Integer) Me.Range("D" & CStr(index)) = Me.myCombo.Value End Sub
这是我开始考虑的代码,但是我不知道如何用一个整数作为索引参数调用事件NOR如何使用所述索引访问单元格。
我想要的效果是这样的:
使用Application.Caller
获取调用myCombo_Change事件的控件的名称。
Sub myCombo_Change() Dim curCombo As Shape Set curCombo = ActiveSheet.Shapes(Application.Caller) curCombo.TopLeftCell.Offset(0, -1) = Now End Sub
将myCombo_Change分配给所有现有的DropDown:
Sub AssignMacroToAllListBoxes() Dim sh As Shape Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets For Each sh In ws.Shapes If TypeName(sh.OLEFormat.Object) = "DropDown" Then sh.OLEFormat.Object.OnAction = "myCombo_Change" End If Next Next End Sub
删除Sheet1上的所有DropDown
Sub DeleteAllDropDownsOnSheet() For Each sh In Sheet1.Shapes If TypeName(sh.OLEFormat.Object) = "DropDown" Then sh.Delete End If Next End Sub