Sum of multiple variables within a cell based on a key

GuyGadois

Active Member
Joined
Jan 12, 2009
Messages
344
Office Version
  1. 2019
Platform
  1. Windows
I have the following sheet. I am trying to find a simple solution where the user would input the Items (yellow shading) and the Sum column is automatically calculated based on the items listed in the Items column. The key will end up being closer to 10 items with associated costs. A client may have multiple items (i.e. A,A,A) which I would like to add up. They may also have up to ten items. Is I possible to add up all the associated 'costs' in the item fields?

Cheers,

GG

ClientItemsSumKey
Client #1A$ 10.00Item Cost
Client #2A, B$ 30.00A$ 10.00
Client #3D$ 40.00B$ 20.00
Client #4D, C$ 70.00C$ 30.00
D$ 40.00
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Here's a UDF you can try. Install it, then use it like a worksheet function as in the example below:
Book1
ABCDEF
1ClientItemsSumKey
2Client #1A10Item Cost
3Client #2A, B30A10
4Client #3D40B20
5Client #4D, C70C30
6Client #50D40
7Client #6X, Y, Z#N/A
Sheet1
Cell Formulas
RangeFormula
C2:C7C2=SumKeys(B2)

VBA Code:
Function SumKeys(S As String) As Variant
Dim Vitem As Variant, Vsum As Variant, i As Long, j As Long, ct As Long
Vitem = Range("E3:E6").Value
Vsum = Split(S, ", ")
For i = LBound(Vsum) To UBound(Vsum)
    For j = LBound(Vitem, 1) To UBound(Vitem, 1)
        If InStr(1, Vsum(i), Vitem(j, 1)) > 0 Then
            ct = ct + 1
            If ct = UBound(Vsum) + 1 Then
                GoTo Nx1
            Else
                GoTo Nx2
            End If
        
        End If
    Next j
Nx2:
Next i
Nx1:
If ct <> UBound(Vsum) + 1 Then
    SumKeys = CVErr(xlErrNA)
    Exit Function
End If
For i = LBound(Vsum) To UBound(Vsum)
    SumKeys = SumKeys + Application.Lookup(Vsum(i), Range("E3:E6"), Range("F3:F6"))
Next i
End Function
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,726
Members
452,939
Latest member
WCrawford

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