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.
[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