Reputation: 141
I am using around 8-10 comboboxes(form control),each one having the same list of items populated. Based on the user's selection from the dropdown, a certain value is displayed in a different cell. I wanted to know if there is way to do this without using a loop(for dropdowns) as all have the same function. Here is the code I am using:
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
Upvotes: 0
Views: 139
Reputation: 34035
Assign the same macro to all of the comboboxes and use:
With WS(1).Dropdowns(Application.Caller)
to get a reference to the combobox that triggered the macro.
If you need to figure out the 'i' value you were using in the loop originally, you can do something like this:
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
Upvotes: 2