简体   繁体   中英

Using VBA code to Copy Specific value from a cell when the dropdown box changes that value

I am new to VBA and have only recently been developing my excel skills.

I have created 3 different scenarios for an investment project situation, these scenarios appear in cell "h13" as a drop down box with three options being available, best case/worst case/base case.

When you select each scenario the various outputs will change on the sheet and I have set up the following code to change the outputs and display the relevant ones according to the scenario:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$H$13" Then
    ActiveSheet.Scenarios(Target.Value).Show
  End If

Now, what I want to achieve is the following:

In Cell E13 I have a numeric value that is my main concern (I should note this is an NPV Formula). Every time we change scenario this value obviously changes. I would like to create a summary table that is simply something like this: Scenario 1 = x Scenario 2 = y Scenario 3 = z So Ideally what I want to do is, when we select scenario 1 we copy the value from E13 to say B21. When we select the next scenario E13 will obviously change, however I would like the copied value of B21 to remain the same, and now the new Scenario 2 value to be displayed in B22.

I have no real idea how to go about this? I have tried adding this on the bottom but the values do not remain 'static'

    If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("h13")) Is Nothing Then
    Range("E13").Copy
    Range("B21:B23").PasteSpecial xlPasteValues
End If
  End Sub

Now I think I know that I need to create a reference so that it would read something like when e13=y then copy, next e13=x copy and loop? it until all outcomes have occured. Not sure how to do it though.

Any help would be appreciated, I have tried to read up on this as much as possible but I cannot really exactly pin point what I need in code terms as I am very new to this

Thanks in advance.

This solution shows the results in a "range\\table" located at B20:D23 (see pictures below)

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rTbl As Range, lRow As Long

    Application.EnableEvents = False                        'To avoid triggering again when table is updated

    If Target.Address = "$H$13" Then

        Rem Filters value in target range
        Select Case Target.Value2
        Case "Base case", "Best case", "Worst case"         'Change as required
        Case Else:  GoTo ExitTkn                            'Value is not in the list then exit
        End Select

        Rem Show Scenario
        ActiveSheet.Scenarios(Target.Value).Show

        Rem Update Results
        Set rTbl = Range("B21").Resize(3, 3)                'Change as required
        With rTbl
            lRow = WorksheetFunction.Match(Target.Value, .Columns(1), 0)
            .Cells(lRow, 2).Value = Range("E13").Value2     'Updates result - Change as required
            .Cells(lRow, 3).Value = Range("D13").Value2     'Updates scenario variable - Change as required
        End With

  End If

ExitTkn:
    Application.EnableEvents = True

  End Sub

在此处输入图片说明 在此处输入图片说明

Suggest to read the following pages to gain a deeper understanding of the resources used:

Select Case Statement , With Statement

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