Value retrieval from the Multiple selection dropdown in excel

ruts_1112

New Member
Joined
Mar 28, 2019
Messages
1
I am trying to make calculation from my dropdown menu. I have following dropdown in sheet 1 in my excel.
[TABLE="width: 95"]
<tbody>[TR]
[TD]Category
[/TD]
[/TR]
[TR]
[TD]AAA
[/TD]
[/TR]
[TR]
[TD]BBB
[/TD]
[/TR]
[TR]
[TD]CCC
[/TD]
[/TR]
[TR]
[TD]DDD
[/TD]
[/TR]
</tbody>[/TABLE]

In sheet 2, i have corresponding values for this dropdown.
[TABLE="width: 175"]
<tbody>[TR]
[TD]Category
[/TD]
[TD]Value
[/TD]
[/TR]
[TR]
[TD]AAA
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]BBB
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]CCC
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]DDD
[/TD]
[TD]4
[/TD]
[/TR]
</tbody>[/TABLE]

I added VBA code for multiple selection and also added simple Vlookup formula to retrieve the value of category.

=VLOOKUP(E2;Sheet2!I2:J5;2;)

With the VBA code, i am able to select all three category and also remove the selected category later. But I am failing to retrieve the sum of selected category. For e.g. I want if customer chooses category AAA & CCC, he/she should be able to see sum as 4. Also if customer first chooses all three category and then removes one of the category then also the sum should get updated. I am not getting how do i update my Vlookup formula to get the sum.

Here is my VBA code for multiple selection.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
        'Updated: 2016/4/12
        Dim xRng As Range
        Dim xValue1 As String
        Dim xValue2 As String
        If Target.Count > 1 Then Exit Sub
        On Error Resume Next
        Set xRng = Cells.SpecialCells(xlCellTypeAllValidation)
        If xRng Is Nothing Then Exit Sub
        Application.EnableEvents = False
        If Not Application.Intersect(Target, xRng) Is Nothing Then
            xValue2 = Target.Value
            Application.Undo
            xValue1 = Target.Value
            Target.Value = xValue2
            If xValue1 <> "" Then
                If xValue2 <> "" Then
                    '                If xValue1 = xValue2 Or _
                    '                   InStr(1, xValue1, ", " & xValue2) Or _
                    InStr(1, xValue1, xValue2 & ",") Then
                    If InStr(1, xValue1, xValue2 & ",") > 0 Then
                        xValue1 = Replace(xValue1, xValue2 & ", ", "") ' If it's in the middle with comma
                        Target.Value = xValue1
                        GoTo jumpOut
                    End If
                    If InStr(1, xValue1, ", " & xValue2) > 0 Then
                        xValue1 = Replace(xValue1, ", " & xValue2, "") ' If it's at the end with a comma in front of it
                        Target.Value = xValue1
                        GoTo jumpOut
                    End If
                    If xValue1 = xValue2 Then        ' If it is the only item in string
                        xValue1 = ""
                        Target.Value = xValue1
                        GoTo jumpOut
                    End If
                    Target.Value = xValue1 & ", " & xValue2
                End If
                jumpOut:
            End If
        End If
        Application.EnableEvents = True
    End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top