组合框VB​​A Excel

时间:2014-06-30 12:27:03

标签: excel vba excel-vba

我正在使用大约8-10个组合框(表单控件),每个组合框都填充了相同的项目列表。根据用户从下拉列表中的选择,在不同的单元格中显示某个值。我想知道是否有办法在不使用循环(用于下拉列表)的情况下执行此操作,因为它们都具有相同的功能。这是我正在使用的代码:

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

1 个答案:

答案 0 :(得分:2)

将相同的宏分配给所有组合框并使用:

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

获取对触发宏的组合框的引用。

如果你需要找出' 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