comboboxVB​​A Excel

我使用了大约8-10个combobox(表单控件),每个combobox都有相同的项目列表。 根据用户从下拉列表中select,某个值显示在不同的单元格中。 我想知道是否有办法做到这一点,而不使用循环(下拉菜单),因为所有function都相同。 这是我正在使用的代码:

Dim ws As Sheets Set ws = ThisWorkbook.Sheets(Array("S1 Fuel Consumption", "EF_Stat", "Summary")) Dim i As Integer For i = 1 To 8 With ws(1).Shapes("Fuel " & i).ControlFormat ~~> 'This is the loop I'm talking about(for 8 shapes) Select Case .ListIndex Case 1 ws(3).Range("B" & i).Value = Empty Case 2 ws(3).Range("B" & i).Value = ws(2).Range("B4").Value Case 3 ws(3).Range("B" & i).Value = ws(2).Range("C4").Value Case 4 ws(3).Range("B" & i).Value = ws(2).Range("D4").Value End Select End With Next i 

分配相同的macros到所有的combobox,并使用:

 With WS(1).Dropdowns(Application.Caller) 

获得触发macros的combobox的引用。

如果你需要弄清楚原来在循环中使用的“i”值,你可以这样做:

 Dim ws As Sheets Dim sCaller As String Dim i As Integer Dim rgOutput As Range Set ws = ThisWorkbook.Sheets(Array("S1 Fuel Consumption", "EF_Stat", "Summary")) sCaller = Application.Caller Set rgOutput = ws(3).Range("B" & Replace(sCaller, "Fuel ", "")) Select Case ws(1).DropDowns(sCaller).ListIndex Case 1 rgOutput.Value = vbNullString Case 2 rgOutput.Value = ws(2).Range("B4").Value Case 3 rgOutput.Value = ws(2).Range("C4").Value Case 4 rgOutput.Value = ws(2).Range("D4").Value End Select