简体   繁体   中英

How to incorporate this drop-down list into my program code VBA

I have the following drop-down/combo box list that has two options, namely Internal and External . My drop-down list looks like this: 在此处输入图片说明

And then I have a macro that runs through the following button: 在此处输入图片说明

My question does not revolve around the actual content of the code but rather the structure of the code to include the drop-down list, so I have simplified it a lot for the sake of getting to the point. My (simplified) code initially made a rudimentary calculation.

Sub InsertEquitiesBonds(Dim x as Double, Dim y as Double)
Dim ws as Worksheet
Set ws = Worksheets("PnL")
ws.Range("C4").Value = x + y
End Sub

I would like to create a decision logic in this code after clicking populate , so like:

If DropDown6_Change.Value = "Internal" Then
ws.Range("C4").Value = x + y 
Else 
ws.Range("C4").Value = x - y
End If

What do I need to use for the above code to be realized?

Almost there.

Sub DropDown1_Change()
    Dim ws As Worksheet
    Set ws = Sheets("Sheet1")
    x = 10
    y = 5
    With ThisWorkbook.Sheets("Sheet1").Shapes("Drop Down 1").ControlFormat
        Select Case .List(.Value)
        Case "Internal": ws.Range("C4").Value = x + y
        Case "External": ws.Range("C4").Value = x - y
        End Select
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM